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() don't work with geom_*line() #72

Closed TiagoOlivoto closed 1 year ago

TiagoOlivoto commented 1 year ago

First of all, thank you so much for the impressive package, very useful!

I'm wondering if the following behaviour is expected. I'm trying to combine facet_nested() with geom_vline() to include a vertical line showing the mean of each level within a nested facet.

library(tidyverse)
library(ggh4x)

new_iris <- transform(
  iris, 
  Nester = ifelse(Species == "setosa", "Short Leaves", "Long Leaves")
)
mean_sepal_width <- 
  new_iris |> 
  group_by(Species) |> 
  summarise(mean = mean(Sepal.Width))

iris_plot <- 
  ggplot(new_iris, aes(Sepal.Width, Sepal.Length)) +
  geom_point()

# work with facet_wrap
iris_plot +
  facet_wrap(~Species) +
  geom_vline(aes(xintercept = mean),
             data = mean_sepal_width)


# don't work with facet_nested()
iris_plot +
  facet_nested(~ Nester + Species) +
  geom_vline(aes(xintercept = mean),
             data = mean_sepal_width)

I'm missing some argument or may this be a bug? Thanks in advance!

Created on 2022-07-31 by the reprex package (v2.0.1)

teunbrand commented 1 year ago

Hi there,

The means_sepal_width data.frame is missing the Nester variable. If you add it in group_by(Species, Nester), it should work as you intended. In this part of the vignette, it explained that not all data needs every facetting variable. The way this works is intentional, but does seem to work out unfortunately in your example.

TiagoOlivoto commented 1 year ago

Thank you so much @teunbrand , it worked like a charm! Since this definitively is not a bug, I'm closing this now. Best!