Question

From https://stackoverflow.com/a/13295880 I learned how to arrange two plots with aligned plot areas.

My question is: How can I get an object of the arranged plots?

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)
Était-ce utile?

La solution

Use function arrangeGrob() to save both plots as object.

theplot <- arrangeGrob(gA, gB, ncol=1)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top