In this vignette of patchwork is explained how to combine multiple ggplots. One difficulty I encountered is to collect the legends and align/justify them properly when their titles are very different in number of characters.
Below is an example - I would like the 'mpg' legend to be also left justified / aligned and not centered beneath the 'Size' legend. Any suggestions? Note that, adding theme(legend.justification = "left")
doesn't solve the problem.
library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp, colour = mpg, size = wt)) +
guides(size = guide_legend(title = "Size - long title for the purpose of this example")) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('Plot 2')
p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
(p1 | (p2 / p3)) + plot_layout(guides = 'collect')
Created on 2019-12-16 by the reprex package (v0.3.0)