【问题标题】:Adding Error Bars to ggplot2 Boxplot in R?在 R 中向 ggplot2 Boxplot 添加误差线?
【发布时间】:2018-08-01 18:11:56
【问题描述】:
alphastats<-summarySE(map, measurevar="shannon", groupvars=c("age_class"))

 age_class      N  shannon        sd         se        ci
1   Non_Smoker 66 5.473424 0.4152997 0.05111986 0.1020934
2   Old_Smoker 47 5.271223 0.6046414 0.08819601 0.1775294
3 Young_Smoker 17 5.324977 0.8682071 0.21057116 0.4463909

嗨,所以我正在尝试将误差线添加到 R 中的 ggplot2 箱线图中。我使用我的三个组中的每一个组的必要标准误差数据创建了上述数据框。

ggplot() + 
geom_boxplot(data=map, aes(x=age_class, y=shannon, fill=age_class), na.rm= TRUE ) + 
theme_bw() + 
geom_jitter(data=map, aes(x=age_class, y=shannon), position=position_jitter(width=0.1)) + 
labs(x="Group", y="Shannon Value") + 
guides(fill=guide_legend(title="Group Type")) + 
annotate("text", x=0.75, y=3.5, size=3, label=paste0("p-value =",alpha_p_round)) + 
geom_errorbar(data=alphastats, aes(ymin=shannon-se, ymax=shannon+se))

当我尝试通过 gg_errorbar() 添加错误栏时,我得到了错误:

" eval(expr, envir, enclos) 中的错误:找不到对象'x'

另外:警告信息:

1: 在 min(x, na.rm = na.rm) :

min 没有非缺失参数;返回Inf

2:在 max(x, na.rm = na.rm) 中:

max 没有非缺失参数;返回 -Inf

3: 在 min(diff(sort(x))) 中:min 没有非缺失参数;返回

Inf

谁能帮我弄清楚我做错了什么?

【问题讨论】:

标签: r ggplot2


【解决方案1】:

您的示例不可重现,但根据您提供的示例数据,以下工作有效:

alphastats <- read.table(
    text = " age_class      N  shannon        sd         se        ci
1   Non_Smoker 66 5.473424 0.4152997 0.05111986 0.1020934
2   Old_Smoker 47 5.271223 0.6046414 0.08819601 0.1775294
3 Young_Smoker 17 5.324977 0.8682071 0.21057116 0.4463909", header = T)

library(ggplot2);
ggplot(alphastats, aes(x = age_class, y = shannon)) +
    geom_point() + 
    theme_bw() +
    labs(x = "Group", y = "Shannon Value") +
    guides(fill=guide_legend(title = "Group Type")) +
    geom_errorbar(aes(ymin = shannon - se, ymax = shannon + se))

【讨论】:

    【解决方案2】:

    您没有在geom_errorbar 中提供x=age_class,因此geom_errorbar 不知道误差线的x 坐标。如果将x=age_class 添加到geom_errorbar,代码将起作用。

    您还可以稍微缩短代码。由于geom_errorbar 使用与其他两个几何图形相同的xy 变量,另一种选择是使用ggplot(map, aes(x=age_class, y=shannon)) + ...。这意味着所有几何图形将分别使用map 数据框和age_classshannon 列用于x 和y,除非另有说明。

    然后,在geom_errorbar 中,您只需为其提供新的数据框以及yminymax 美学。但是,您不需要提供xy 美学,除非您希望geom_errorbar 使用与主要ggplot 调用中使用的不同的列。

    所以代码是:

    ggplot(map, aes(x=age_class, y=shannon)) + 
      geom_boxplot(aes(fill=age_class)) + 
      geom_jitter(width=0.1) +   # geom_jitter takes a direct width argument. You can use the `position` argument, but it's not necessary.
      geom_errorbar(data=alphastats, aes(ymin=shannon-se, ymax=shannon+se)) +
      labs(x="Group", y="Shannon Value", fill="Group Type") +  # Note that the fill label has been moved to labs
      annotate("text", x=0.75, y=3.5, size=3, label=paste0("p-value =", alpha_p_round)) +
      theme_bw()
    

    你确定要geom_jitter吗?如果您希望shannon 的平均值与误差线一致,请使用geom_point。如果您希望它们都移动相同的量,请使用position_nudge

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      相关资源
      最近更新 更多