Reputation: 9793
mod = lm(iris$Petal.Length ~ iris$Petal.Width + iris$Species)
plot(iris$Petal.Length ~ iris$Petal.Width, col = iris$Species)
abline(mod)
Here I'm stratifying Species
and I want to plot 3 regression lines, one for each species. abline(mod)
seems to only add one line only. Also, what if I wanted to add LOESS curve?
Upvotes: 2
Views: 613
Reputation: 10203
A ggplot one-liner:
library(ggplot2)
ggplot(iris, aes(Petal.Width, Petal.Length, color=Species)) + geom_point() + geom_smooth(method='lm', formula=y~x)
Leave out the arguments to geom_smooth()
and you would get the LOESS line. However, the data here are so scant that this fails.
Upvotes: 3
Reputation: 2085
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])
Upvotes: 2