Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrange ggplot plots (grobs with same widths) using gtable to create 2x2 layout

I am attempting to use grobs and gtable to arrange 4 (ggplot2) plots into a 2x2 grid. I don't know how to set widths, and also a non- 1xn, or nx1 arrangement.

Using this code:

data(iris)
a <- ggplot(iris, aes(x=Species, y=Petal.Width)) + geom_boxplot(color="black") + ylab(expression(Foo~Bar~(g~cm^{-3})))
b <- ggplot(iris, aes(x=Species, y=Petal.Length*100)) + geom_boxplot(color="black") + ylab("foobar (mm)")
c <- ggplot(iris, aes(x=Species, y=Sepal.Width)) + geom_boxplot(color="black") + ylab("foobar (%)")
d <- ggplot(iris, aes(x=Species, y=log10(Sepal.Length))) + geom_boxplot(color="black") + ylab("foobar (cm)")

plots <- list(a,b,c,d)
grobs = lapply(plots, ggplotGrob)
g = do.call(rbind, c(grobs, size="first"))

g$widths = do.call(unit.pmax, lapply(grobs, "[[", "widths"))
grid.newpage()
grid.draw(g)

I can create the following 1x4 arrangement. 1 by 4 plot arrangement

If I use grid.arrange for two columns, on the 4 plots, the plots are of different widths.

4 by 4 plot arrangement - unequal widths

How can I bind plots into a gtable for a 4 x 4 arrangement?

# I thought maybe I could cbind, then rbind, but this does not work
plots1 <- list(a,b)
plots2 <- list(c,d)
grobs1 = lapply(plots1, ggplotGrob)
grobs2 = lapply(plots2, ggplotGrob)

g1 = do.call(cbind, c(grobs1, size="first"))
g2 = do.call(cbind, c(grobs2, size="first"))
# g3 = do.call(rbind, c(g1,g2, size="first")) #this does not work
like image 431
nofunsally Avatar asked Jan 28 '16 17:01

nofunsally


People also ask

How do I arrange multiple Ggplots?

To arrange multiple ggplots on one single page, we'll use the function ggarrange()[in ggpubr], which is a wrapper around the function plot_grid() [in cowplot package]. Compared to the standard function plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.

How do I plot two Ggplots together?

Combine multiple ggplot on one page.Use the function ggarrange() [ggpubr package], a wrapper around the function plot_grid() [cowplot package]. Compared to plot_grid(), ggarange() can arrange multiple ggplots over multiple pages.

What is Grid Arrange function in R?

If layout parameters are ommitted altogether, grid. arrange() will calculate a default number of rows and columns to organise the plots. More complex layouts can be achieved by passing specific dimensions (widths or heights), or a layout matrix defining the position of each plot in a rectangular grid.


2 Answers

I think you already had the answer.
Your last line returns an error, but a small edit results in a combined plot where widths within columns are the same:

g3 = do.call(rbind, c(list(g1,g2), size="first")) #combine g1 and g2 into a list g3

A sidenote for aesthetics/reference:
If your x-axis is the same, you can drop it from the top two plots.

library(ggplot2); library(gridExtra); library(grid)

# Tweak the margins to use up empty space.  Margins: Top, Right, Bottom, Left
# For reference: a1= top left,    b1= top right 
#                c1= bottom left, d1= bottom right
a1 <- a + theme(axis.title.x = element_blank(), 
                axis.text.x = element_blank(), 
                axis.ticks.x= element_blank(), 
                plot.margin= unit(c(1, 1, -0.5, 0.5), "lines") ) 
b1 <- b + theme(axis.title.x = element_blank(), 
                axis.text.x = element_blank(), 
                axis.ticks.x= element_blank(), 
                plot.margin= unit(c(1, 1, -0.5, 0.5), "lines") ) 
c1 <- c + theme(plot.margin= unit(c(0, 1, 0.5, 0.5), "lines") )  
d1 <- d + theme(plot.margin= unit(c(0, 1, 0.5, 0.5), "lines") )  

grobz <- lapply(list(a1, b1, c1, d1), ggplotGrob)
grobz.plot <- arrangeGrob( grobs = list(rbind(grobz[[1]], grobz[[3]], size = "last"),
                                        rbind(grobz[[2]], grobz[[4]], size = "last")),
                           ncol = 2)
grid.draw(grobz.plot)

grobz

These StackOverflow questions are helpful in aligning plots:

  • Using rbind in gtable to set plot width (Baptiste) [link]
  • Relative panel heights in gtable (Baptiste) [link]
  • Plot widths and legends [link]
like image 78
oshun Avatar answered Sep 21 '22 06:09

oshun


Pretty similar to the above, but using gtable functions*

library(ggplot2)
pl <- list(ggplot() + xlab("x"), 
           ggplot() + ylab("y"), 
           ggplot() + ylab("y"), 
           ggplot() + ggtitle("title") + xlab("x"))

library(grid)
library(gridExtra)

gl <- lapply(pl, ggplotGrob)
# gt <- cbind(rbind(gl[[1]], gl[[3]]),
#            rbind(gl[[2]], gl[[4]]))

#  alternative to remove x-axes of top row of plots
gt <- cbind(rbind(gl[[1]][1:3,], gl[[3]]),
            rbind(gl[[2]][1:3,], gl[[4]]))

grid.newpage()
grid.draw(gt)

enter image description here

*: actually, since gtable doesn't allow the use of pmax when comparing units, this is using a drop-in replacement from the dev version of gridExtra.

like image 25
baptiste Avatar answered Sep 24 '22 06:09

baptiste