I am using patchwork to create a multi-plot figure in R. They share one legend, so I am including the legend at the top right panel using ggpubr::get_legend() (note: I cannot use patchwork::plot_layout() because the legends have different underlying values).
Base plot
library(ggplot2)
library(patchwork)
library(ggpubr)
p <- ggplot(data = mtcars %>% mutate(cyl = as.factor(cyl)),
mapping = aes(x = wt, y = mpg, group = cyl, color = cyl)) +
geom_smooth(method = "lm")
When there are 6 of the same plots on the top and bottom rows, the plots align:
(p | p | p | p | p | p) /
(p | p | p | p | p | p) &
theme(legend.position = "none")
When the 5th plot on the top row is a legend, they do not align:
leg <- as_ggplot(get_legend(p))
(p | p | p | p | p | leg) /
(p | p | p | p | p | p) &
theme(legend.position = "none")


