Closed eliocamp closed 11 months ago
Yeah I think ggplot2 has moved ahead on this with a smarter warning/rescue while facet_nested()
uses an older approach.
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")
mtcars$cyl <- factor(mtcars$cyl)
base %+% mtcars +
facet_grid(am ~ cyl + vs)
#> Warning: Combining variables of class <factor> and <numeric> was deprecated in ggplot2
#> 3.4.0.
#> ℹ Please ensure your variables are compatible before plotting (location:
#> `combine_vars()`)
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
#> Warning: Combining variables of class <numeric> and <factor> was deprecated in ggplot2
#> 3.4.0.
#> ℹ Please ensure your variables are compatible before plotting (location:
#> `combine_vars()`)
#> This warning is displayed once every 8 hours.
#> Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
#> generated.
Created on 2023-07-19 with reprex v2.0.2
What I don't understand is why is ggplot2 combining variables of different type. 🤔
IIRC they switched a bunch of internals to {vctrs} and came across some cases where {vctrs} was too strict (for backward compatibility) about combining different types of variables and then resolved that by being more permissive. The secret sauce is in ggplot2:::vec_rbind0()
, but this reaches very deep into the internals with weird restart invocations. I had to work with this function at some point and then and there decided that I'm never going to copy those internals due to their depth and niche functionality.
I'm going to close this issue as this is some backward compatibility issue that ggplot2 solved, but I'm not going to commit to fully mirror all solutions ggplot2 has in place for backward compatibility.
When the faceting variable is a factor and not all layers have the faceting variable
facet_nested()
throws errorAs an example. This works:
But if
cyl
is a factor, it doesn't.The error goes away if I remove the second layer.
Created on 2023-07-19 with reprex v2.0.2