【问题标题】:How to plot regression line for a 2x2x2 design in ggplot2 in R?如何在 R 中的 ggplot2 中绘制 2x2x2 设计的回归线?
【发布时间】:2018-05-27 03:32:55
【问题描述】:

我的数据 (dtf.long) 如下所示:

nutrition fertilizer season seedlingdensity plandensity fitted
  nitrogen  none       wet        5             19       6
  nitrogen  none       dry        5             19       8
  nitrogen  phos       wet        4             23       16
  nitrogen  phos       dry        5             19       10
  iron      none       wet        5             29       21
  iron      none       dry        5             19       14
  iron      phos       wet        4             23       12
  iron      phos       dry        5             20       14
  ....
  ...

总共有 16 个重复。我想用 y 轴上的 log(seedlingdensity) 和 x 轴上的 log(plandensity) 绘制回归,由食物分面。这两种肥料可以一种颜色和不同的季节。

我试着写了一段代码,但我仍然不知道如何为赛季的 pch 编写代码

回归的模型拟合存储在拟合列中

summary_dat = dtf.long %>%
              group_by(nutrition, fertilizer, season) %>%
              summarise(mean_predict=mean(fitted),
                        sd_predict = sd(fitted),
                        n_predict = n()) %>%

  mutate(se_predict = sd_predict / sqrt(n_predict),
         lower_ci = mean_predict - qt(1 - (0.05 / 2), n_predict - 1) * se_predict,
         upper_ci = mean_predict + qt(1 - (0.05 / 2), n_predict - 1) * se_predict)

ggplot() + 
  geom_point(data=dtf.long, aes(x=log(plantdensity), y=log(seedlingdensity), group=fertilizer, color = fertilizer), position=position_dodge(width=0.5)) + 
  geom_errorbar(data=summary_dat, aes(x=log(plantdensity), ymax=upper_ci, ymin=lower_ci, group=fertilizer, color=fertilizer), position=position_dodge(width=0.5), width=0.2) + 
  geom_point(data=summary_dat, aes(log(plantdensity), y=mean_predict, group=fertilizer, color=fertilizer), size=3, position=position_dodge(width=0.5)) + 
  facet_grid(nutrition ~ .) + xlab("log(Plant Density") + ylab("log(Seedling Density)")

【问题讨论】:

    标签: r ggplot2 regression mixed-models


    【解决方案1】:

    您可以将参数shape 添加到对geom_point 的调用中,并将其映射到data.frame 中变量season 的值。

    ggplot() + 
     geom_point(data=dtf.long, 
        aes(x=log(plantdensity), 
            y=log(seedlingdensity), 
            group=fertilizer, 
            color = fertilizer,
            shape = season), 
        position=position_dodge(width=0.5)) + 
     geom_errorbar(data=summary_dat, 
        aes(x=log(plantdensity), 
            ymax=upper_ci, 
            ymin=lower_ci), 
        position=position_dodge(width=0.5), width=0.2) + 
     facet_grid(nutrition ~ .) + 
     xlab("log(Plant Density") + 
     ylab("log(Seedling Density)")
    

    您可以在this page on the ggplot site找到更多示例

    【讨论】:

      猜你喜欢
      • 2018-02-27
      • 1970-01-01
      • 2020-10-07
      • 2017-07-02
      • 2015-01-15
      • 1970-01-01
      • 2017-02-05
      • 2020-12-25
      • 2014-07-06
      相关资源
      最近更新 更多