ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Setting panel sizes in `theme()`

Open teunbrand opened this issue 1 year ago • 2 comments

This PR aims to fix #5338.

Briefly, the layout object gets a new method that sets the panel sizes.

The gist is that you can give a vector of units to theme(panel.widths) or theme(panel.heights) that then applies to each panel. It overrules any aspect ratios set in the coord or theme.

Somewhat special behaviour occurs if you give a unit of length 1, but you have multiple panels. In that case, the total panel area is set, rather than the size of individual panels.

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_grid(~ drv)

# Square 2x2cm panels
p + theme(
  panel.widths = unit(c(2, 2, 2), "cm"),
  panel.heights = unit(2, "cm")
)

# Total width is 6cm, so every panel is somewhat narrower than 2cm
p + theme(
  panel.widths = unit(6, "cm"),
  panel.heights = unit(2, "cm")
)

Created on 2024-09-09 with reprex v2.1.1

I'm not quite sure whether the layout is the correct place for the method. It can also belong to the facet class, for example.

teunbrand avatar Sep 09 '24 17:09 teunbrand