Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify widths and heights of plots with grid.arrange

I have three plots and I try to combine them with grid.arrange. The last plot should have a smaller height than the first two plots and all the plots should have the same width.

A working example:

p1 <- qplot(mpg, wt, data=mtcars)
p2 <- p1
p3 <- p1 + theme(axis.text.y=element_blank(), axis.title.y=element_blank())

grid.arrange(arrangeGrob(p1,p2, ncol=1, nrow=2),
         arrangeGrob(p3, ncol=1, nrow=1), heights=c(4,1)) 

enter image description here

Here, the last plot has a larger width than the first two. In my real data, even if I keep the text and the title on the y-axis, I still have a different width for the third plot.

I tried to add "widths":

 grid.arrange(arrangeGrob(p1,p2, ncol=1, nrow=2),
         arrangeGrob(p3, ncol=1, nrow=1), heights=c(4,1), widths=c(2,1))

But it turns into a two column plot...

enter image description here

I also tried another code:

p1 <- ggplotGrob(p1)
p2 <- ggplotGrob(p2)
p3 <- ggplotGrob(p3)
# 
stripT <- subset(p2$layout, grepl("spacer", p2$layout$name))
p3 <- p3[-stripT$t, ]

grid.draw(rbind(p1, p2, p3, size = "first")) 

I have the same widths but now I don't know how to change the heights...

enter image description here

Well, can someone help me to combine both the height and width aspects for a final plot?

like image 603
Chika Avatar asked Mar 24 '16 10:03

Chika


People also ask

How do you change to size of grid arrange in R?

To change the size of plots arranged using grid. arrange, we can use heights argument. The heights argument will have a vector equal to the number of plots that we want to arrange inside grid.

How do you make a grid plot in R?

Creating a Grid of Plots To do this, you use the parameter value mfrow=c(x,y) where x is the number of rows that you wish to have in your plot and y is the number of columns. When you plot, R will place each plot, in order by row within the grid that you define using mfrow .

What package is grid arrange in R?

grid. arrange() function sets up a gtable layout to place multiple grobs on a page. It is located in package "gridExtra".

How do I arrange multiple Ggplots?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.


2 Answers

Try plot_grid from the cowplot package:

library(ggplot2) library(gridExtra) library(cowplot) p1 <- qplot(mpg, wt, data=mtcars) p2 <- p1 p3 <- p1 + theme(axis.text.y=element_blank(), axis.title.y=element_blank()) plot_grid(p1, p2, p3, align = "v", nrow = 3, rel_heights = c(1/4, 1/4, 1/2)) 

enter image description here

like image 191
lukeA Avatar answered Sep 17 '22 13:09

lukeA


with gtable you need to set the heights of the panels manually,

g1 <- ggplotGrob(p1) g2 <- ggplotGrob(p2) g3 <- ggplotGrob(p3)  library(gridExtra) g <- rbind(g1, g2, g3)  set_panel_heights <- function(g, heights){   g$heights <- grid:::unit.list(g$heights) # hack until R 3.3 comes out   id_panels <- unique(g$layout[g$layout$name=="panel", "t"])   g$heights[id_panels] <- heights   g }  g <- set_panel_heights(g, lapply(1:3, grid::unit, "null")) grid::grid.draw(g)  

enter image description here

Although a bit verbose, this approach is more general than specifying relative heights: you can mix all sorts of grid units,

grid::grid.newpage() g <- do.call(rbind, replicate(3, ggplotGrob(ggplot()), simplify = FALSE)) g <- set_panel_heights(g, list(unit(1,"in"), unit(1,"line"), unit(1,"null"))) grid::grid.draw(g)  

enter image description here

like image 40
baptiste Avatar answered Sep 17 '22 13:09

baptiste