I’m sorry necroing this thread and unintended self-promotion, but I had a go at generalizing this to a facet_nested()
function and it can be found in the ggh4x package.
The function isn’t tested extensively but I thought it might be of some convenience to people. Maybe some good feedback will come from this.
There are two other modifications that I made in this function beyond the scope of grouping strips. One is that it doesn’t automatically expand missing variables. This is because I was of the opinion that nested facets should be able to co-exist with non-nested facets without any entries to the 2nd or further arguments in vars()
when plotting with two data.frames. The second is that it orders the strips from outer to inner, so that inner is nearer to the panels than outer, even when switch
is set.
Reproducing the plot in this question would then be as follows, assuming df
is the df
in the question above:
# library(ggh4x)
p <- ggplot(df, aes(x = x, y = y)) +
geom_point() +
facet_nested(f1 ~ f2 + f3)
There was also a related question with a more real-world example plot, which would work like the following, assuming df
is the df
from that question:
p <- ggplot(df, aes("", density)) +
geom_boxplot(width=0.7, position=position_dodge(0.7)) +
theme_bw() +
facet_nested(. ~ species + location + position) +
theme(panel.spacing=unit(0,"lines"),
strip.background=element_rect(color="grey30", fill="grey90"),
panel.border=element_rect(color="grey90"),
axis.ticks.x=element_blank()) +
labs(x="")