The more I use it, the deeper I fall in love with ggplot2. I know, some of you have heard me kvel about it ad nauseum (oh, yiddish and latin in one sentence – extra points!). But the graphs really look great, and once you wrap your head around a few concepts, it’s surprisingly easy to make it do most anything you want.

Except for one thing.

One thing I loved about the old R plotting functions was the ability to setup panels easily, and fill them with totally different graphs. Ye olde par(mfrow=c(2,2)) for a 2 x 2 grid, for example.

What exactly do I mean? Let’s say I’m working with the soil chemistry data in the vegan package. First, maybe I just want to eyeball the historgrams of both the hummus depth and bare soil columns.

To do this in ggplot2, and with a single commend to put them in a single window, first you need to melt the data with reshape2 so that the column names are actually grouping variables, and then you can plot it. In the process, you create an additional data frame. And, you also have to do some extra specifying of scales, facets, etc. etc. Here’s the code and graphs.

This produces a nice graph. But, man, I had to think about reshaping things, and all of those scales? What if I just wanted to make two historgrams, and slam 'em together. This is where gridExtra is really nice. Through its function grid.arrange, you can make a multi-paneled graph using ggplot2 plots, lattice plots, and more (although, not regular R plots...I think).

So, let's see the same example, but with gridExtra.

library(gridExtra)
#make two separate ggplot2 objects
humDist

"Oh, what a trivial problem," you may now be saying. But, if you want to, say, plot up 5 different correlations, or, say, the same scatterplot with 4 different model fits, this is a life-saver - if nothing else, in terms of readability of your code for later use.

This is all well and good, but, simple. Let's get into more fun multi-panel figures. Let's say we wanted a bivariate scatter-plot of Hummus Depth and Bare Soil with a linear fit. But, we also wanted to plot the histograms of each variable in adjacent panels. Oh, and flip the histogram of whatever is on the y-axis. Sexy, no? This is pretty straightforward. We can use the ggplot2 objects we already have, flip the co-ordinates on one, create a bivariate plot with a fit, and fill in one final panel with something blank.

#First, the correlation. I'm using size just to make bigger points. And then I'll add a smoothed fit.
corPlot

Nice. Note the use of the grid.rect. gridExtra is loaded with all sorts of interesting ways to place shapes and other objects into your plots - including my favorite - grid.table, for when you don't want to deal with text.

a

Or, heck, if you want to make that part of the above plot, use tableGrob instead of grid.table, and then slot it in where the blank panel is. The possibilities are pretty endless!

UPDATE: Be sure to see Karthik's comment below about alternatively using viewports. Quite flexible, and very nice, if a hair more complex.

WARNING: This blog entry contains me awkwardly groping with math. It’s not pretty. It’s not done elegantly – indeed, for problems of even moderate complexity I fired up maxima (which is totally awesome!) rather than screw up the algebra on paper. And there are a few leaps that I make that I’m sure someone could write a proof for, but, well… While I fall somewhere in the middle of the theoretical – experimental axis of scientists, that doesn’t mean it’s something I do every day, so, expect some turbulence. I welcome comments and suggestions.

And, indeed, despite my lit searching, I’m not entirely convinced that someone hasn’t done this before, so, I may be re-inventing a very old wheel. But I thought it might be interesting to post these thoughts, if only for my own processing of recent research results.

I also admit, showing some (clumsy) mathematical thoughts publically makes me feel, well, like I’m not wearing any pants. Oh well. Onwards! With or without pants!

This led me to think more about diversity effects, and why are they saturating, anyway? Should they be? It’s not Kyle’s original question, but, it’s an interesting one and leads down similar theoretical pathways (I think).

So I decided to go back to basic competition theory – the Lotka-Volterra competition equations. Continue reading →