r functions: ggplot barplot with errorbars

Summary

The best way to create a barplot with errorbars using ggplot in R is not obvious. I'll show a super simple solution.

example R plot made using the below function and built-in data.frame

The best way to create a barplot with errorbars using ggplot in R is not obvious. Below is a super simple solution. As it is self-explanatory, this will be my shortest entry ever. cheers!

update 2013.12.10: the code was modified slightly by adding position_dodge() to make the error lines be smaller than the bars and the theme has been cleaned up to remove the default gray background.

update 2013.12.28: wrapped the script into a function (to generalize it)—pass data as a data.frame and x/y/fill as column name strings.

download ggplot_barplot.R

R / S+
  1. ggplotErrorBarplot <-function(data,x,y,fill,...){
  2.         # makes a barplot with errorbars using ggplot, variable inputs are strings
  3.         # biafra ahanonu, updated: 2013.12.28
  4.  
  5.         # load ggplot
  6.         require(ggplot2)
  7.  
  8.         # create functions to get the lower and upper bounds of the error bars
  9.         stderr <- function(x){sqrt(var(x,na.rm=TRUE)/length(na.omit(x)))}
  10.         lowsd <- function(x){return(mean(x)-stderr(x))}
  11.         highsd <- function(x){return(mean(x)+stderr(x))}
  12.  
  13.         # create a ggplot
  14.         ggplot(data,aes_string(x=x,y=y,fill=fill))+
  15.         # first layer is barplot with means
  16.         stat_summary(fun.y=mean, geom="bar", position=position_dodge(), colour='white')+
  17.         # second layer overlays the error bars using the functions defined above
  18.         stat_summary(fun.y=mean, fun.ymin=lowsd, fun.ymax=highsd, geom="errorbar", position=position_dodge(.9),color = 'black', size=.5, width=0.2)+
  19.         theme(line = element_blank(),panel.background = element_rect(fill = "white", colour = NA))
  20. }
  21.  

-biafra
bahanonu [at] alum.mit.edu

additional articles to journey through:

bash scripting: playlist maker
23 march 2013 | programming

Decided to take another stab at converting small programs to bash. This time, the python playlist maker was updated to bash and greatly sim[...]plified, no longer need to do sketchy recursions.

from the archives: declaration of independence, internet edition
11 july 2013 | america

A revised Declaration of Independence I did awhile ago (i.e. high school) for a writing class. It is slightly a mockery of the style of wri[...]ting sometimes used back then, e.g. finding unnecessarily complicated ways of saying a simple concept; long, ponderous sentences; and an abuse of the Capital.

¿qué es la calle?
24 may 2013 | short story | spanish

Había terminado y salé para mi cocina. Tenía hambre pero este día no había comida dentro de mi despensa. Me fui y caminé hacienda[...] la Tport—una máquina que puede transportar una persona a otro lugar sin energía y tiempo. Cuando entré la máquina, toqué algunos botónes y esperé. Pero nada ocurrió y lo hice las mismas acciones otra vez—y nada ocurrió.

How would the disappearance of streets affect the social fabric? This short story briefly (in castellano!) explores a world in which instantaneous, free transport is possible. Meant mainly to practice my spanish, i plan to follow-up with a more detail story in the future.

state of sbsa: a review of 2017 and thoughts on future directions
04 june 2017 | sbsa

I spent the past year leading the Stanford Biosciences Student Association (SBSA) as President. This post consist of the letter to the comm[...]unity I sent out at the end of my term giving some highlights of the past year, those who have helped out, and thoughts on future directions.

©2006-2024 | Site created & coded by Biafra Ahanonu | Updated 21 October 2024
biafra ahanonu