rggplot2

Make every subplot the same output size across multiple plots in ggplot


Imagine we have a list of multiple plots. I would like to have each individual plot have the same output size. For example, we say the width is 5 and height is 4, this should be the output size for each plot, also the plots inside a facet. Here is some example code:

library(ggplot2)

p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
p2 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width)) + geom_point() + facet_wrap(~Species, scales = "free", ncol = 1)
p3 <- ggplot(mtcars, aes(x = mpg, y = cyl)) + geom_point() + facet_wrap(~vs, scales = "free", ncol = 1)

l <- list(p1, p2, p3)
l
#> [[1]]

#> [[2]]

#> [[3]]

Created on 2025-05-26 with reprex v2.1.0

Now each graph has a different output size. So I was wondering if it is possible to get the same output size for each individual plot of a list?


Solution

  • You could use the ggh4x package with force_panelsizes:

    library(ggh4x)
    lapply(l, \(p) p + force_panelsizes(cols=unit(5, "cm"),  # widths
                                        rows=unit(4, "cm"))) # heights
    

    enter image description here