add two linear regression lines and two y axis in

2020-05-08 07:25发布

I want to make a plot with two y-axes and add two linear regression lines to each group. I have seen some methods on this website, and it is easy for me to get two y-axes for the points. But I can not control the linear regression lines. The plot I want to make like this:

enter image description here

My data are as following:

   ggplot(plotdata) + geom_point(aes(x,z),colour="green") + 
   geom_smooth(aes(x,y), method=lm, se=FALSE,colour="green") + 
   geom_point(aes(x,y*100), colour="red") + 
   geom_smooth(aes(x,y*100), method=lm, se=FALSE,colour="red") +  
   scale_y_continuous(sec.axis = sec_axis(~ . * 0.01))

I used the upper code to make this plot, but I always got the following result:

enter image description here

It seems like the sec.axis method cannot control the second regression line.

Could anyone give me some reminders? Tons of thanks.

标签: r ggplot2
1条回答
神经病院院长
2楼-- · 2020-05-08 08:00

This question seems to be duplicated. link

library(ggplot2)
z=200*(y=1:10)
x=2.7*y

factor<-50

p1<-ggplot(plotdata) + geom_point(aes(x,y*factor), colour="red") +
  geom_smooth(aes(x,y*factor), method=lm, se=FALSE,colour="red") 

p1+geom_point(aes(x,z),colour="green") +  labs(y="z")+
  geom_smooth(aes(x,z), method=lm, se=FALSE,colour="green")+     scale_y_continuous(sec.axis = sec_axis(~ . / 50))

enter image description here

查看更多
登录 后发表回答