ggh4x icon indicating copy to clipboard operation
ggh4x copied to clipboard

`facet_grid_2()`: remove empty panels when `independent` is not set to `all`

Open aleixop opened this issue 2 years ago • 2 comments

Hi, thanks for the great package! I was wondering if it is possible to remove empty panels when using facet_grid2() and setting independent to either x or y instead of all. Here's an example:

p <- ggplot(mpg, aes(displ, hwy)) + geom_point()
p + facet_grid2(cyl ~ drv, independent = 'y', scales = 'free_y')

Captura de pantalla 2022-06-10 a les 8 59 02

Is there a way to remove empty panels as it would happen if using scales = 'free' and independent = 'all'?

Thanks!

aleixop avatar Jun 10 '22 07:06 aleixop

Hi there, thanks for your suggestion. I think it'd make sense to include some option to drop empty panels. I don't know when I'll get around to doing it though. Currently, also with independent = "all", the panels are still drawn (you can see this when the panel background has a different fill than the plot background), so I don't think it is possible unless you wish to edit the ggplot grobs with {gtable}/{grid}. It'd probably makes most sense to add an argument like drop_empty_panel = TRUE/FALSE to the function.

teunbrand avatar Jun 10 '22 09:06 teunbrand

Thank you for your quick response! I didn't realize panels were drawn because I was using theme_minimal(). The argument you suggest would be a good solution. Thanks!

aleixop avatar Jun 10 '22 09:06 aleixop

I am aslo facing the same issue. When can we expect to have drop_empty_panel = TRUE/FALSE argument?

bappa10085 avatar May 03 '23 17:05 bappa10085

That is a fair question that I don't know the answer to.

teunbrand avatar May 03 '23 17:05 teunbrand

One can now do the following in the development version:

library(ggh4x)
#> Loading required package: ggplot2
ggplot(mpg, aes(displ, hwy)) + geom_point() +
  facet_grid2(cyl ~ drv, render_empty = FALSE)

Created on 2023-07-15 by the reprex package (v2.0.1)

teunbrand avatar Jul 15 '23 10:07 teunbrand