【问题标题】:How to plot regression lines for each group separately and all groups together in one plot?如何在一个图中分别绘制每个组的回归线和所有组的回归线?
【发布时间】:2021-08-04 14:27:47
【问题描述】:

我已经分别为每个组绘制了回归线。如何添加一条结合了所有组信息的其他回归线?

这是我原来的 R 脚本:

plotsr.sp <- ggplot(data = result.sp.melt, 
                    aes(x=belt,y=sr, group = site, color = site))+
             geom_smooth(method = "lm", se = F)+
             scale_x_discrete(labels = c("Low","Mid","High"))

这是一个示例。我要补充的是中间的黑线。

【问题讨论】:

    标签: r ggplot2 regression


    【解决方案1】:

    这可以通过两个geom_smooth 层来实现。第一个使用分组变量作为局部美学,这将为您提供组的回归线,而第二个将为总样本添加回归线:

    library(ggplot2)
    
    ggplot(data = result.sp.melt, aes(x=belt,y=sr))+
      geom_smooth(aes(group = site, color = site), method = "lm", se = F)+
      geom_smooth(method = "lm", se = F, color = "black")
    

    使用mtcars作为示例数据:

    ggplot(data = mtcars, aes(x=hp,y=mpg))+
      geom_smooth(aes(color = factor(cyl)), method = "lm", se = F)+
      geom_smooth(method = "lm", se = F, color = "black")
    

    【讨论】:

      猜你喜欢
      • 2021-04-28
      • 2019-06-16
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-12
      • 1970-01-01
      • 2018-07-10
      相关资源
      最近更新 更多