Export aligned ggplot2 panels at final size

How do I align two ggplot2 panels and export them at 89 mm?

Align the two ggplot2 panels with patchwork, then export with ggsave at 89 × 60 mm for PDF or 1,052 × 709 pixels for a 300 DPI PNG.

On this page

How do I check the exported figure?

Open the PDF at its final size and check the labels, tick marks and axis titles. For raster delivery, check the exported PNG’s actual pixel dimensions. To guarantee the rounded-up pixel dimensions in this example, the PNG export uses pixel units.

Two aligned scatterplots showing fuel economy versus weight and horsepower in the mtcars data.
Output of the example below using R’s built-in mtcars data.

I draw two scatterplots from R’s built-in mtcars data. The layout uses patchwork. Install ggplot2 and patchwork once if either package is missing. Run the script from the folder where you want the two files saved.

Runnable R example

library(ggplot2)
library(patchwork)

theme_figure <- theme_classic(base_size = 7) +
  theme(plot.tag = element_text(face = "bold"))

panel_a <- ggplot(mtcars, aes(wt, mpg)) +
  geom_point(size = 1.2, colour = "#17665c") +
  labs(x = "Weight (1,000 lb)", y = "Miles per gallon") +
  theme_figure

panel_b <- ggplot(mtcars, aes(hp, mpg)) +
  geom_point(size = 1.2, colour = "#17665c") +
  labs(x = "Horsepower", y = "Miles per gallon") +
  theme_figure

figure <- (panel_a | panel_b) + plot_annotation(tag_levels = "A")
ggsave("figure.pdf", figure, width = 89, height = 60, units = "mm")
ggsave("figure.png", figure, width = 1052, height = 709, units = "px", dpi = 300)

Sources

Figure size and DPI calculator