Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

facet_nested() adds "phantom" panels if there are nested facets. #116

Closed
eliocamp opened this issue Jul 19, 2023 · 2 comments
Closed

facet_nested() adds "phantom" panels if there are nested facets. #116

eliocamp opened this issue Jul 19, 2023 · 2 comments

Comments

@eliocamp
Copy link

Trying to solve other issues I found that facet_nested() behaves strangely in specific case.

I'm trying to plot data belonging to a category as "reference" data in every panel and without getting a panel of its own. This seems to work:

library(ggplot2)

base <- ggplot(mapping = aes(mpg, disp)) +
  geom_point(data = subset(mtcars, cyl != 4)) +
  geom_point(data = subset(mtcars, cyl == 4) |> 
               transform(cyl = NULL), 
             color = "gray") 

base +
  ggh4x::facet_nested(am ~ cyl)

Which is similar to ggplot2sfacet_grid()`.

base +
  facet_grid(am ~ cyl)

But if I add a nested facet, the "reference" data is moved to its own panel:

base +
  ggh4x::facet_nested(am ~ cyl + vs)

And note that this is not what ggplot2 does in this case

base +
  facet_grid(am ~ cyl + vs)

(Although note that ggplot2 does draw phantom panels if one uses a global data (see: tidyverse/ggplot2#5356) although it draws the value of the facet, which ggh4x doesn't do.)

Created on 2023-07-19 with reprex v2.0.2

@teunbrand
Copy link
Owner

I'm doing this on purpose in nested facets, which is described in the details of ?facet_nested():

Unlike facet_grid(), this function only automatically expands missing variables when they have no variables in that direction, to allow for unnested variables. It still requires at least one layer to have all faceting variables.

The reason why this might be desirable is explained here.

To have the default facet_grid() behaviour, you can use the following:

library(ggplot2)
library(ggh4x)

base <- ggplot(mapping = aes(mpg, disp)) +
  geom_point(data = subset(mtcars, cyl != 4)) +
  geom_point(data = subset(mtcars, cyl == 4) |> 
               transform(cyl = NULL), 
             color = "gray") 
base +
  facet_grid2(am ~ cyl + vs, strip = strip_nested())

Created on 2023-07-19 with reprex v2.0.2

@teunbrand
Copy link
Owner

I'm going to close this because this is by design and viable workarounds are available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants