Left align two graph edges (ggplot)

前端 未结 9 1304
梦谈多话
梦谈多话 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:32

    I wanted to generalize this for any number of plots. Here is a step-by-step solution using the approach by Baptiste:

    plots <- list(A, B, C, D)
    grobs <- list()
    widths <- list()
    

    collect the widths for each grob of each plot

    for (i in 1:length(plots)){
        grobs[[i]] <- ggplotGrob(plots[[i]])
        widths[[i]] <- grobs[[i]]$widths[2:5]
    }
    

    use do.call to get the max width

    maxwidth <- do.call(grid::unit.pmax, widths)
    

    asign the max width to each grob

    for (i in 1:length(grobs)){
         grobs[[i]]$widths[2:5] <- as.list(maxwidth)
    }
    

    plot

    do.call("grid.arrange", c(grobs, ncol = 1))
    

提交回复
热议问题