facet_wrap2() group by two variables, but label with one

50 views Asked by At

My problem

I have a plot that I have used facet_wrap2() from the ggh4x library to plot. I have used two groupiung variables; groundSurface (either a ground or a surface sample) and fourLoc (a location ID).

On the final plot I only want the "groundSurface showing". Not the "fourLoc" identifier. i.e. no "BE" and "mere" labels. Only "ground" or "surface".

Here is an example plot.

enter image description here

Here is my code:

library(ggh4x)
library(ggplot2)

p1t <- ggplot(test, aes(x=DateTime, y=mAODscale)) +
  geom_line() +
  facet_wrap2(groundSurface ~ fourLoc, drop=FALSE, ncol=2, strip=strip_nested(bleed=FALSE)) +
  labs(x=NULL, y='value', color=NULL) +
  theme_bw() +
  theme(axis.text.x=element_text(angle=90, vjust=0.5),
        text=element_text(size=12, 'Calibri'),
        legend.direction="horizontal",legend.position="bottom", legend.box="vertical")

plot(p1t)

Here is the data for the plot:

test <- structure(list(loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("B1", 
"HM1", "HM3"), class = "factor"), DateTime = c("01/03/2022", 
"02/03/2022", "03/03/2022", "01/03/2022", "02/03/2022", "03/03/2022", 
"01/03/2022", "02/03/2022", "03/03/2022", "01/03/2022", "02/03/2022", 
"03/03/2022", "01/03/2022", "02/03/2022", "03/03/2022", "01/03/2022", 
"02/03/2022", "03/03/2022"), calYear = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "2022", class = "factor"), 
    month = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), levels = "3", class = "factor"), 
    spi = c(0.131279514, 0.875832767, 0.105353781, 0.652808322, 
    0.348200666, 0.137788759, 0.546853561, 0.878139205, 0.649630365, 
    0.133656114, 0.960711263, 0.107314452, 0.866935108, 0.401248114, 
    0.553906897, 0.350247914, 0.59256722, 0.609365765), fourLoc = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
    2L, 2L), levels = c("BE", "mere"), class = "factor"), mAODscale = c(0.344703325, 
    0.813991101, 0.50608775, 0.130934881, 0.502777104, 0.67505956, 
    0.440368963, 0.800293223, 0.028270253, 0.416160857, 0.033790935, 
    0.625116508, 0.341240233, 0.437566191, 0.823241792, 0.163872636, 
    0.134161724, 0.233193229), label2 = structure(c(1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L
    ), levels = c("B1", "HM1", "HM3"), class = "factor"), groundSurface = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 
    2L, 2L), levels = c("ground", "surface"), class = "factor"), 
    maxDate = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 
    1L, 0L, 0L, 1L, 0L, 0L), MinDate = c(0L, 1L, 0L, 0L, 1L, 
    0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L), maxDate2 = c(0L, 
    0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 
    0L, 1L), noMaxDate2 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
    0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), Wtstart = c(1L, 0L, 
    0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 
    0L)), row.names = c(NA, -18L), class = "data.frame")
1

There are 1 answers

0
stefan On

One option to get rid of the labels for the second variable would be to set by_layer_x=TRUE and to remove the strip background and text using element_blank(). However, this still leaves a blank space in place of the strip texts. Unfortunately I haven't found an option to get rid of that.

library(ggh4x)
#> Loading required package: ggplot2
library(ggplot2)

ggplot(test, aes(x = DateTime, y = mAODscale)) +
  geom_line() +
  facet_wrap2(groundSurface ~ fourLoc,
    drop = FALSE, ncol = 2,
    strip = strip_nested(
      bleed = FALSE,
      by_layer_x = TRUE,
      background_x = list(
        element_rect(),
        element_blank()
      ),
      text_x = list(
        element_text(),
        element_blank()
      )
    )
  ) +
  labs(x = NULL, y = "value", color = NULL) +
  theme_bw() +
  theme(
    axis.text.x = element_text(angle = 90, vjust = 0.5),
    text = element_text(size = 12, "Calibri"),
    legend.direction = "horizontal", legend.position = "bottom", legend.box = "vertical"
  )