Multiple ggplots of different sizes

后端 未结 5 1702
醉酒成梦
醉酒成梦 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:19

    I appreciate all the other answers, but Didzis Elferts's comment on the OP connected to the answer that I found easiest to implement.

    library(ggplot2)
    p1 <- qplot(x=wt,y=mpg,geom="point",main="Scatterplot of wt vs. mpg", data=mtcars)
    p2 <- qplot(x=wt,y=disp,geom="point",main="Scatterplot of wt vs disp", data=mtcars)
    p3 <- qplot(wt,data=mtcars)
    p4 <- qplot(wt,mpg,data=mtcars,geom="boxplot")
    p5 <- qplot(wt,data=mtcars)
    p6 <- qplot(mpg,data=mtcars)
    p7 <- qplot(disp,data=mtcars)
    p8 <- qplot(disp, y=..density.., geom="density", data=mtcars)
    p9 <- qplot(mpg, y=..density.., geom="density", data=mtcars)
    
    vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y)
    
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(3, 5))) # 3 rows, 5 columns
    print(p1, vp = vplayout(1:2, 1:3))  # the big plot covers rows 1:2 and cols 1:3
    print(p2, vp = vplayout(1, 4))
    print(p3, vp = vplayout(1, 5))
    print(p4, vp = vplayout(2, 4))
    print(p5, vp = vplayout(2, 5))
    print(p6, vp = vplayout(3, 1))
    print(p7, vp = vplayout(3, 2))
    print(p8, vp = vplayout(3, 3))
    print(p9, vp = vplayout(3, 4:5))
    

提交回复
热议问题