R:添加两条具有相同数据的回归线 [英] R: adding two regression line with same data

查看:54
本文介绍了R:添加两条具有相同数据的回归线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的数据绘制回归线.这是我的数据

图书馆(汽车)数据(萨林斯)m1<- lm(英亩~消费者,数据=萨林斯)

然后我删除第 4 行,得到一个新的数据集,如下所示.

Sahlins[-c( 4), ]a<- Sahlins[-c(4), ]m2 <- lm(英亩~消费者,数据=a)

我尝试为原始数据集 Sahlins 添加两条回归线(m1 和 m2).但它只是不起作用.仅适用于两个单独的 plot .这是我的 r 代码.

库(ggplot2)ggplot(萨林斯,aes(消费者,英亩))+geom_point()+geom_smooth(方法=lm",se=F)ggplot(a,aes(consumers,acres))+geom_point()+geom_smooth(method="lm", se=F)

如何在一个图中得到两条回归线?这是我想要的情节.

I am plotting regression line for my data. Here is the data i have

library(car)
data(Sahlins)
m1<- lm(acres~consumers,data=Sahlins)

then i delete row 4, get a new data set as below.

Sahlins[-c( 4), ]
a<- Sahlins[-c( 4), ]
m2 <- lm(acres~consumers,data=a)

I try to adding two regression line (m1, and m2) for original data set Sahlins. but it just not work. Only work with two separate plot . here is the r code i have.

library(ggplot2)
ggplot(Sahlins,aes(consumers,acres))+geom_point()+geom_smooth(method="lm", se=F) 
ggplot(a,aes(consumers,acres))+geom_point()+geom_smooth(method="lm", se=F) 

how can i get two regression line in one plot ? this is the plot i want. enter image description here Thank you .

解决方案

library(car)
data(Sahlins)

ggplot(Sahlins, aes(consumers, acres)) + 
  geom_point() + 
  geom_smooth(method="lm", se=F) +
  geom_smooth(data = Sahlins[-4, ], method="lm", se=F)

这篇关于R:添加两条具有相同数据的回归线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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