GGtutorial: Day 2 - grid.arrange

Yesterday we worked on casting and melting with the reshape() package. Today, let’s continue with that to use a custom function before getting into colors, fills and grid.arrange (like mfrow, but compatible with ggplot). We can use a function we define to extract standard errors of estimates, and then use them to construct a matrix of 95% confidence intervals for each of our estimates of trust over year and partisanship (here it’s just a dummy variable for whether someone is a Democrat, 0s are Republicans, Independents are dropped).  While I understand some of this can be done using the plyr() package, that’s for another day…

This is the output:

So, what if you were thinking about changing the geom of that plot from “linerange” to “errorbar,” and fiddling around with the options? Which might look better (or less bad)? One way to compare multiple plots side by side is to use the grid.arrange() function from within the gridExtra package. As it is similar to the mfrow() option, you see I label my arranged grid MFrowGG.

The syntax for this function could not be easier. To arrange N plots in an M column grid, this is the syntax:

grid.arrange(Plot1, Plot2, … , PlotN, ncol=M) 

This is the output:

So, if you’ve ever been trying to find a way to use R’s multiple figures per row (mfrow) option with ggplot, this is an easy function to use.