3
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?

score324
  • 687
  • 10
  • 18
  • very related: https://stackoverflow.com/questions/45683358/change-secondary-line-axis-color – tjebo Feb 19 '21 at 06:35

1 Answers1

4

add + theme(axis.title.y.right = element_text(angle = 90))enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53