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

comments

comments powered by Disqus

more articles to enjoy:

automatic python playlist creator
16 november 2012 | programming

Now that I've gotten back in the python groove somewhat, wanted to make a playlist script that recursively went through a folder, found all[...] files in a sub-folder and create a playlist from them. Implementation described and link to github repository.

los ambiciones equivocados
05 may 2010 | short story | spanish

Mis amigos y yo fuimos al bar cuyo nombre es “Club Dos”, es muy famoso en esta parte de la gran ciudad. Sus ciudadanos le [...]ponen algunos nombres a la ciudad, como “La Ciudad sin Tierra” o “La Ciudad de Edificios”. Para que tengas entender más sobre lo que ocurrirá, es necesario que yo les explique lo que es la ciudad.

Next Spanish short, it focuses on a magnificent city and its origin story. It is something of a cautionary tale, made all the more real by my experiences in Singapore.

filugori reboot
15 may 2012 | filugori

Several months ago I took a hard look at Filugori: The Long Tale, the story I started in grade school that was meant to be a mash-[...]up of my favorite books and fictional universes. However, it lacked a certain vision. The story was fun, frantic and fanciful, but there was no heart. It lacked cohesion and the universe did not appear to justify its own existence. Why should someone care to read this tale? What would they gain from it? While fleshing out the background of the universe, providing details on the four major epochs that define the story, I came to realize that I wanted to tell a very different tale than originally planned.

powerpoint themes
10 august 2012 | design

Many default PowerPoint themes are too outlandish and graphics heavy for my taste. So over the last year or so, I have begun designing my o[...]wn themes for PowerPoint. I'll go over the design decisions behind a couple and provide links to download the themes.

©2006-2017 | biafra ahanonu | updated 19 june 2017
biafra ahanonu