使用小平面和翻转坐标删除 ggplot boxplot 中未使用的级别?

Removing unused levels in ggplot boxplot with facets AND flipped coordinates?

在 R 中,我试图在 ggplot 中制作一个箱线图,其中翻转坐标(水平框)使用 facets 分组。当我在不翻转坐标的情况下构建它时,ggplot 将在 scales="free" 的方面内删除未使用的因子水平,但是当我还包括 coord_flip.

时这似乎不起作用

最小示例:

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)))

不删除未使用级别的普通分面箱线图有效(但不是我想要的):

ggplot(dat, aes(x=GROUP, y=RESP)) + 
  geom_boxplot() + 
  facet_grid(.~FACET)

水平下降的普通分面箱线图也可以正常工作(不是我想要的):

ggplot(dat, aes(x=GROUP, y=RESP)) + 
  geom_boxplot() + 
  facet_grid(.~FACET, scales="free", space="free")

具有翻转坐标的多面箱线图(我想要的)不会删除未使用的级别:

ggplot(dat, aes(x=GROUP, y=RESP)) + 
  geom_boxplot() + 
  facet_grid(FACET~., scales="free", space="free") + 
  coord_flip()

重新排列 ggplot 命令的顺序并不能解决问题。我怀疑答案是在FACET的一些调整中~。公式,但解不出来

这是 ggplot2 的问题:coord_flip and free scales don't work

您可以在此处阅读有关此事的讨论:

How to drop unused factors in faceted R ggplot boxplot?

In ggplot2, coord_flip and free scales don't work together