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

other entires to explore:

global history of architecture
08 june 2013 | architecture

One of my favorite classes at MIT was 4.605 (Global History of Architecture), which explored various styles and themes found throughout dif[...]ferent buildings and other types of architecture from the beginning of civilization to the present. In the spirit of sharing my enthusiasm for the course i'll discuss some takeaways from, and have included a couple papers i wrote for, the class.

week 5 and 6 | once again
21 july 2012 | singapore

Sometimes it feels like life should have a record button that nicely parses the endless stream of awesomeness that comes at you. Not a came[...]corder or microphone, but a program that understands the world, can automatically find and retrieve the most useful links then type-up a nicely formatted document to present to others. Alas, while some of this technology exist, it isn't in one nice little package. Plus, writing about Singapore is almost as fun as experiencing it. Below are some finds from my fifth and sixth week in Singapore, from adventures in Bali to savouring the taste of Ayam Goreng Pedas Ramen (super spicy!).

sochi olympic stats: medal count
19 february 2014 | america

There have been several articles that re-order the medal count ranking by comparing medals to population or GDP, which then show small coun[...]tries topping the charts. This analysis ignores some obvious facts: small countries are over-represented in the number of athletes they send and the relationship between athletes sent and medals is linear. I present a brief analysis to support and expand on these claims.

©2006-2024 | Site created & coded by Biafra Ahanonu | Updated 17 April 2024
biafra ahanonu