LSMA

LONDON SCHOOL OF MULTIOMICS & AI

Free course

Free course

0 of 25 lessons

Lesson 6 of 9

Module 3 · Figures that tell the truth

Multi-panel figures

Multi-panel figures

Multi-panel figures

Journal figures combine several panels, labelled A, B, C. Building them in code, rather than pasting images together, keeps fonts, sizes and colours consistent and lets you rebuild the figure in seconds.

⏱ 10 min readModule 3 · Figures that tell the truthFree
Lesson at a glance5 steps · about 10 min
  1. Small multiples: facets
  2. Different plots side by side: patchwork in R
  3. What this does, block by block
  4. Reading the figure
  5. Common mistakes

Small multiples: facets

Faceting repeats one plot for each group, on shared axes: facet_wrap(~ supp) in ggplot2.

The ~ (tilde) is read as "by": facet_wrap(~ supp) means "one panel for each value of supp". With the tooth growth data you would get two panels, one for ascorbic acid and one for orange juice, each showing tooth length against dose. These repeated panels are often called small multiples. They work because the reader learns how to read one panel, then reuses that knowledge for every other panel, comparing like with like.

Use facets when every panel shows the same kind of plot for a different subset of the data. When the panels are different kinds of plot, from different data, you need a different tool.

In Figures with ggplot2 you built a plot by adding layers with +; a facet is one more addition of the same kind. Starting from the tooth growth data of Choosing the right chart, ggplot(tg, aes(x = dose, y = len)) + geom_point() + facet_wrap(~ supp) gives the two panels just described. ggplot2 splits the data by supp, draws the same layers in each panel, and writes each group's name in a strip above its panel.

ggplot2 has two faceting functions. facet_wrap() lays a single sequence of panels out in rows, wrapping onto a new row when needed; ncol or nrow sets the shape, so facet_wrap(~ supp, ncol = 1) stacks the two panels one above the other. facet_grid() crosses two variables, rows before the tilde and columns after it: facet_grid(supp ~ dose) makes a grid of 2 rows and 3 columns, one panel for each of the six groups. A dot means "nothing in this direction", as in facet_grid(. ~ supp), which gives one row of panels.

The strips and their order come from the data. Each strip shows a level of the faceting variable, in the order of its factor levels, which is why it helps that supp was turned into a factor with readable labels when tg was made. A strip reading "OJ" or "VC" makes the reader look up a code; a strip reading "Orange juice" does not.

Facets are not always better than colour. When groups share one panel, distinguished by colour, the reader can compare them point by point; when each group gets its own panel, comparison means moving the eye between panels. A useful rule is to put the comparison that matters most within a panel, and the secondary split across panels. If the groups overlap so much that one panel becomes unreadable, facets are the cure. One more caution from Honest axes and scales: keep the scales fixed whenever readers will compare panels.

The term small multiples was popularised by Edward Tufte in his books on statistical graphics, and the same idea appears as trellis displays in Becker, Cleveland & Shyu 1996 (Journal of Computational and Graphical Statistics). Faceting in ggplot2 is described in Wickham's ggplot2: Elegant Graphics for Data Analysis (Springer, 2016). patchwork is written by Thomas Lin Pedersen and distributed on CRAN; cite it with citation("patchwork") and give the version, since its layout options have grown over time. Journals differ on panel letters (bold or not, upper or lower case), so check the author guide rather than relying on the defaults.

Different plots side by side: patchwork in R

The patchwork package combines separate ggplot objects into one figure. You build each panel on its own, store it in a variable, then join them with + (side by side) or / (one above the other). The example joins a tooth growth plot and a birth weight plot, two unrelated datasets, into one two-panel figure.

Why not facets here? Facets need one data frame with a column that names the groups, and every panel shares the same x and y variables. The two panels below come from different data frames, measure different things and have different axes, so there is no column to facet by. patchwork works at the level of finished plots instead: anything you can draw with ggplot2 can become a panel. The birth weight data are those analysed in Risk factors for low birth weight, where lwt is the mother's weight in pounds at her last menstrual period and bwt the baby's weight.

Files for this lessonSave each file in your project folder under the name shown here. Your browser may give the download a different name; rename it before you run the code.
birthweight.csv189 births: birth weight, mother's weight and age, smoking
R
library(patchwork)
bw <- read.csv("birthweight.csv")

pa <- ggplot(tg, aes(x = dose, y = len, colour = supp)) +
  geom_point(position = position_jitterdodge(jitter.width = 0.15, dodge.width = 0.6, seed = 1), alpha = 0.7) +
  scale_colour_manual(values = okabe) +
  labs(x = "Dose (mg/day)", y = "Tooth length", colour = NULL) +
  theme_classic(base_size = 11) + theme(legend.position = "top")

pb <- ggplot(bw, aes(x = lwt, y = bwt, colour = smoke)) +
  geom_point(alpha = 0.6) +
  geom_smooth(method = "lm", se = TRUE) +
  scale_colour_manual(values = c("non-smoker" = "#0072B2", smoker = "#E69F00")) +
  labs(x = "Mother's weight (lb)", y = "Birth weight (g)", colour = NULL) +
  theme_classic(base_size = 11) + theme(legend.position = "top")

fig <- pa + pb + plot_annotation(tag_levels = "A")
ggsave("figure1.png", fig, width = 180, height = 85, units = "mm", dpi = 300)

What this does, block by block

  • library(patchwork) loads the package; read.csv("birthweight.csv") reads the birth weight data into bw. In this file the smoke column already holds the labels "non-smoker" and "smoker".
  • Panel A, pa. Tooth length against dose, coloured by source. position_jitterdodge() does two things at once: it dodges the two sources apart within each dose (dodge.width = 0.6), so ascorbic acid and orange juice sit side by side instead of on top of each other, and it jitters points slightly within each of those columns (jitter.width = 0.15). seed = 1 keeps the jitter the same every run. colour = NULL in labs() removes the legend title, and theme(legend.position = "top") moves the legend above the plot, where it takes less width.
  • Panel B, pb. Each mother is one point: her weight in pounds (lwt) on the x-axis and her baby's weight in grams (bwt) on the y-axis, coloured by smoking. geom_smooth(method = "lm", se = TRUE) fits a straight line by linear regression for each colour group and shades its 95% confidence band. The named vector in scale_colour_manual() fixes which colour each group receives.
  • pa + pb places the panels side by side. plot_annotation(tag_levels = "A") labels them A and B in the top-left corner of each; "a" or "1" would give lower-case letters or numbers.
  • ggsave() writes the figure to figure1.png at 180 × 85 mm and 300 dpi (dots per inch, the number of pixels per inch of printed width).

The + between pa and pb looks like the + that adds layers, but here it joins two complete plots, and it only works once patchwork is loaded. Without library(patchwork), R stops with an error saying it cannot add one plot to another. patchwork also lines up the plotting areas of the panels, so the axes of A and B start at the same height even though their labels differ in length. Pasting two images together cannot do that, which is one reason assembled figures often look slightly crooked.

Panel A: tooth length by dose and vitamin C source. Panel B: birth weight against mother's weight for smokers and non-smokers, each with a fitted line and confidence band.
Drawn by the R code above, at 180 mm wide: a full page width in most journals.

Reading the figure

Both panels share a font, a text size and a theme, because the code gave them the same settings. Panel A shows tooth length rising with dose, with orange juice ahead at the two lower doses. Panel B shows heavier mothers tending to have heavier babies, with a separate fitted line and band for smokers and non-smokers. Each panel keeps its own legend here because the two panels describe different groups.

Notice what is not needed: no copying images into slides, no resizing by hand, no retyping of labels. If a data point is corrected tomorrow, running the script again rebuilds the whole figure, identical in every other respect.

The tags A and B are what the text and the legend refer to. A results sentence such as "heavier mothers had heavier babies (Figure 1B)" sends the reader to one panel without describing where it is. That is why tags go in the same place on every panel, top left, and are read in order, left to right and then top to bottom.

Worked example · figure sizes

single column ≈ 85–90 mm · full width ≈ 170–180 mm (check the journal's guide)
in ggsave give units = "mm"; other tools may need inches: 180 mm ÷ 25.4 = 7.09
design at the final size, so 11-point text stays 11 points when printed

Step by step. One inch is exactly 25.4 mm. Width: 180 ÷ 25.4 = 7.09 inches. Height: 85 ÷ 25.4 = 3.35 inches. At 300 dpi the PNG is therefore about 7.09 × 300 = 2,126 pixels wide and 3.35 × 300 = 1,004 pixels tall.

What the answer means. A point is a printer's unit of text size: 1/72 of an inch, about 0.35 mm. If you design a figure at 360 mm wide and the journal prints it at 180 mm, everything shrinks by half, and 11-point text becomes 5.5-point text, too small to read comfortably. Saving at the printed size from the start avoids that surprise.

Multi-panel figures · LSMA free course A figure 180 millimetres wide and 85 high holds two panels, tagged A and B. Beside it, drawn to the same scale, a single-column figure 85 millimetres wide holds one panel. PLANNING THE FIGURE SIZE 180 mm = 7.09 in 85 mm = 3.35 in ABA Full width · 180 × 85 mm two panels, tagged A and B Single column · 85 mm one panel
Both outlines are drawn to the same scale, so you can see how much less room a single-column figure gives each panel. The panel contents are sketches. Check the exact widths in the journal's author guide.
Go deeper: layouts and shared settings in patchwork

Besides + and /, patchwork has | for side by side, and brackets group panels just as in arithmetic: (pa | pb) / pc puts two panels on top and a third across the full width below. plot_layout(widths = c(2, 1)) makes the first column twice as wide as the second, useful when one panel holds more detail.

The & operator applies a setting to every panel at once, where + would change only the last one. fig & theme(text = element_text(size = 9)) sets 9-point text throughout, and fig & theme(legend.position = "bottom") moves every legend. This keeps panels consistent without repeating the same lines in each plot's code, and a single change at the end restyles the whole figure.

Key point

Use the same colours for the same groups in every panel, align axes that readers will compare, and keep one legend where several panels share it.

A second example: a figure with three panels showing, for the same control and treated mice, body weight, tumour size and survival. Control should be the same colour in all three, the legend can appear once, and if two panels share an x-axis such as "days after treatment", they should share its range too. In patchwork, plot_layout(guides = "collect") gathers identical legends into one; you will see it in the next lesson.

Common mistakes

  • Assembling panels in a slide program. Each pasted image is scaled differently, so text sizes drift from panel to panel, and every change in the data means redoing the layout by hand.
  • Letting software pick colours per panel. ggplot2 assigns default colours by the order of the groups in each plot, so the same group can change colour between panels. Setting colours with a named vector, as both panels do here, prevents this.
  • Forgetting the size until the end. Decide on single column or full width first, then set the text size for that width.
  • Too many panels. A figure of twelve small panels, each with its own axes and legend, leaves little room for the data. Combine panels that share a layout into facets, move detail to supplementary figures, or split the figure in two.
  • Tags that do not match the text. If panels are reordered late, the legend and the results can still refer to the old letters. Generating the tags with plot_annotation() and rereading the legend after every change keeps them in step.

Further reading · Module 3Weissgerber et al. 2015: why bar charts hide the data, with examples. · All reading for this course

Check your understanding

1. What does plot_annotation(tag_levels = "A") do?

2. Why build multi-panel figures in code rather than in a drawing program?

3. A figure 85 mm wide is what in inches?

Up next

Lesson 7

Omics figures

Ask BioTutor

Create a free website with Framer, the website builder loved by startups, designers and agencies.