R:添加多条回归线和黄土曲线进行绘图

R: adding multiple regression lines and loess curve to plot

mod = lm(iris$Petal.Length ~ iris$Petal.Width + iris$Species)
plot(iris$Petal.Length ~ iris$Petal.Width, col = iris$Species)
abline(mod)

我在这里进行分层 Species,我想绘制 3 条回归线,每个物种一条。 abline(mod) 好像只能加一行而已。另外,如果我想添加 LOESS 曲线怎么办?

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])

一个 ggplot 单线:

library(ggplot2)

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

省略 geom_smooth() 的参数,您将得到 LOESS 行。但是,这里的数据太少了,所以失败了。