如何更改ggplot2中第二个y轴标签的方向

时间:2021-02-19 05:01:50

标签: r ggplot2

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

我想改变第二个 y-axis 的方向,从下到上(类似于主 y-axis。有没有办法在 ggplot2 中做到这一点?

>

1 个答案:

答案 0 :(得分:0)

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