Search code examples
ggplot2facet

ggplot: how to remove unused factor levels from a facet?


The following code

d1 = data.frame(y=1:2,group=factor(c('A','B'), levels=c('A','B','C')), fac = 'f1')
d2 = data.frame(y=1:3,group=factor(c('A','B','C'), levels=c('A','B','C')), fac = 'f2')
d = rbind(d1,d2)

library(ggplot2)
ggplot( d, aes(x=group, y=y) ) + geom_point(size=3) + facet_grid( ~ fac)

Results in the following plot. How can I remove the unused factor level C from the facet 'f1'? enter image description here


Solution

  • Setting scales = free in facet grid will do the trick:

    facet_grid( ~ fac, scales = "free")