1

I have recently updated a graph that I made a month or so ago using ggplot2 that has two y-axes. The second axis is a conversion of the primary axis into different units to enable comparison with the literature.

Now, possibly after the latest update, some secondary axis arguments are interfering with the primary axis. My own graph is relatively complicated, but the same problem can be produced with the following example using the iris dataset.

I'll just provide code to plot the frames here as the data are irrelevant. I tried to include images but there seems to be a serious problem with SO's image hosting right now.

  1. A simple graph places the y axis and label at left, notice the range of the data from 0–2:

    ggplot(iris, aes(x=Sepal.Width, y = Petal.Width))
    
  2. A secondary axis that just adds 100 to the original value can be added to the right hand side like this:

    ggplot(iris, aes(x=Sepal.Width, y = Petal.Width)) + 
    scale_y_continuous(sec.axis = sec_axis(~.+100))
    
  3. If I add a name to the second axis, that names switches to the left hand side, replacing the primary axis label on the primary axis:

    ggplot(iris, aes(x=Sepal.Width, y = Petal.Width)) + 
    scale_y_continuous(sec.axis = sec_axis(~.+100), 
    name = "bogus secondary axis")
    

That's a problem, as I now have an incorrect label on my primary axis and an unlabelled secondary axis. It doesn't matter if I explicitly state a y-axis label using labs(), it will overwrite it just the same.

  1. I tried specifying that the secondary axis go on the right hand side using the position argument, but that only reversed example 2.

    ggplot(iris, aes(x=Sepal.Width, y = Petal.Width)) + 
    scale_y_continuous(sec.axis = sec_axis(~.+100), 
    name = "bogus secondary axis",
    position = 'right')
    

So the additional arguments to scale_y_continuous() are being applied to the primary axis, not the secondary. If I merely duplicate the primary axis everything works as expected:

 ggplot(iris, aes(x=Sepal.Width, y = Petal.Width)) + 
  scale_y_continuous(sec.axis = dup_axis())

Can anyone see what I am doing wrong or tell me how this kind of thing should be specified now? I want my secondary axis margin and label back!

dhd
  • 79
  • 7
  • How about adding `ylab("primary axis")` like [this](https://stackoverflow.com/a/51458034/786542) ? – Tung Aug 08 '18 at 04:19
  • @Tung inserting that argument doesn't have any effect as far as I can see. Did you try it on the example above? – dhd Aug 08 '18 at 04:31

1 Answers1

1

This works for me. Note the name = inside the sec_axis()

library(ggplot2)

ggplot(iris, aes(x = Sepal.Width, y = Petal.Width)) +
  ylab("primary axis") +
  scale_y_continuous(sec.axis = sec_axis(~. + 100,
    name = "bogus secondary axis"))

Created on 2018-08-07 by the reprex package (v0.2.0.9000).

Tung
  • 26,371
  • 7
  • 91
  • 115
  • 1
    that's it! Solves my minimum reproducible example, and the hideously elaborate version that prompted it. – dhd Aug 08 '18 at 06:15