How to perfectly align an unequal number of plots (ggplot2,gridExtra)

后端 未结 2 1383
北恋
北恋 2021-02-04 11:34

I would like to perfectly align these plots :

Here is the R code :

library(tidyverse)
library(gridExtra)
groupes <- tribble(~type, ~group, ~p         


        
2条回答
  •  面向向阳花
    2021-02-04 12:10

    it's probably more manageable if you can set an absolute panel size (and size the device accordingly).

    justify <- function(x, hjust="center", vjust="center"){
      w <- sum(x$widths)
      h <- sum(x$heights)
      xj <- switch(hjust,
                   center = 0.5,
                   left = 0.5*w,
                   right=unit(1,"npc") - 0.5*w)
      yj <- switch(vjust,
                   center = 0.5,
                   bottom = 0.5*h,
                   top=unit(1,"npc") - 0.5*h)
      x$vp <- viewport(x=xj, y=yj)
      return(x)
    }
    
    library(grid)
    gl <- lapply(list(p1,p2,p3),egg::set_panel_size, height=unit(1,"in"))
    gl <- lapply(gl, justify, vjust="top")
    gridExtra::grid.arrange(grobs=gl, nrow=1)
    

提交回复
热议问题