How to share a legend for combined ggplot2 plots?

library(ggplot2)
p1 <- ggplot(mpg) + geom_point(aes(x = class, y = hwy, color = drv))
p2 <- ggplot(mpg) + geom_boxplot(aes(x = class, y = hwy, fill = drv))
p3 <- ggplot(mpg) + geom_bar(aes(class, fill = drv))

patchwork

library(patchwork)
(p1 + p2) / p3 + plot_layout(guides = "collect")

plot of chunk patchwork12.3

cowplot

library(cowplot)
pw <- plot_grid(
    plot_grid(p1 + theme(legend.position="none"),
              p2 + theme(legend.position="none"), ncol = 2),
    p3 + theme(legend.position="none"), ncol = 1)
legend_b <- get_legend(p1 + theme(legend.position="bottom"))
plot_grid(pw, legend_b, ncol = 1, rel_heights = c(1, .1))

plot of chunk cowplot12.3

ggpubr

library(ggpubr)
ggarrange(plot_grid(p1 + theme(legend.position="none"),
                    p2 + theme(legend.position="none")),
          p3, ncol = 1, common.legend = TRUE, legend = "bottom")

plot of chunk ggpubr12.3

Ref: https://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots