maRvin
maRvin

Reputation: 335

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

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 reprex v2.0.2

I manually edited the plot above to show the desired plot: 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.

Upvotes: 1

Views: 1162

Answers (2)

teunbrand
teunbrand

Reputation: 37953

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)

Upvotes: 3

TarJae
TarJae

Reputation: 78937

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

From https://teunbrand.github.io/ggh4x/articles/Facets.html

  my_strips <- strip_themed(
    # Horizontal strips
    background_x = elem_list_rect(fill = c("white", "dodgerblue")),
    text_x = elem_list_text(colour = c("white", "white"),
                            face = c("bold", "bold")),
    by_layer_x = TRUE,
    text_y = elem_list_text(angle = c(0, 90)),
    by_layer_y = FALSE
  )
  
  
  df %>%
    pivot_longer(!c(Species, Nester), names_to = "Measure", values_to = "Value") %>%
    ggplot(aes(Measure, Value)) +
    geom_boxplot() +
    facet_nested(~ Nester + Species , scales = "free_y", independent = "y", 
                 strip = my_strips)+
    theme(
      axis.text.x=element_text(angle=90, hjust=0, vjust=0))
  

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

Upvotes: 3

Related Questions