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")Use | to place plots beside each other, /
to stack them, and parentheses to nest layouts.
plot_layout() sets the number of columns/rows and the
relative sizes.
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 | sctrggpubr::ggarrange() for the built-in arrangement
helper.