tidyverse / ggplot2

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

strip.placement = "outside" and strip.switch.pad.wrap don't work together perfectly for facet_wrap. #5935

Open junjunlab opened 3 months ago

junjunlab commented 3 months ago

strip.switch.pad.wrap doesn't work when strip.placement = "outside" and strip.position = "bottom":

library(ggplot2)
base <- ggplot(mtcars, aes(mpg, drat)) +
  geom_point()

base + facet_wrap(~cyl) +
  theme(strip.placement = "outside",
        strip.switch.pad.wrap = unit(0.1,"npc"))

image

strip will only be added on one panel when strip.position = "bottom/left":

base + facet_wrap(~cyl,strip.position = "bottom",
                  ncol = 2) +
  theme(strip.placement = "outside",
        strip.switch.pad.wrap = unit(0.1,"npc"))

image

base + facet_wrap(~cyl,strip.position = "left") +
  theme(strip.placement = "outside",
        strip.switch.pad.wrap = unit(0.1,"npc"))

image

It doesn't work when strip.position = "right":

base + facet_wrap(~cyl,strip.position = "right") +
  theme(strip.placement = "outside",
        strip.switch.pad.wrap = unit(0.1,"npc"))

image

teunbrand commented 3 months ago

Arguably, it does work as documented in strip.switch.pad.wrap (emphasis mine):

space between strips and axes when strips are switched (unit); inherits from spacing.

So if there is no axis, there is no padding.

I can imagine a need for spacing between the panel and the strip, regardless of the presence of an axis. But I think that should probably be a separate theme element from strip.switch.pad.wrap and apply both to facet_wrap() and facet_grid().

Slightly off-topic; I don't know why there is a separate wrap/grid argument for the padding. You can't ever build a plot with both wrap and grid facetting, so they're mutually exclusive.

junjunlab commented 3 months ago

I got it, the padding only works when presence of an axis. Thanks, maybe it's a little need for some peole.