Recommand · September 23, 2021 0

Collapse one facet when using facet_grid with multiple variables

If I had a plot like so:

library(ggplot2)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- diamonds %>% 
  group_by(cut, color, clarity) %>% 
  filter(cut %in% c("Fair","Good","Very Good") &
           clarity %in% c("SI1","SI2","VS1","VS2") &
           color %in% c("D","E","F")) %>% 
  summarise(meanprice = mean(price))
#> `summarise()` has grouped output by 'cut', 'color'. You can override using the `.groups` argument.

ggplot(df, aes(y=color, x = meanprice)) +
  geom_bar(stat = "identity") +
  facet_grid(rows = cut + clarity ~.)

Created on 2021-09-23 by the reprex package (v2.0.1)

Is there a simple way to prevent the ‘clarity’ facet from being repeated? That is, it would only say "Fair" once, "Good" once, etc. Ideally there would be no space at that level of the facet (so the grouping is clear).