如何在ggplot2中更改一个facet的宽度

时间:2017-02-24 17:05:20

标签: r ggplot2

我有一个刻面的条形图,其中一个组的成员少于其他组。我可以通过设置scales = "free_x"来仅绘制存在的组,但是小平面与其他小组的宽度相同,因此条形更宽。这令人分心,我不认为我可以提交出版的数字。我知道ggplot2中的faceting功能曾经允许你调整各个方面的宽度,但这似乎不再是这种情况。

这是一个例子。我想让右侧小平面更窄,以使条形宽度与其他小平面相同。任何帮助非常感谢。

response<-rnorm(42, mean=10)
factor1<-factor(rep(c("A","B","C"), each=14))
factor2<-factor(rep(c(1,2,3,1,2,3,1,2,3,1,2,3,1,2), times=3))
factor3<-factor(rep(c("X","X","X","Y","Y","Y","X","X","X","Y","Y","Y","Y","Y"),times=3))
factor4<-factor(rep(c(rep(c("27 degrees","30 degrees"), each=6),"33 degrees","33 degrees"), times=3))

data1<-data.frame(response,factor1,factor2,factor3,factor4)


p1<- ggplot(data=data1, aes(x=factor2, y=response, fill=factor1))
p1<- p1+geom_bar(stat="identity")
p1<- p1+facet_grid(~factor4+factor3,scales = "free_x")+theme_bw()
p1<- p1+scale_y_continuous(expand = c(0,0))+theme(strip.background=element_rect( fill="white"))
p1<- p1 + scale_fill_brewer(guide = guide_legend(reverse=TRUE))
p1

这给出了:

enter image description here

0 个答案:

没有答案