Multiple ggplots of different sizes

后端 未结 5 1701
醉酒成梦
醉酒成梦 2020-12-02 12:35

It\'s relatively simple using grid.arrange in the gridExtra package to arrange multiple plots in a matrix, but how can you arrange plots (the ones

5条回答
  •  生来不讨喜
    2020-12-02 13:01

    You can use the same matrix interface as layout with grid.arrange,

    library(gridExtra)
    library(grid)
    gl <- lapply(1:9, function(ii) grobTree(rectGrob(), textGrob(ii)))
    
    grid.arrange(grobs = gl, layout_matrix = rbind(c(1,1,1,2,3),
                                                   c(1,1,1,4,5),
                                                   c(6,7,8,9,9)))
    

    enter image description here

    and the same works for ggplots; note that NA can be used to indicate blank cells. The result is a gtable, compatible with ggsave().

    gl <- replicate(9, ggplot(), FALSE)
    grid.arrange(grobs = gl, layout_matrix = rbind(c(1,1,1,2,3),
                                                   c(1,1,1,4,5),
                                                   c(6,7,8,NA,9)))
    

提交回复
热议问题