# Plotting error bars with R

Updated 2018 May 20th

Error bars may show confidence intervals, standard errors, and standard deviations. Each feature conveys a different message and this paper on error bars in experimental biology explains it very nicely. For this post I will demonstrate how to plot error bars that show the standard error (SE) or standard error of the mean (SEM). I found two nice resources that demonstrate the plotting of error bars with R and in this post I illustrate them with simple examples. The first method is from the website of James Holland Jones, where he wrote an R function that plots arrows to a bar plot.

# generate some random numbers
set.seed(31)
a <- runif(10, 0, 10)
b <- runif(10, 0, 10)
c <- runif(10, 0, 10)

# store them as a data.frame
df <- data.frame(a=a, b=b, c=c)

# function for error bars
error.bar <- function(x, y, upper, lower=upper, length=0.1,...){
if(length(x) != length(y) | length(y) !=length(lower) | length(lower) != length(upper))
stop("vectors must be same length")
arrows(x,y+upper, x, y-lower, angle=90, code=3, length=length, ...)
}

# function for standard error of the mean
sem <- function(x){
sd(x)/sqrt(length(x))
}

# calculate means
my_mean <- apply(df, 2, mean)

# calculate sem
my_sem <- apply(df, 2, sem)

# barplot
barx <- barplot(my_mean,
names.arg=names(df),
ylim=c(0,ceiling(max(df))),
xlab='Class',
ylab='Unit of measure')
error.bar(barx, my_mean, my_sem)


The second resource I found on plotting error bars with R was from the Cookbook for R, which showed many examples using the R package ggplot2. Here is a simple example I adapted from their cookbook, using the same set of random numbers I generated above:

# install if necessary
install.packages('ggplot2')

library(ggplot2)

set.seed(31)
a <- runif(10, 0, 10)
b <- runif(10, 0, 10)
c <- runif(10, 0, 10)
df <- data.frame(a=a, b=b, c=c)

sem <- function(x){
sd(x)/sqrt(length(x))
}

my_mean <- apply(df, 2, mean)
my_sem <- apply(df, 2, sem)

# new data frame for storing the mean and sem
mean_sem <- data.frame(mean=my_mean, sem=my_sem, group=names(df))

# larger font
theme_set(theme_gray(base_size = 20))

# plot using ggplot
ggplot(mean_sem, aes(x=group, y=mean)) +
geom_bar(stat='identity') +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem),
width=.2) +
xlab('Class') +
ylab('Unit of measure')


I had been using the first approach for plotting error bars with R but I think the ggplot2 plot looks much better (and with less effort).

Lastly, it has been over a month since my last post, though I have been updating old posts. I've been spending time writing my thesis and papers but I've also been preparing for a bigger-than-usual post, which I hope will be interesting.

## Update 2018 May 20th

Here's a third approach using ggplot2 and the stat_summary layer.

library(reshape2)
library(ggplot2)

set.seed(31)
a <- runif(10, 0, 10)
b <- runif(10, 0, 10)
c <- runif(10, 0, 10)
df <- data.frame(a=a, b=b, c=c)
df_melted <- melt(df)

ggplot(df_melted, aes(x = variable, y = value)) +
stat_summary(fun.y = mean, geom = "bar") +
stat_summary(fun.data = mean_se, fun.args = list(mult = 1), geom = "errorbar", width = 0.2)


.