ggplot2
ggplot2 copied to clipboard
Facets use nonexistent values if removed from inside geoms.
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
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).
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