Organize a Grob with gtable elements

From https://stackoverflow.com/a/3129691/ I learned how to organize two plots with lined plot areas.

My question is: how can I get an object of stacked graphs?

Example:

require(ggplot2) require(gridExtra) A <- ggplot(CO2, aes(x=Plant)) + geom_bar() +coord_flip() B <- ggplot(CO2, aes(x=Type)) + geom_bar() +coord_flip() gA <- ggplot_gtable(ggplot_build(A)) gB <- ggplot_gtable(ggplot_build(B)) maxWidth = grid::unit.pmax(gA$widths[2:3], gB$widths[2:3]) gA$widths[2:3] <- as.list(maxWidth) gB$widths[2:3] <- as.list(maxWidth) ## works: grid.arrange(gA, gB, ncol=1) ## does not work: theplot <- grid.arrange(gA, gB, ncol=1, plot=FALSE) 
+4
source share
1 answer

Use the arrangeGrob() function to save both graphs as objects.

 theplot <- arrangeGrob(gA, gB, ncol=1) 
+5
source

All Articles