我正在尝试使用ggplot2制作更好的R基础图.不仅有一个共同的传奇,而且因为我喜欢ggplot2样式和自定义.我的数据由3个单独的数据集组成,这些数据集包含几组(但不同)治疗的相同的两组观察结果.因此,我想在1个图中生成3个单独的图,但是具有不同的因子水平.为了说明我的观点,这里的第一个图像是我到目前为止用R base生成的图像:

我尝试使用与我的数据具有完全相同结构的伪数据生成ggplot2图:
foo<-data.frame(c(letters,letters),c(rep('T1',26),rep('T2',26)),
runif(52),rep(c(rep('Ori1',12),rep('Ori2',8),rep('ori3',6)),2))
names(foo)<-c('Treatment','Type','Count','Origin')
a<-ggplot(foo,aes(x = factor(Treatment),y = Count))
a+ facet_grid(Origin~., scales="free_y", space="free") +
geom_bar(stat="identity",aes(fill=factor(foo$Type)),position="dodge")
+theme_bw()+theme(axis.text.x=element_text(angle=60,hjust=1))+coord_flip()
Run Code Online (Sandbox Code Playgroud)
这给了我以下不良后果.

我知道堆栈溢出主题从ggplot2中的Facet中删除未使用的因子以及如何从ggplot2 facets中删除空因子?但是,他们没有处理我试图在这里实现的聚类条形图,我觉得它们是问题,但现在不知道如何解决它.欢迎所有指针.
为了说明我的评论:
a<-ggplot(foo,aes(x = factor(Treatment),y = Count))
a+ facet_wrap(~Origin, scales="free_x") +
geom_bar(stat="identity",aes(fill=factor(Type)),position="dodge") +
theme_bw() +
theme(axis.text.x=element_text(angle=60,hjust=1))
Run Code Online (Sandbox Code Playgroud)

请注意,如果添加coord_flip并切换到free_y您,则会收到有关coord_flip不使用某些类型的自由缩放的特定错误,这是您遇到问题的根源.