3

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.

David Roberts
  • 617
  • 1
  • 11
  • 23

1 Answers1

4

It is an issue of ggplot2: coord_flip and free scales don't work

You can read a discussion about this matter here:

How to drop unused factors in faceted R ggplot boxplot?

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

Community
  • 1
  • 1
mpalanco
  • 12,960
  • 2
  • 59
  • 67
  • Sure, using grid.arrange() seems like a useful fix. It just necessitates making a bunch of plots, which is annoying, but not the end of the world. Thanks for the links. – David Roberts Jul 29 '15 at 17:46