ggplot2 - Correctly arrange odd number of plots into one figure

后端 未结 4 1641
攒了一身酷
攒了一身酷 2020-12-19 11:42

I have an odd number of plots to arrange into one figure and I desire to show the last plot centered in the last row of the figure.

Here some sample data:

         


        
4条回答
  •  Happy的楠姐
    2020-12-19 11:54

    This uses grid and gridExtra to place the plots on a 2×4 matrix layout. Each of your plots occupies two "slots". The two outer "slots" on the bottom row are plotted as NULL grobs to centre your plot.

    # Convert to grobs
    lst_p <- lapply(lst_p, ggplotGrob)
    
    # Plot using gridExtra and grid
    gridExtra::grid.arrange(lst_p[[1]], lst_p[[2]], grid::nullGrob(), lst_p[[3]], grid::nullGrob(),
                            layout_matrix = matrix(c(1,1,2,2,3,4,4,5), byrow = TRUE, ncol = 4))
    

提交回复
热议问题