Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add SE error bars to my barplot in ggplot2?

I made a simple barplot with ggplot2 comparing the mean lifespan (age) of males and females for 2 insect species. My code looks like this, with "dataset" being, well, my data set...

    gplot(dataset, aes(Species, Age, fill=Sex))+
stat_summary(fun.y = mean, geom = "bar", position = "dodge")+
scale_fill_manual(values = c("Grey25", "Grey"))+
theme(legend.title = element_blank())+
scale_y_continuous(limits = c(0,15))

I tried using the following code to manually enter the value of the mean±SE to set the limits for the error bar. For the sake of simplicity, let's assume mean=10 and SE=0.5 for males of species1.

geom_errorbar(aes(ymin=9.5, ymax=10.5),width=.2,position=position_dodge(.9))

This code does indeed work, but it sets the same error bars for each bar in my plot.

How can I add error bars equal to the corresponding SE for each bar in my plot?

I am fairly new to ggplot and R in general so any help/advice is welcome.

like image 803
Stéphane Avatar asked Jul 02 '17 16:07

Stéphane


People also ask

How do I add SE bars in R?

Error bars can be added to plots using the arrows() function and changing the arrow head. You can add vertical and horizontal error bars to any plot type. Simply provide the x and y coordinates, and whatever you are using for your error (e.g. standard deviation, standard error).

How do you add individual error bars?

For adding individual error bars in excel, you're going to click on your graph, click on the + for chart elements, and select “More Options” from the list of four additional error bars options. A new dialogue will pop up next, and from here, you must choose the series you want to add error bars to and click ok.

Can Ggplot calculate error bars?

In order to include error bars with ggplot2 plots, you need to add the layer geom_errorbar and specify the ymin and ymax in your aesthethics, either for the plot, or within geom_errorbar.


2 Answers

You don't need more than to add stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge") to your plot:


library(ggplot2)

ggplot(diamonds, aes(cut, price, fill = color)) +
  stat_summary(geom = "bar", fun = mean, position = "dodge") +
  stat_summary(geom = "errorbar", fun.data = mean_se, position = "dodge")

If you prefer to calculate the values beforehand, you could do it like this:

library(tidyverse)
pdata <- diamonds %>% 
  group_by(cut, color) %>% 
  summarise(new = list(mean_se(price))) %>% 
  unnest(new)


pdata %>% 
  ggplot(aes(cut, y = y, fill = color)) +
  geom_col(position = "dodge") +
  geom_errorbar(aes(ymin = ymin, ymax = ymax), position = "dodge")
like image 143
Thomas K Avatar answered Oct 17 '22 07:10

Thomas K


You can add an error bar on your barplot with the geom_errorbar geom.

You need to supply the ymin and ymax, so you need to compute it manually.

From the geom_errorbar help page:

p + geom_errorbar(aes(ymin = lower, ymax = upper), width = 0.2)
like image 34
Colin FAY Avatar answered Oct 17 '22 07:10

Colin FAY