When combining multiple plots with patchwork, legends often need special handling:
ggguides provides collect_legends() and
collect_axes() to address these challenges.
library(patchwork)
p1 <- ggplot(mtcars, aes(mpg, wt, color = factor(cyl))) +
geom_point() + labs(title = "Plot 1", color = "Cylinders")
p2 <- ggplot(mtcars, aes(mpg, hp, color = factor(cyl))) +
geom_point() + labs(title = "Plot 2", color = "Cylinders")
# Default patchwork: duplicate legends
p1 | p2Use collect_legends() to gather legends from all plots
in a composition:
For vertically stacked plots, legends can be centered or span the full height. Using different plot heights makes the spanning behavior more visible.
# Create plots with different heights using plot_layout
p3 <- ggplot(mtcars, aes(mpg, disp, color = factor(cyl))) +
geom_point() + labs(title = "Plot 3", color = "Cylinders")
# Stack with different heights: 1, 1/2, 1/4
stacked <- (p1 / p2 / p3) + plot_layout(heights = c(4, 2, 1))
collect_legends(stacked, position = "right")Use span = TRUE to make the legend fill the full
height:
Attach the legend to specific row(s) instead of spanning all:
# Legend attached to row 1 only (the tallest plot)
gt <- collect_legends(stacked, position = "right", span = 1)
grid::grid.draw(gt)# Legend attached to rows 1 and 2
gt <- collect_legends(stacked, position = "right", span = 1:2)
grid::grid.draw(gt)When stacking plots vertically, the x-axis may be duplicated. Use
collect_axes() to remove redundant axes:
# Plots stacked vertically - x-axis is duplicated
p_top <- ggplot(mtcars, aes(mpg, wt)) +
geom_point() + labs(y = "Weight")
p_bottom <- ggplot(mtcars, aes(mpg, disp)) +
geom_point() + labs(y = "Displacement")
# Without axis collection (both have x-axis)
p_top / p_bottomggguides functions work together:
p1_styled <- p1 + legend_style(size = 11, title_face = "bold")
p2_styled <- p2 + legend_style(size = 11, title_face = "bold")
collect_legends(p1_styled | p2_styled, position = "right")Handle more complex patchwork layouts:
p4 <- ggplot(mtcars, aes(qsec, wt, color = factor(cyl))) +
geom_point() + labs(title = "Plot 4", color = "Cylinders")
# 2x2 grid
layout <- (p1 | p2) / (p3 | p4)
collect_legends(layout, position = "right")ggguides provides cowplot-compatible functions that work without patchwork:
| Function | Purpose | Key Parameters |
|---|---|---|
collect_legends() |
Gather legends from patchwork | position, span |
collect_axes() |
Remove duplicate axes | guides |
get_legend() |
Extract legend as grob | - |
shared_legend() |
Combine plots with shared legend | ncol, nrow, position |
Learn more: