2016-09-14 99 views
1
mod = lm(iris$Petal.Length ~ iris$Petal.Width + iris$Species) 
plot(iris$Petal.Length ~ iris$Petal.Width, col = iris$Species) 
abline(mod) 

enter image description hereR:添加多个回归线和黄土曲线绘制

在这里,我分层Species,我想绘制3次回归线,每一个物种。 abline(mod)似乎只能添加一行。另外,如果我想添加LOESS曲线呢?

+0

你想要在基本情节而不是ggplot,这使得这种事情超级简单吗? – RoyalTS

回答

2

一个ggplot一行代码:

library(ggplot2) 

ggplot(iris, aes(Petal.Width, Petal.Length, color=Species)) + geom_point() + geom_smooth(method='lm', formula=y~x) 

离开了参数geom_smooth(),你会得到黄土线。但是,这里的数据非常少,以至于失败。

1
mod = lm(iris$Petal.Length ~ iris$Petal.Width + iris$Species) 
plot(iris$Petal.Length ~ iris$Petal.Width, col = iris$Species) 
abline(coefficients(mod)[1], coefficients(mod)[2]) 
abline(coefficients(mod)[1], coefficients(mod)[3]) 
abline(coefficients(mod)[1], coefficients(mod)[4])