在ggplots中添加两条线性回归线和两条y轴 [英] add two linear regression lines and two y axis in ggplots

查看:184
本文介绍了在ggplots中添加两条线性回归线和两条y轴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个带有两个y轴的图,并向每组添加两条线性回归线.我已经在该网站上看到了一些方法,对我来说很容易获得两个y轴.但是我不能控制线性回归线. 我想这样绘制的情节:

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:

我的数据如下:

   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:

似乎sec.axis方法无法控制第二条回归线.

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

有人可以给我一些提醒吗? 谢谢.

Could anyone give me some reminders? Tons of thanks.

推荐答案

这个问题似乎重复了. 链接

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

这篇关于在ggplots中添加两条线性回归线和两条y轴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆