Publication quality plots with cowplot

I often turn to the ggplot2 R package for creating statistical plots. (Read Hadley's excellent tutorial on how to build a plot with ggplot2). ggplot2 is an implementation of the grammar of graphics, a concise approach for describing the components of a graphic.

It often takes extra effort to arrange ggplot2 objects on a grid. Sometimes the code may look like a hack or unpolished. This is partly due to the fact that R does not offer very clean syntax and therefore code can be difficult to read and understand. Here is a snippet of code that I was using to print four ggplot2 objects on a 2 x 2 grid.

#new grid
  #a 2 rows and 2 columns viewpoint
  pushViewport(viewport(layout = grid.layout(2,2)))

  #a function to render each viewpoint
  vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)

  #print each plot in its respective area/grid
  print(plot1, vp = vplayout(1, 1))
  print(plot2, vp = vplayout(1, 2))
  print(plot3, vp = vplayout(2, 1))
  print(plot4, vp = vplayout(2, 2))

Using the recent cowplot R package, the seven lines of code are reduced to a single line! This may not seem a lot, but for the sake of brevity, beauty and intent, I think it is a big deal.


The above code is more legible than the previous code. The code also places labels against each plot. This is a winner feature for publication quality graphics. Many journals and reviewers expect well labelled graphics. Cowplot offers several other features.

Liked this post? Share it with your followers. and Follow me on Twitter!