2016-09-14 128 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])