0
votes
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.

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:

"Error in eval(expr, envir, enclos) : object 'x' not found

In addition: Warning messages:

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

no non-missing arguments to min; returning Inf

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

no non-missing arguments to max; returning -Inf

3: In min(diff(sort(x))) : no non-missing arguments to min; returning

Inf

Could anyone help me in figuring out what I'm doing wrong?

2
Your example code is not reproducible. map and alpha_p_round are not defined.Maurits Evers
Please take a look at How to create a Minimal, Complete, and Verifiable example and then edit your question accordingly.Brien Foss

2 Answers

0
votes

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

enter image description here

0
votes

You haven't provided x=age_class in geom_errorbar, so geom_errorbar doesn't know the x coordinates for the error bars. If you add x=age_class to geom_errorbar, the code will work.

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

Then, in geom_errorbar, you just need to feed it the new data frame and the ymin and ymax aesthetics. 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.

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? If you want the mean value of shannon to coincide with the error bar, use geom_point. If you want them both shifted by the same amount, use position_nudge.