Combining ggpubr Plots with patchwork

Every ggpubr function returns a standard ggplot object. That means you can lay several ggpubr plots out together with patchwork, whose +, | and / operators compose plots with almost no boilerplate. patchwork complements the built-in ggpubr::ggarrange(), and is handy when you want shared legends, nested layouts, or automatic panel tags.

library(ggpubr)
library(patchwork)

# A few publication-ready ggpubr plots to arrange
data("ToothGrowth")
tg <- ToothGrowth
tg$dose <- as.factor(tg$dose)

bxp <- ggboxplot(tg, x = "dose", y = "len", fill = "dose", palette = "jco",
  add = "jitter") +
  stat_compare_means(method = "anova")

sctr <- ggscatter(mtcars, x = "wt", y = "mpg",
  add = "reg.line", conf.int = TRUE,
  cor.coef = TRUE, cor.method = "pearson")

dens <- ggdensity(tg, x = "len", fill = "dose", palette = "jco")

Side by side, stacked, and nested

Use | to place plots beside each other, / to stack them, and parentheses to nest layouts.

bxp | sctr

(bxp | sctr) / dens

Controlling the layout

plot_layout() sets the number of columns/rows and the relative sizes.

(bxp | sctr) + plot_layout(widths = c(1, 1.4))

One shared legend

When panels carry the same legend, plot_layout(guides = "collect") gathers those identical legends into a single one. The & operator then applies a theme element to every panel at once - here to move the shared legend to the bottom.

oj <- ggboxplot(subset(tg, supp == "OJ"), x = "dose", y = "len",
  fill = "dose", palette = "jco", add = "jitter") +
  labs(title = "Orange juice")

vc <- ggboxplot(subset(tg, supp == "VC"), x = "dose", y = "len",
  fill = "dose", palette = "jco", add = "jitter") +
  labs(title = "Vitamin C")

(oj | vc) +
  plot_layout(guides = "collect") &
  theme(legend.position = "bottom")

Titles and panel tags

plot_annotation() adds a figure title and automatic panel tags (A, B, C …), the layout most journals expect for a multi-panel figure.

(bxp | sctr) / dens +
  plot_annotation(
    title = "Tooth growth and fuel economy",
    tag_levels = "A"
  )

Correlation matrices

ggpubr does not draw correlation matrices; for that, use ggcorrplot, which also returns a ggplot and so composes with patchwork in exactly the same way.

library(ggcorrplot)

corr <- round(cor(mtcars[, c("mpg", "disp", "hp", "drat", "wt", "qsec")]), 2)
cmat <- ggcorrplot(corr, hc.order = TRUE, type = "lower",
  lab = TRUE, colors = c("#6D9EC1", "white", "#E46726"))

cmat | sctr

See also

  • ggpubr::ggarrange() for the built-in arrangement helper.
  • patchwork for the full composition API.
  • ggcorrplot for correlation matrices.