teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

Inner vs. outer panel spacing using `strip_nested()` #126

Closed natalieoshea closed 9 months ago

natalieoshea commented 9 months ago

I'm really appreciating the ability to set different plotting parameters for inner vs. outer strip panels in a nested facet plot using unique arguments to the strip parameter via strip_nested(). Would it be possible to add the ability to dynamically set different panel.spacing theme elements as well? I would love to be able to have narrower panel margins on my inner strip panels and larger panel margins on my outer strip panels. Thank you!

teunbrand commented 9 months ago

If you know the amount of panels in a direction, you can already set (# panels - 1) panel spacing in the theme in vanilla ggplot2 (see example). The same works for ggh4x, see e.g. #92. I don't think it will be straightforward to implement a more programmatic way of setting hierarchical panel spacing, so for now, I'm not planning on implementing this.

If somebody is willing to put together a PR to implement this, I'll reopen this issue.

library(ggplot2)

ggplot(mpg, aes(displ, hwy)) +
  geom_point() +
  facet_wrap(~ cyl, nrow = 1) +
  theme(
    panel.spacing.x = unit(c(1, 0.5, 0.25), "cm")
  )

Created on 2023-09-21 with reprex v2.0.2