Left align two graph edges (ggplot)

前端 未结 9 1303
梦谈多话
梦谈多话 2020-11-22 01:08

I\'m using ggplot and have two graphs that I want to display on top of each other. I used grid.arrange from gridExtra to stack them. The problem is I want the

9条回答
  •  逝去的感伤
    2020-11-22 01:44

    Try this,

     gA <- ggplotGrob(A)
     gB <- ggplotGrob(B)
     maxWidth = grid::unit.pmax(gA$widths[2:5], gB$widths[2:5])
     gA$widths[2:5] <- as.list(maxWidth)
     gB$widths[2:5] <- as.list(maxWidth)
     grid.arrange(gA, gB, ncol=1)
    

    Edit

    Here's a more general solution (works with any number of plots) using a modified version of rbind.gtable included in gridExtra

    gA <- ggplotGrob(A)
    gB <- ggplotGrob(B)
    grid::grid.newpage()
    grid::grid.draw(rbind(gA, gB))
    

提交回复
热议问题