20

I want to obtained an unbalanced grid of plots such as

require(ggplot2)
require(gridExtra)

df <- data.frame(value1 = rnorm(200),
                 value2 = rnorm(200),
                 value3 = rnorm(200),
                 value4 = rnorm(200))

p1 <- ggplot(df) + geom_density(aes(x=value1))
p2 <- ggplot(df) + geom_density(aes(x=value2))
p3 <- ggplot(df) + geom_density(aes(x=value3))
p4 <- ggplot(df) + geom_density(aes(x=value4))

grid.arrange(p1, arrangeGrob(p2,p3,p4, ncol=3), heights=c(2.5/4, 1.5/4), ncol=1)

enter image description here

but using a function

myplot <- function(i){
  p <- ggplot(df) + geom_density(aes_string(x=i))
  return(p)
}

and an lapply call

p <- lapply(c("value1","value2","value3","value4"), myplot)
do.call(grid.arrange, c(p))

In this case grid.arrange distribute the plots in a 2 by 2 matrix. But I want to obtain an unbalanced layout as with

grid.arrange(p1, arrangeGrob(p2,p3,p4, ncol=3), heights=c(2.5/4, 1.5/4), ncol=1)
5
  • My question is different because in my case the plots are unbalanced. I can't simply pass a list to do.call because I want different heights for different plots (see figure) Commented May 18, 2015 at 9:42
  • So, you want to define a layout. Please adjust your question title and body accordingly. Commented May 18, 2015 at 9:45
  • Did some edits. Not sure whether it's OK, not sure exactly what you mean by layout. Commented May 18, 2015 at 9:48
  • Maybe you can use this: stackoverflow.com/a/18428858/1412059 Commented May 18, 2015 at 9:49
  • Nested grid.arrange/arrangeGrob calls define a layout. You can't achieve that by just passing heights. Commented May 18, 2015 at 9:50

1 Answer 1

35

You can now do,

grid.arrange(p1,p2,p3,p4, layout_matrix = rbind(c(1,1,1),c(2,3,4)))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.