R: adding two regression line with same data

2019-08-16 12:14发布

问题:

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 .

回答1:

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 plot ggplot2