align_plots()
Published

August 7, 2025

Plot Composer Basics

library(ggalign)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'ggalign'
#> The following object is masked from 'package:ggplot2':
#> 
#>     element_polygon
library(ggplot2)
p1 <- ggplot(mtcars) + 
  geom_point(aes(mpg, disp)) + 
  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')

p4 <- ggplot(mtcars) + 
  geom_bar(aes(gear)) + 
  facet_wrap(~cyl) + 
  ggtitle('Plot 4')

Combine plots directly:

align_plots(p1, p2)

By row:

align_plots(p1, p2, ncol = 1)

Align a list of plots:

align_plots(!!!list(p1, p2, p3))

Set different width for plots:

align_plots(p1, p2, widths = c(2, 1))

Works for multiple plots:

align_plots(p1, p2, p3, p4, ncol = 2, widths = c(2, 1))

Set space:

align_plots(p1, NULL, p2)

More see Data-Free Composition of ggalign tutorial book.