I need to change all the labels (titles/subtitles) from a script that creates close to 100 hundred plots -- all different. Instead of changing the script code I'd like to loop through all plots and add a new labs function. This way nothing is altered from the original. To do this I need to get all ggplot2 objects into list.
My question is: How can one store all ggplot2 objects in the environment into a list?
The code below should create eleven ggplot2 plots and store them in the environment (want to do the reverse .. get them into a list from the environment).
library(tidyverse)
# Make a lot of ggplots using mtcars
all_plots <- list()
all_plots <- map(names(mtcars),
possibly(function(x){
mtcars %>%
ggplot(aes(mpg, x))+
geom_point()
}, NULL)) %>%
set_names(names(mtcars))
# function to add all ggplots to the environemnt
list_environment <- function(x) {
if(is.list(x)) {
list2env(x, envir = .GlobalEnv)
lapply(x, test)
}
}
list_environment(all_plots)
ls()