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

为君一笑 提交于 2020-01-30 12:38:05

问题


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'?


回答1:


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

facet_grid( ~ fac, scales = "free")



来源:https://stackoverflow.com/questions/57026999/ggplot-how-to-remove-unused-factor-levels-from-a-facet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!