teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

facet_nested have a different behaviour as compare to facet_grid when combining character and numeric #139

Closed smouksassi closed 6 months ago

smouksassi commented 6 months ago

I expected faced nested to repeat the vlines in all panels since the faceting variable is missing i.e same behaviour of facet_grid

library(ggplot2)
library(ggh4x)
library(patchwork)

 (ggplot(mtcars,aes(hp,mpg,color=factor(gear)))+
  geom_point()+
  geom_vline(data= data.frame(vs = c("0","0","1","1"),
                        intercept=c(100,200,200,300)),
             aes(xintercept=intercept), color = "red")+
  facet_nested(~gear+vs) ) /
(ggplot(mtcars,aes(hp,mpg,color=factor(gear)))+
  geom_point()+
  geom_vline(data= data.frame(vs = c("0","0","1","1"),
                              intercept=c(100,200,200,300)),
             aes(xintercept=intercept), color = "red")+
  facet_grid(~gear+vs))
#> Warning: Combining variables of class <numeric> and <character> 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 2024-01-03 with reprex v2.0.2

teunbrand commented 6 months ago

Thanks for the report Samer!

This behaviour is on purpose, 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.

Motivation for this decision is found in the vignette.

If you do not want this behaviour, you can use facet_grid2(..., strip = strip_nested()) to achieve a plot more closely resembling the facet_grid() behaviour.

The issue was also reported in #116 and my viewpoint (it is intended and it will not change) remains the same.

smouksassi commented 6 months ago

thank you this is now much clearer facet_grid2(..., strip = strip_nested()) does the trick