Home > Mobile >  How to remove nesting facet labels in facet_nested (ggh4x package)
How to remove nesting facet labels in facet_nested (ggh4x package)

Time:08-24

I try to get rid of the nesting (upper) facet label. So far I can only apply changes to all facet labels together and not just the grouping ones.

library(tidyverse)
library(ggh4x)

df <- as_tibble(iris) %>%
  mutate(
    Nester = if_else(Species == "setosa", "Short Leaves", "Long Leaves"),
    Nester = factor(Nester)
  ) %>%
  pivot_longer(!c(Species, Nester), names_to = "Measure", values_to = "Value")

ylim <- df %>%
  split(.$Nester) %>%
  map(., ~ range(.$Value))

df %>%
  ggplot(aes(Measure, Value))  
  geom_boxplot()  
  theme(axis.text.x = element_text(angle = 90, , vjust = .5))  
  facet_nested(~ Nester   Species, scales = "free_y", independent = "y")  
  facetted_pos_scales(
    y = list(
      Species == "versicolor" ~ scale_y_continuous(limits = ylim[[1]]),
      Species == "virginica" ~ scale_y_continuous(limits = ylim[[1]], guide = "none"),
      Species == "setosa" ~ scale_y_continuous(limits = ylim[[2]])
    )
  )

Created on 2022-08-23 with enter image description here

I need a method that works with nested facets, as my actual data is far bigger and more complex.

EDIT: The solution needs to use nested facets.

CodePudding user response:

Update on OP request: This is a solution using the color trick white on white is not visible:

From enter image description here

First answer: We could use just facet_wrap for this. No need for facet_nested:

library(tidyverse)

p <- df %>%
  pivot_longer(!c(Species, Nester), names_to = "Measure", values_to = "Value") %>%
  ggplot(aes(Measure, Value))  
  geom_boxplot()  
  theme(axis.text.x = element_text(angle = 90, , vjust = .5))  
  facet_wrap(.~Species)

p

enter image description here

CodePudding user response:

This answer resembles @TarJae's answer, but uses element_blank()s instead. The result is pretty similar, but may be relevant if exporting graphics with a transparent background.

library(tidyverse)
library(ggh4x)

df <- as_tibble(iris) %>%
  mutate(
    Nester = if_else(Species == "setosa", "Short Leaves", "Long Leaves"),
    Nester = factor(Nester)
  ) %>%
  pivot_longer(!c(Species, Nester), names_to = "Measure", values_to = "Value")

ylim <- df %>%
  split(.$Nester) %>%
  map(., ~ range(.$Value))

strips <- strip_nested(
  text_x = list(element_blank(), element_text()),
  background_x = list(element_blank(), element_rect()), 
  by_layer_x = TRUE
)

df %>%
  ggplot(aes(Measure, Value))  
  geom_boxplot()  
  theme(axis.text.x = element_text(angle = 90, , vjust = .5))  
  facet_nested(
    ~ Nester   Species, scales = "free_y", independent = "y",
    strip = strips
  )  
  facetted_pos_scales(
    y = list(
      Species == "versicolor" ~ scale_y_continuous(limits = ylim[[1]]),
      Species == "virginica" ~ scale_y_continuous(limits = ylim[[1]], guide = "none"),
      Species == "setosa" ~ scale_y_continuous(limits = ylim[[2]])
    )
  )

Created on 2022-08-23 by the reprex package (v2.0.1)

  • Related