rm(list = ls())
library(ggplot2)
data = data.frame(x = runif(100, 1, 10), y1 = rnorm(100, 4,1), y2 = rgamma(100, 5,6))
ggplot(data=data,aes(x = x,y=y1))+
geom_point(aes(y=y1,color = "Y1"))+
stat_smooth(aes(x = x,y=y1), method=lm, se = FALSE, color = "#66C2A5", formula = y ~ x ) +
geom_point(data = data,aes(x=x,y=y2,color="Y2"))+
stat_smooth(aes(x = x,y=y2), method=lm, se = FALSE, color = "red", formula = y ~ x ) +
scale_y_continuous("Normal", sec.axis = sec_axis(y2~ .*5 , name = "Gamma"))+
xlab("Unif") + expand_limits(x = 0, y = 0)+
theme(axis.text.y.left = element_text(color = "black"),
axis.text.y.right = element_text(color = "red"))+
theme_light()+
theme(legend.position="bottom")
I want to change the direction of the second y-axis
, starting from bottom to top (similar to the primary y-axis
. Is there any way to do this in ggplot2
?