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

Facets use nonexistent values if removed from inside geoms. #5356

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

Facets use nonexistent values if removed from inside geoms. #5356

eliocamp opened this issue Jul 19, 2023 · 2 comments

Comments

@eliocamp
Copy link
Contributor

facet_wrap() (and facet_grid()) seem to draw panels for values that are not drawn if they are removed using the data argument in a geom.

So, this draws a panel for cyl = 4 even though that data doesn't exist in the plot.

library(ggplot2)

ggplot(mtcars, aes(mpg, disp)) +
  geom_point(data = \(x) subset(x, cyl != 4)) +
  facet_wrap(~cyl, drop = TRUE)

Compare with the result removing the category at the top-level call of the plot.

mtcars |> 
  subset(cyl != 4) |> 
  ggplot(aes(mpg, disp)) +
  geom_point() +
  facet_wrap(~cyl, drop = TRUE)

The use case for this is to use one of the categories as the reference and draw it in every panel, but I don't want it to have its own panel. So my though was to plot one geom without the reference category and then another with just the reference and without the faceting variable. But it doesn't work because it adds the panel for the reference category even though that category is never seen by any geom.

ggplot(mtcars, aes(mpg, disp)) +
  geom_point(data = \(x) subset(x, cyl != 4)) +
  geom_point(data = \(x) subset(x, cyl == 4) |> 
               transform(cyl = NULL), 
             color = "gray") +
  facet_wrap(~cyl, drop = TRUE)

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

@teunbrand
Copy link
Collaborator

I've noticed this too but have no idea whether this behaviour is intended or not.
In any case, from a quick test it seems that ignoring the global data in Facet$compute_layout() violates some facet order expectations in the tests, so I'm not sure how easy this is to fix (if at all desired).

@eliocamp
Copy link
Contributor Author

If it's any help to decide, not adding global data and just passing the correct data to the geoms works as expected:

library(ggplot2)
ggplot(mapping = aes(mpg, disp)) +
  geom_point(data = subset(mtcars, cyl != 4)) +
  geom_point(data = subset(mtcars, cyl == 4) |> 
               transform(cyl = NULL), 
             color = "gray") +
  facet_wrap(~cyl, drop = TRUE)

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

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

No branches or pull requests

2 participants