在ggplot2中调整带有和不带图例的图形的条形图大小

时间:2015-06-26 20:59:42

标签: r plot ggplot2 legend

大家好我试图用ggplot2和gridExtra显示一组条形图(2行,2列)。同一行中的图形共享相同的图例,因此我只想显示一次。这是我的代码:

Up_BP <- ggplot(data1, aes(x=Category, y=Proportion, fill=Term))  + geom_bar(stat="identity", colour="black", width=0.8) + theme_bw() + scale_fill_hue() + theme(axis.title.x = element_text(face="bold", colour="black", size=20, vjust=0.0),  axis.text.x  = element_text(angle=0, vjust=0.5, size=16))  + theme(axis.title.y = element_text(face="bold", colour="black", size=20, vjust=1),  axis.text.y  = element_text(angle=0, vjust=0.5, size=16)) + scale_y_continuous(expand = c(0,0.001), limits = c(0,1.001)) + theme(panel.background = element_rect(colour = "black", size = 1.5)) + labs(title = "Up-regulated transcripts:\nBiological process") + theme(plot.title = element_text(face="bold", size = 25, vjust=1)) + scale_x_discrete(labels = function(Category) str_wrap(Category, width = 20)) + theme(legend.title = element_text(colour="Black", size=20, face="bold")) + theme(legend.text = element_text(colour="black", size = 16)) + coord_fixed(ratio = 1.5) + theme(legend.position = "none") 
Down_BP <- ggplot(data2, aes(x=Category, y=Proportion, fill=Term))  + geom_bar(stat="identity", colour="black", width=0.8) + theme_bw() + scale_fill_hue() + theme(axis.title.x = element_text(face="bold", colour="black", size=20, vjust=0.0),  axis.text.x  = element_text(angle=0, vjust=0.5, size=16))  + theme(axis.title.y = element_text(face="bold", colour="black", size=20, vjust=1),  axis.text.y  = element_text(angle=0, vjust=0.5, size=16)) + scale_y_continuous(expand = c(0,0.001), limits = c(0,1.001)) + theme(panel.background = element_rect(colour = "black", size = 1.5)) + labs(title = "Down-regulated transcripts:\nBiological process") + theme(plot.title = element_text(face="bold", size = 25, vjust=1)) + scale_x_discrete(labels = function(Category) str_wrap(Category, width = 20)) + theme(legend.title = element_text(colour="Black", size=20, face="bold")) + theme(legend.text = element_text(colour="black", size = 16)) + coord_fixed(ratio = 1.5)
Up_MF <- ggplot(data3, aes(x=Category, y=Proportion, fill=Term))  + geom_bar(stat="identity", colour="black", width=0.8) + theme_bw() + scale_fill_hue() + theme(axis.title.x = element_text(face="bold", colour="black", size=20, vjust=0.0),  axis.text.x  = element_text(angle=0, vjust=0.5, size=16))  + theme(axis.title.y = element_text(face="bold", colour="black", size=20, vjust=1),  axis.text.y  = element_text(angle=0, vjust=0.5, size=16)) + scale_y_continuous(expand = c(0,0.001), limits = c(0,1.001)) + theme(panel.background = element_rect(colour = "black", size = 1.5)) + labs(title = "Up-regulated transcripts:\nMolecular function") + theme(plot.title = element_text(face="bold", size = 25, vjust=1)) + scale_x_discrete(labels = function(Category) str_wrap(Category, width = 20)) + theme(legend.title = element_text(colour="Black", size=20, face="bold")) + theme(legend.text = element_text(colour="black", size = 16)) + coord_fixed(ratio = 1.5) + theme(legend.position = "none")
Down_MF <- ggplot(data4, aes(x=Category, y=Proportion, fill=Term))  + geom_bar(stat="identity", colour="black", width=0.8) + theme_bw() + scale_fill_hue() + theme(axis.title.x = element_text(face="bold", colour="black", size=20, vjust=0.0),  axis.text.x  = element_text(angle=0, vjust=0.5, size=16))  + theme(axis.title.y = element_text(face="bold", colour="black", size=20, vjust=1),  axis.text.y  = element_text(angle=0, vjust=0.5, size=16)) + scale_y_continuous(expand = c(0,0.001), limits = c(0,1.001)) + theme(panel.background = element_rect(colour = "black", size = 1.5)) + labs(title = "Down-regulated transcripts:\nMolecular function") + theme(plot.title = element_text(face="bold", size = 25, vjust=1)) + scale_x_discrete(labels = function(Category) str_wrap(Category, width = 20)) + theme(legend.title = element_text(colour="Black", size=20, face="bold")) + theme(legend.text = element_text(colour="black", size = 16)) + coord_fixed(ratio = 1.5)
grid.arrange(Up_BP, Down_BP, Up_MF, Down_MF, ncol=2, nrow=2)

代码工作正常,但我在缩放条形图时遇到问题。 “coord_fixed(ratio = 1.5)”命令似乎也考虑了图例,因此,右列图小于左侧的图表(抱歉由于某种原因无法发布图片)。

关于如何在每个情节都存在的情况下将每个情节都放在同一维度上的任何建议?此外,我想包装一些传奇文本,如果可能的话。 谢谢!

0 个答案:

没有答案