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.
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.
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)