R ggplot - 将多个线性回归线绘制为单个图

时间:2021-01-15 12:12:07

标签: r ggplot2 linear-regression

我有 3 个不同的线性回归模型,比如 -lm1, lm2, lm3,它们是使用标准 R lm 创建的

lm1 = loess(df1$y ~ df1$x, span=0.10)
lm2 = loess(df2$y ~ df2$x, span=0.10)
lm3 = loess(df3$y ~ df3$x, span=0.10)

我想使用 ggplot 将这 3 个模型的拟合线合并到一个图中。获得如下图的正确方法是什么,每个模型都有一条线,每个模型都装有相应数据框中的输入数据。

enter image description here

1 个答案:

答案 0 :(得分:0)

对于每个 df,您的 X 和 Y 变量是否相同?如果是这样,你总是可以 rbind() 你的 dfs 并创建另一个变量(例如,df_source)并按照以下方式做一些事情:

'''
ggplot(total_df, aes(x = xvar, y = yvar, color = df_source))+
    geom_jitter()+
    geom_smooth(method = "lm", se = F)
'''

抖动对我的好处是看到每个 df 或“条件”的基础数据分布。