Arrange ggplots together in custom ratios and spacing

前端 未结 2 1611
走了就别回头了
走了就别回头了 2020-12-30 10:27

I am trying to combine n number of barplots with one common label plot at the bottom. My problem is that grid.arrange combines the two plots in 50%-50%. I am looking for som

相关标签:
2条回答
  • 2020-12-30 10:54
    grid.arrange(plot1, plot2, widths=c(0.7, 0.3), ncol=2)
    

    sorry for your example:

    grid.arrange(plot1, plot2, heights=c(0.7, 0.3), nrow=2)
    

    EDIT - for spacing between plots:

    blank<-rectGrob(gp=gpar(col="white")) # make a white spacer grob
    grid.arrange(plot1, blank, plot2, heights=c(0.7, 0.05, 0.25), nrow=3)
    
    0 讨论(0)
  • 2020-12-30 10:58

    grid.arrange() won't let you align the panels properly, it's better to work with gtable.

    require(gtable)
    
    g1 <- ggplotGrob(plot1)
    g2 <- ggplotGrob(plot2)
    ## add dummy column for missing strips
    g2 <- gtable_add_cols(g2, unit(0,"mm"))
    ## merge, basing widths on g1
    g <- gtable:::rbind_gtable(g1, g2, "first")
    ## add spacing
    g <- gtable_add_rows(g, unit(1,"cm"), pos=nrow(g1))
    grid.newpage()
    grid.draw(g)
    

    enter image description here

    Note that the panel heights are equal by default with this strategy. That's because they appear as unit(1, "null") in the layout (g$heights). It's trivial to edit g$heights if another ratio is required.

    0 讨论(0)
提交回复
热议问题