In R, I'm trying to make a boxplot in ggplot with flipped coordinates (horizontal boxes) grouped using facets. When I build this without flipping coordinates, ggplot will drop unused factor levels within facets with scales="free", but this doesn't seem to work when I also include coord_flip.
Minimal example:
library('ggplot2')
dat <- data.frame(RESP=rnorm(60), GROUP=rep(letters[1:6],each=10), FACET=c(rep(LETTERS[1:2],each=25),rep(LETTERS[3],10)))
The normal faceted boxplot wihtout dropping unused levels works (but not what I want):
ggplot(dat, aes(x=GROUP, y=RESP)) +
geom_boxplot() +
facet_grid(.~FACET)
The normal faceted boxplot with dropped levels also works fine (not what I want):
ggplot(dat, aes(x=GROUP, y=RESP)) +
geom_boxplot() +
facet_grid(.~FACET, scales="free", space="free")
The faceted boxplot with flipped coordinates (what I want) does not drop the unused levels:
ggplot(dat, aes(x=GROUP, y=RESP)) +
geom_boxplot() +
facet_grid(FACET~., scales="free", space="free") +
coord_flip()
Re-arranging the order of the ggplot commands doesn't fix it. I suspect the answer is in some adjustment of the FACET~. formula, but can't solve it.