tidyverse / ggplot2

An implementation of the Grammar of Graphics in R
https://ggplot2.tidyverse.org
Other
6.39k stars 2k forks source link

Variable panel size (`space`) for `facet_wrap()` #5956

Open teunbrand opened 1 week ago

teunbrand commented 1 week ago

This PR addresses a topic discussed off-github.

To recap, there exist ggforce::facet_rows() and ggforce::facet_cols() that allow variable space space for panels, but confines the layout to a single row or column respectively. We realised that it would be straightforward this in ggplot2 after #5917 would be implemented. There'd need to be a space argument that needs to be passed on, and when space = "free_x/y", we'd need to restrict the nrow/ncol layout options. That is what this PR does.

Some examples. 1 row, free column-width:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2
p <- ggplot(mpg, aes(displ, hwy)) +
  geom_point()

p + facet_wrap(~ drv, space = "free_x", scales = "free_x")

1 column, free row-height:

p + facet_wrap(~ drv, space = "free_y", scales = "free_y")

Giving space = "free_x/y" and ncol/nrow throws a warning and ncol/nrow are ignored.

p + facet_wrap(~ drv, space = "free_x", scales = "free_x",
               ncol = 2, nrow = 1)
#> Warning: Cannot use `space = "free_x"` with custom `nrow` or `ncol`.

Created on 2024-06-25 with reprex v2.1.0