Hi all I am trying to display a panel of bar plots (2 rows, 2 columns) with ggplot2 and gridExtra. The graphs in the same row share the same legend, hence I want to display it only once. Here is my code:
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)
The code works just fine, but I have a problem with scaling the bar plots. The "coord_fixed(ratio = 1.5)" command appears to take into account the legend as well, and accordingly, the right column graphs are smaller than the ones on the left side (sorry can't post picture for some reason).
Any advice on how I can have each plot in the same dimension despite the presence of the legend? Also, I would like to wrap some of the legend text, if that is possible. Thanks!