【问题标题】:ggplot2 - Correctly arrange odd number of plots into one figureggplot2 - 正确地将奇数个图排列成一个图形
【发布时间】:2023-03-29 00:44:01
【问题描述】:

我有奇数个图要排列成一个图,我希望在图的最后一行居中显示最后一个图。

这里有一些示例数据:

library(ggplot2)
set.seed(99)

x_1 = data.frame(z = rnorm(100))
x_2 = data.frame(z = rnorm(100))
x_3 = data.frame(z = rnorm(100))

lst = list(x_1, x_2, x_3)

lst_p = list()

for (i in 1:length(lst)) {
    lst_p[[i]] = ggplot(data=lst[[i]], aes(lst[[i]]$z)) + 
    geom_histogram() +
        xlab("X LAB") +
        ylab("Y LAB") 
}

p_no_labels = lapply(lst_p, function(x) x + xlab("") + ylab(""))

title = cowplot::ggdraw() + cowplot::draw_label("test", size = 20)

p_grid = cowplot::plot_grid(plotlist = p_no_labels, ncol = 2)

print(cowplot::plot_grid(title, p_grid, 
                         ncol = 1, rel_heights = c(0.05, 1, 0.05)))

我希望第三个图位于图的中心。 我正在使用cowplot

有什么建议吗? 谢谢

【问题讨论】:

    标签: r ggplot2 cowplot


    【解决方案1】:

    您可以为此使用gridExtra::grid.arrange。您需要做的就是使用布局矩阵指定绘图的布局。

    library(ggplot2)
    set.seed(99)
    
    x_1 = data.frame(z = rnorm(100))
    x_2 = data.frame(z = rnorm(100))
    x_3 = data.frame(z = rnorm(100))
    
    lst = list(x_1, x_2, x_3)
    
    lst_p = list()
    
    for (i in 1:length(lst)) {
    lst_p[[i]] = ggplot(data=lst[[i]], aes(lst[[i]]$z)) + 
    geom_histogram() +
    xlab("X LAB") +
    ylab("Y LAB") 
    }
    
    p_no_labels = lapply(lst_p, function(x) x + xlab("") + ylab(""))
    
    
    #layout of plots
    
    lay <- rbind(c(1,2),c(1,2),
                  c(3,3))
    
    
    
    #arrange the grid and specify the `layout_matrix`
    gridExtra::grid.arrange(grobs=p_no_labels, layout_matrix=lay)
    

    【讨论】:

      【解决方案2】:

      一个不太优雅的解决方案是将第一行的中间图留空:

      p_no_labels2<-list(p_no_labels[[1]],NULL,p_no_labels[[2]],NULL,p_no_labels[[3]])
      p_grid = cowplot::plot_grid(plotlist = p_no_labels2, ncol = 3) 
      

      【讨论】:

        【解决方案3】:

        使用嵌套的cowplot::plot_grids:

        library(ggplot2)
        set.seed(99)
        
        x_1 = data.frame(z = rnorm(100))
        x_2 = data.frame(z = rnorm(100))
        x_3 = data.frame(z = rnorm(100))
        
        lst = list(x_1, x_2, x_3)
        
        lst_p = list()
        
        for (i in 1:length(lst)) {
            lst_p[[i]] = ggplot(data=lst[[i]], aes(lst[[i]]$z)) + 
            geom_histogram() +
                xlab("X LAB") +
                ylab("Y LAB") 
        }
        
        
        p_no_labels = lapply(lst_p, function(x) x + xlab("") + ylab(""))
        
        title = cowplot::ggdraw() + cowplot::draw_label("test", size = 20)
        top_row = cowplot::plot_grid(p_no_labels[[1]], p_no_labels[[2]], ncol=2)
        bottom_row = cowplot::plot_grid(NULL, p_no_labels[[2]], NULL, ncol=3, rel_widths=c(0.25,0.5,0.25))
        
        cowplot::plot_grid(title, top_row, bottom_row, ncol=1, rel_heights=c(0.1,1,1))
        

        【讨论】:

          【解决方案4】:

          这使用 gridgridExtra 将绘图放置在 2×4 矩阵布局上。您的每个地块都占用两个“插槽”。底行的两个外部“插槽”被绘制为 NULL grobs 以使您的绘图居中。

          # 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))
          

          【讨论】:

            猜你喜欢
            • 2020-10-06
            • 2011-08-29
            • 2013-10-01
            • 2023-03-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-26
            • 2014-04-23
            相关资源
            最近更新 更多