1

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!

Tobler
  • 41
  • 5
  • Yeah, grid.arrange does that. One way is to draw graphs without a legend and add the legend seperately. See [common legend for combined ggplots](http://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots) – RHA Jun 27 '15 at 14:52
  • Awesome! That worked pretty well actually. The only issue I am having now is that the legends (I have one for each row) are aligned center in their column. It looks a bit funky, because not all legend labels have the same length. Any chance somebody knows how to left align things in grid.arrange? – Tobler Jun 29 '15 at 14:49
  • Have a look at [consistent width plots in ggplot with legends](http://stackoverflow.com/questions/16255579/how-can-i-make-consistent-width-plots-in-ggplot-with-legends/16258375#16258375) and at [justification of multiple legends](http://stackoverflow.com/questions/13426470/justification-of-multiple-legends-in-ggmap-ggplot2) – Sandy Muspratt Jun 29 '15 at 20:15

0 Answers0