Sorry, I think it's a dumb question.
I've just created a function that return a map (with geom_sf) of points for each grouping variable of a whole sf dataframe (points).
At the end I get a list of ggplot objects.
Now I want to plot the results 4 by 4, in a grid.
Here a simpler reproducible example
## 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])
How can I do that ? I'm sure it's quite simple