It's often convenient to make large nested lists to keep track of plots created with ggplot2
.
For example, here's how I might store a handful of plots in a large list of plots, with sublists for topics and sub-topics.
summary_plots
$Demographics
Demographics$Socioeconomic$Household_Income_Plot
Demographics$Socioeconomic$Education_Plot
Demographics$Age_Plot
$Product_Usage
Purchase_Frequency_Plot
- ....
How can I extract all of the ggplot2
plot objects from this entire list? I'd like to be able to create a 'flat', one-level list containing all of the plots that were contained in the original list.
Here's a quick example list:
generic_plot <- ggplot(mtcars) + geom_point(aes(x = wt, y = mpg))
summary_plots <- list()
summary_plots$Demographics$Socioeconomic$Income <- generic_plot
summary_plots$Demographics$Socioeconomic$Education <- generic_plot
summary_plots$Demographics$Age <- generic_plot
summary_plots$Product_Usage$Purchase_Frequency <- generic_plot
The desired result would be the equivalent of creating a list like the following:
list('Demographics.Socioeconomic.Income' = generic_plot,
'Demographics.Socioeconomic.Education' = generic_plot,
...)