应用一个函数在 ggplots 列表上创建 cowplots,每次有 4 个图

apply a function for creating cowplots on a list of ggplots with 4 plots each time

抱歉,我认为这是一个愚蠢的问题。

我刚刚创建了一个函数,该函数 return 整个 sf 数据帧(点)的每个分组变量的点映射(具有 geom_sf)。

最后我得到了一个 ggplot 对象列表。

现在我想在网格中以 4 乘 4 的比例绘制结果。

这是一个更简单的可重现示例

## I have a df

library(ggplot2)
library(cowplot)
df <- data.frame(V1 = runif(100, 0.0, 1.0), 
                 V2 = runif(100, 0.0, 1.0),
                 V3 = runif(100, 0.0, 1.0),
                 V4 = runif(100, 0.0, 1.0),
                 V5 = runif(100, 0.0, 1.0),
                 V6 = runif(100, 0.0, 1.0),
                 V7 = runif(100, 0.0, 1.0),
                 V8 = runif(100, 0.0, 1.0))

## I've created a function to produce several ggplot from this df
# for this example, let's say it is a list of histograms

plot_data_column <- function (data, column) {
  ggplot(data, aes_string(x = column)) +
    geom_histogram(fill = "lightgreen") +
    xlab(column)
}

myplots <- lapply(colnames(df), plot_data_column, data = df)

## Now i want to create a function that make grids of 4 plots each times
# Something like that (below) but working on the 4 first element of the list, 
# then the 4 next and so on until the last items on the list, automaticaly

Gridplot <- function(myplots){
  Grid <- cowplot::plot_grid(plotlist = myplots)
  return(Grid)
}

Grid1 <- Gridplot(myplots[1:4])

我该怎么做?我敢肯定这很简单

您需要将 myplots 列表分成大小为 4 的块。 您可以使用 split(myplots, ceiling(seq_along(myplots)/n)) 来做到这一点,其中 n 是块的大小(根据答案 here)。

所以你可以这样做:

## I have a df

library(ggplot2)
library(cowplot)
#> 
#> ********************************************************
#> Note: As of version 1.0.0, cowplot does not change the
#>   default ggplot2 theme anymore. To recover the previous
#>   behavior, execute:
#>   theme_set(theme_cowplot())
#> ********************************************************
df <- data.frame(V1 = runif(100, 0.0, 1.0), 
                 V2 = runif(100, 0.0, 1.0),
                 V3 = runif(100, 0.0, 1.0),
                 V4 = runif(100, 0.0, 1.0),
                 V5 = runif(100, 0.0, 1.0),
                 V6 = runif(100, 0.0, 1.0),
                 V7 = runif(100, 0.0, 1.0),
                 V8 = runif(100, 0.0, 1.0))

## I've created a function to produce several ggplot from this df
# for this example, let's say it is a list of histograms

plot_data_column <- function (data, column) {
  ggplot(data, aes_string(x = column)) +
    geom_histogram(fill = "lightgreen") +
    xlab(column)
}

myplots <- lapply(colnames(df), plot_data_column, data = df)

## Now i want to create a function that make grids of 4 plots each times
# Something like that (below) but working on the 4 first element of the list, 
# then the 4 next and so on until the last items on the list, automaticaly

Gridplot <- function(myplots, n){

  splitted_plots <- split(myplots, ceiling(seq_along(myplots)/n))

  lapply(splitted_plots, function(x) plot_grid(plotlist = x))

  # Grid <- cowplot::plot_grid(plotlist = myplots)
  # return(Grid)
}

Grid_list <- Gridplot(myplots, 4)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Grid_list[[1]]

Grid_list[[2]]

reprex package (v0.3.0)

于 2019-11-24 创建

请注意,使用此解决方案,如果您想在每个网格中绘制更多图,只需更改 Gridplot 函数中的 n