0

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.

Love_qq_xq
  • 148
  • 13
  • Possible duplicate of [Plotting two variables as lines using ggplot2 on the same graph](https://stackoverflow.com/questions/3777174/plotting-two-variables-as-lines-using-ggplot2-on-the-same-graph) – mysteRious Jul 29 '18 at 04:50
  • Thanks for your reminder. I have seen this question. I can control the point with two y axes. But I can not control the linear regression lines. – Love_qq_xq Jul 29 '18 at 04:53
  • The two y-axes solution is at: https://stackoverflow.com/questions/3099219/plot-with-2-y-axes-one-y-axis-on-the-left-and-another-y-axis-on-the-right – mysteRious Jul 29 '18 at 04:54
  • Don’t use ggplot2 functions. Use ‘predict’. – IRTFM Jul 29 '18 at 06:05
  • Do you mean I didn't use ggplot2 to draw this plot? What is predict? Is it a package? – Love_qq_xq Jul 29 '18 at 06:17
  • Possible duplicate of [Multiple y axis for bar plot and line graph using ggplot](https://stackoverflow.com/questions/45194147/multiple-y-axis-for-bar-plot-and-line-graph-using-ggplot) – Iman Jul 29 '18 at 11:30

1 Answers1

1

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

Iman
  • 2,224
  • 15
  • 35