Introduction

This vignette is Part 3 of 3 for an R workshop created for BIOL 548L, a graduate-level course on data visualization taught at the University of British Columbia.

When the workshop runs, we split students into three groups with successively increasing levels of difficulty. To make sense of what follows, we recommend working through (or at least looking at) Part 1’s vignette and Part 2’s vignette .

All code and contents of this vignette were written together by Vikram B. Baliga, Andrea Gaede and Shreeram Senthivasan.

The endgame: create a multi-panel plot entirely via code

Multi-panel plots can provide an elegant way to tell a visual story that is driven by narrative. One example of this is to craft a figure that (from top to bottom) first shows raw data, then how data are processed and analyzed, and then ultimately delivers a strong punchline.

We will use Figure 3 from Gaede et al. 2017 Current Biology as an example. Although the original version of this figure was made via creating individual plots in R and then stitching them together in Adobe Illustrator, in this vignette we’ll provide code that will create the entire figure.

Here’s the original figure, as seen in the paper:

Figure 3 from Gaede et al. 2017

Figure 3 from Gaede et al. 2017

This study examined how neurons in the lentiformis mesencephali (LM) brain region of three species of birds respond to visual motion across the retina. The species examined were Anna’s hummingbird (Calypte anna), the pigeon (Columba livia), and the zebra finch (Taeniopygia guttata).

This figure shows that in hummingbirds, these neurons respond to faster visual motion speeds than those in zebra finches or pigeons. A raw trace of neural spikes from a zebra finch is shown in panel A. Panels B, C, and D show how the raw traces can be analyzed to determine the firing rate of neurons, and how these firing rates vary with the speed of the image the bird is seeing. Ultimately, E and F characterize the distribution of firing rates for all three species and show that hummingbirds’ LM neurons respond more to higher visual speeds than the LMs of the other species.

Objectives

The specific learning objectives for this post are:

  1. Plan a sequence of plots that support a declarative statement of a result
  2. Construct a multi-panel figure using cowplot
  3. Evaluate which among a diversity of plotting options most effectively communicates a logical argument

Package loading and setup

Load or install&load packages

We’ll first load packages that will be necessary. The package.check() function below is designed to first see if each package is already installed. If any aren’t, the function then installs them from CRAN. Then all the packages are loaded.

The code block is modified from this blog post

Data sets

You can get all data used in this vignette (and the other two vignettes!) by downloading this zip file.

First, create and save panels as separate ggplot objects

We will first create each panel individually. To save space, we’ll hide the code that generates each plot. To see the code for each plot that follows, please see Part 2’s vignette.

Figure 3 Panel A:

Figure 3 Panel B:

Figure 3 Panel C:

Figure 3 Panel D:

Figure 3 Panel E:

Figure 3 Panel F:

A quick intro to cowplot

We’ll go over cowplot::plot_grid() basics:

  • Multiple panels can be combined via cowplot::plot_grid()
  • Each panel should be saved as a separate ggplot object beforehand and then each will be fed in as an argument to plot_grid()
  • Panels can be annotated and labeled
  • draw_image() & draw_grob() can also be used with ggplot() and plot_grid()

Plot objects can be added sequentially

By default, additional panels are initially added within the same row.
plot_grid() then tends to prefer to have ncol = nrow as best as possible. I’ll create an example by pasting fig_B over and over again. Each panel will be numbered so you can track how plots are added sequentially within plot_grid().

This looks okay, but we may prefer to have the plots arranged in one column.
Let’s first just focus on panels E and F.

Cowplot also allows you to add labels to panels as you’d see in a journal article.

See the arguments of cowplot::plot_grid() to see how labels can be adjusted. We’ll adjust the size and (temporarily) change to a serif font.

This looks horrible, but we’re using extreme values here to emphasize what is being adjusted.

Now start assemble sets of panels for each section of Fig 3

The layout of the figure is complex; nrows and ncols can’t be set easily. Luckily we can build sets of panels together, save each one, then stitch it all together at the end.

Let’s start with panels E and F.

For panel A, we’ll adjust the padding around the margin.
Within the argument plot.margin, the order is top, right, bottom, left; think TRouBLe (T,R,B,L)

Panels B and C
When combining B and C, we should note that C has an x-axis label but B does not. Therefore, we will add blank plots (NULL) as padding and then adjust the relative heights to fit things comfortably. We still need to supply 4 labels, so "" will be used for blank plots.

One neat trick is that using a negative number within rel_heights reduces space between plots (or elements). We can use this to reduce the gap between panels if we don’t like how cowplot handles things by default.

Panel D
Similarly, with panel D, we’ll add a NULL object to the cowplot and adjust the relative heights to the proportions we’d like.

You can cowplot multiple cowplots

Combine all the sets of panels together

It may help to specify the dimensions of the plot window to ensure that the plot is made with correct overall proportions.

try(dev.off(), silent = T)
dev.new(width = 8.5, height = 11, units = "in")

For Mac users, qartz() seems to be the way to go. E.g.: quartz(width = 8.5, height = 11)

Now for the plot itself:

The final step: Add icons to the multi-panel plot

The bird heads and other legend elements are conspicuously absent. The cowplot::draw_image() function allows for an image to be placed on the canvas, which we’ll use for the bird heads.

Similarly, cowplot::draw_grob() places grobs (GRaphical OBjects). We’ll use this to add in colored rectangles for the legends.

Each image’s (or grob’s) location is specified with x- and y- coordinates. Each runs from 0 to 1, with (0,0) being the lower left corner of the canvas.

At the moment, vectorization of the code you see below doesn’t seem possible. It’s a bit tedious (ok, VERY tedious. And un-tidy!) but it works!

And there you have it! We have recreated the entire figure – including annotations, icons, and inserted images – all through code.

Thanks for reading!

🐢

Appendix: saving plots; using photos & maps

A1: Saving multi-panel plots

cowplot::save_plot() tends to work better with multi-panel figures than ggsave() does. You can specify size (in inches) via the arguments base_height and base_width within save_plot().

For example: save_plot("Gaedeetal_Fig3.svg", cow_final, base_height = 11, base_width = 8.5)

A2: Using photographs with ggplot/cowplot

Often, you may opt to add a photograph as its own panel.

Here’s a quick example:

Now create a ggplot object that features only the image.

Now cowplot it!

A3: Using maps with ggplot/cowplot

Adding in maps can be useful to showing geographic trends. The maps package allows for convenient plotting of (annotated) maps with ggplot().

Quick example. Load the ‘world’ map:

If you’d, for whatever reason, like to highlight certain countries:

This map can be added to a cowplot rather easily:

🐢