A Journey in Making Plots in R with ggplot2

I use the language R for all of my statistical analyses and also for making figures. This is a little example of what goes into making a nice looking presentation of data in R. Making figures in R is a mostly fun, sometime frustrating, but ultimately rewarding. It is neat to code out a bonkers mass of characters and then out comes a beautiful figure.  I just started using a fancy package, ggplot2. With ggplots you can create “themes” which change the look of the figures. So here is the code for a figure using the default themes and the resulting figure:

plot = ggplot(nn, aes(x=year, y=MPD, colour=fencing)) + geom_errorbar(aes(ymin= MPD-se, ymax=MPD+se), width=.1) + geom_line() + geom_point()
plot

I didn’t much like the colors, the background with grid lines and the small axes labels. But everything can be changed, it just takes a lot of time to figure out the right code to make the changes you want. So here is the code for my improved figure:

library(grid)

plot= ggplot(nn90, aes(x=year, y=MPD)) + geom_errorbar(aes(ymin=MPD-se, ymax=MPD+se), width=.05) + geom_line(size=1,aes(linetype= fencing,color=fencing)) + geom_point(size=3,aes(shape=fencing))

ng1 = theme(panel.background = element_rect(fill = “white”,colour = “white”), panel.grid.major = element_line(colour = NA), axis.line = element_line(size = 1.2, colour=”black”), axis.ticks=element_line(color=”black”), axis.text=element_text(color=”black”,size=15), axis.title=element_text(color=”black”,size=20), panel.grid.minor = element_line(colour = NA), legend.position = “top”, legend.direction=”horizontal”, legend.text = element_text(size=20), legend.key = element_rect(fill = “white”), legend.title = element_blank(),legend.key.size = unit(1.5, “cm”))

plot + scale_x_continuous(breaks =(seq(min(nn90$year),max(nn90$year),by=2))) + scale_colour_manual(values= c(“black”,”black”))+ xlab(“Year”) +ylab(“Mean Parwise Distance ± SE”) + ng1

I think it looks a lot nicer. I won’t go into trying to explain what all the different bits of the code do, but the part that is “ng1=theme(…” is the theme. That is the main part that is responsible for the change in appearance. For any ggplot users feel free to use the theme.

So that is what a day of fiddling around in R will get you. Now I am one step closer to presenting these results at a conference next week. (I will explain what these results actually mean later :)

One thought on “A Journey in Making Plots in R with ggplot2

  1. Thanks for sharing. I like certain features of ggplot2. However, the charts are not simple nor beautiful and the process of creating simple, yet beautiful and informative charts becomes painful. gnuplot is an excellent alternative for the chart you posted here.

    Using the mock data set and the code below, created a chart similar to the one above. As you can see the code is simple and logical, and the resulting chart is nice.

    #Year #fenced #sefenced #rabbits #serabbits
    1992 -1.2 0.1 NaN 0.1
    1993 -1.5 0.1 -0.8 0.1
    1994 -1.3 0.1 -1.3 0.1
    1995 -1.7 0.1 -1.1 0.1
    1996 -2 0.1 -1.2 0.1
    1997 -1.8 0.1 -1 0.1
    1998 NaN 0.1 NaN 0.1
    1999 NaN 0.1 NaN 0.1
    2000 -2.8 0.1 -2 0.1
    2001 NaN 0.1 NaN 0.1
    2002 NaN 0.1 NaN 0.1
    2003 NaN 0.1 NaN 0.1
    2004 -2.6 0.1 -1.3 0.1
    2005 -2.1 0.1 -1.5 0.1
    2006 -2.5 0.1 -1.4 0.1
    2007 NaN 0.1 NaN 0.1
    2008 -0.8 0.1 0 0.1
    2009 NaN 0.1 NaN 0.1
    2010 NaN 0.1 NaN 0.1
    2011 NaN 0.1 NaN 0.1
    2012 -1.6 0.1 -0.5 0.1

    set datafile missing “NaN”
    set xrange [1991:2013]
    set key center horizontal top
    set xtics 2 nomirror
    set xlabel “Year”
    set ylabel “Mean Parwise Distance {/Symbol \261} SE”
    set yrange [-3:0.5]
    set ytics 1 nomirror
    set border 3
    set bars small
    plot “rabbits.dat” u 1:2:3 w yerrorlines t “Fenced”, “” u 1:4:5 w yerrorlines t “Rabbits”
    set term postscript eps enhanced
    set output “rabits.eps”
    replot
    set term windows
    set output

Leave a Reply