0

How to get the secondary y axis in reverse order in ggplot, starting zero as top down pattern. I tried few transformation and breaks but didn't quite the get the things what I was expecting.

Any relevant examples with code will be helpful.

Thanks.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
Saurav Das
  • 104
  • 2
  • Hi Saurav, welcome to Stack Overflow. It will be extremely challenging to answer your question without **1)** the code you've tried thus far and **2)** at least a sample of your data. Please edit your question with the output of `dput(data)` or `dput(head(data))` if your data is very large. See [How to make a great R reproducible example](https://stackoverflow.com/a/5963610/) for more. – Ian Campbell Nov 27 '20 at 21:57

1 Answers1

1

Do you mean like this?

Data

set.seed(1)

df <- data.frame(x = 1:10, y1 = runif(10, 0, 10), y2 = runif(10, -10, 0))

df
#>     x        y1          y2
#> 1   1 2.6550866 -7.94025425
#> 2   2 3.7212390 -8.23443247
#> 3   3 5.7285336 -3.12977153
#> 4   4 9.0820779 -6.15896282
#> 5   5 2.0168193 -2.30158580
#> 6   6 8.9838968 -5.02300758
#> 7   7 9.4467527 -2.82381492
#> 8   8 6.6079779 -0.08093905
#> 9   9 6.2911404 -6.19964821
#> 10 10 0.6178627 -2.22554779

Code

library(ggplot2)

ggplot(df, aes(x, y1)) +
  geom_line() +
  geom_line(aes(y = 10 + y2), colour = "red") +
  scale_y_continuous(sec.axis = sec_axis(trans = ~ 10 -., name = "y2"))

Created on 2020-11-27 by the reprex package (v0.3.0)

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87