简体   繁体   English

将错误栏添加到R中的ggplot2 Boxplot?

[英]Adding Error Bars to ggplot2 Boxplot in R?

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

Hi, so I'm trying to add error bars to a ggplot2 boxplot in R. I have the above data frame created with the necessary standard error data for each of my three groups. 嗨,我想在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))

When I attempt to add the error bars through gg_errorbar(), I get the error: 当我尝试通过gg_errorbar()添加错误栏时,出现错误:

"Error in eval(expr, envir, enclos) : object 'x' not found “ eval(expr,envir,enclos)中的错误:找不到对象'x'

In addition: Warning messages: 另外:警告消息:

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

no non-missing arguments to min; 没有min的必填项; returning Inf 返回Inf

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

no non-missing arguments to max; 没有max的必填项; returning -Inf 返回-Inf

3: In min(diff(sort(x))) : no non-missing arguments to min; 3:在min(diff(sort(x)))中:没有min的必填参数; returning 返回

Inf 信息

Could anyone help me in figuring out what I'm doing wrong? 有人可以帮我弄清楚我在做什么错吗?

Your example is not reproducible, but based on the sample data you provide, the following works: 您的示例不可复制,但是根据您提供的示例数据,以下工作有效:

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

在此处输入图片说明

You haven't provided x=age_class in geom_errorbar , so geom_errorbar doesn't know the x coordinates for the error bars. 您尚未在geom_errorbar提供x=age_class ,因此geom_errorbar不知道误差线的x坐标。 If you add x=age_class to geom_errorbar , the code will work. 如果将x=age_class添加到geom_errorbar ,则代码将起作用。

You could also shorten your code somewhat. 您也可以稍微缩短代码。 Since geom_errorbar uses the same x and y variables as the other two geoms, another option would be to do ggplot(map, aes(x=age_class, y=shannon)) + ... . 由于geom_errorbar使用与其他两个geoms相同的xy变量,因此另一个选择是执行ggplot(map, aes(x=age_class, y=shannon)) + ... This means that all geoms will use the map data frame and the age_class and shannon columns for x and y, respectively, unless told to do otherwise. 这意味着所有geoms将使用map数据帧和age_classshannon列x和y,分别除非额外的事情。

Then, in geom_errorbar , you just need to feed it the new data frame and the ymin and ymax aesthetics. 然后,在geom_errorbar ,您只需为其提供新的数据框以及yminymax美观度即可。 However, you don't need to provide x or y aesthetics unless you want geom_errorbar to use different columns for those than were used in the main ggplot call. 但是,除非您希望geom_errorbar为主ggplot调用中使用的列使用不同的列,否则无需提供xy美观。

So the code would be: 因此,代码将是:

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

Are you sure you want geom_jitter ? 您确定要geom_jitter吗? If you want the mean value of shannon to coincide with the error bar, use geom_point . 如果您希望shannon的平均值与误差线一致,请使用geom_point If you want them both shifted by the same amount, use position_nudge . 如果您希望它们偏移相同的量,请使用position_nudge

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM