ggplot2: Define plot layout with grid.arrange() as argument of do.call()

岁酱吖の 提交于 2019-11-29 01:12:18

问题


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)

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)

回答1:


You can now do,

grid.arrange(p1,p2,p3,p4, layout_matrix = rbind(c(1,1,1),c(2,3,4)))


来源:https://stackoverflow.com/questions/30299529/ggplot2-define-plot-layout-with-grid-arrange-as-argument-of-do-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!