2

I know that it's generally looked down upon to have two y-axes for several reasons, but there is a very specific reason for having it here as the two are basically the same concepts and need to be plotted side by side.

Anyway, what I'm hoping to do here is have something similar to

scale_y_reverse(breaks=seq(0,1,.05),
                   sec.axis = sec_axis(~ 1-., name = "SecondAxis", breaks=seq(0,1,.05) ))

Unfortunately, this doesn't seem to work. I need the primary axis to go from 1 to zero as it goes up, and the second y axis to go from 0 to 1 as it goes up. enter image description here

Doesn't seem to work unfortunately from the code I have above as you can see above, which was proposed as a solution in another thread.

Marco Sandri
  • 23,289
  • 7
  • 54
  • 58
zad
  • 83
  • 7

1 Answers1

1

I can't reproduce your issue.

Update: This seems to be an issue (bug) specific to ggplot2_3.1.0. There exist a couple of issues on GitHub related to unexpected behaviour of sec_axis in 3.1.0: sec_axis formula behaviour #2974, Ticks misaligned for sec_axis with some scale transformations and data in 3.1.0 #2978.

The following example is reproducible in ggplot2_3.0.0 but fails in ggplot2_3.1.0.


Here is a minimal & reproducible example, could you please double-check that this works for you.

# Generate sample data
x <- seq(1, 4 * pi, length.out = 100)
y <- sin(x)^2

library(ggplot2)
ggplot(data.frame(x, y), aes(x, y)) +
    geom_point() +
    scale_y_reverse(
        breaks = seq(0, 1, 0.1),
        sec.axis = sec_axis(~ 1 - ., name = "SecondAxis", breaks = seq(0, 1, 0.1)))

enter image description here

Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • That's weird. I can't get what you get. It gives me the same y-axis on both sides with your exact code. – zad Dec 05 '18 at 04:47
  • sorry, but I'm unable to reproduce that on RStudio Cloud either, where I have a clean slate and nothing there. Could you check which version of ggplot2 you have? – zad Dec 05 '18 at 05:01
  • @dailyzad That's strange indeed. What `ggplot2` version are you using? I've tested this on `ggplot2_3.0.0` with `R_3.5.1`. – Maurits Evers Dec 05 '18 at 05:02
  • `RStudio 1.1.463` `R 3.5.1 ` `ggplot2 3.1.0` – zad Dec 05 '18 at 05:04
  • @dailyzad Wow that's really weird. I've just upgraded `ggplot2` to `3.1.0` and now I can't reproduce above plot either. So something must've changed between `3.0.0` and `3.1.0`. – Maurits Evers Dec 05 '18 at 05:08
  • Well that's ridiculous! I'm not sure it's a good change either. Perhaps this needs to be pointed out to the RStudio team if it wasn't intentional. – zad Dec 05 '18 at 05:10
  • @dailyzad This seems like a bug in `ggplot2_3.1.0` to me. It might be related to [this issue](https://github.com/tidyverse/ggplot2/issues/2974) and/or [this issue](https://github.com/tidyverse/ggplot2/issues/2978), which should get patched soon (it seems that there are a few issues related to odd behaviour of `sec_axis` in `3.1.0`). Regrettably this is doesn't really offer a solution to your problem. – Maurits Evers Dec 05 '18 at 05:14
  • thanks. This is super helpful. I'm currently writing some functions for a package and I'll downgrade to a later version for now and watch this bug like a hawk. Maybe even point the RStudio team towards this particular thread. Thanks again! – zad Dec 05 '18 at 05:19