After creating a plot matrix using GGally::ggpairs()
, I would like to store the individual scatter plots for later use.
Here is my current code:
# load necessary package
library(GGally) # loads `ggplot2`
library(magrittr) # allows for the use of `%>%`
# create a matrix of plots
mtcars %>%
na.omit() %>%
ggpairs(columns = 1:7)
# how do I automate this process?
P1 <- ggplot(aes(x = disp, y = hp)) +
geom_point()
P2 <- ggplot(aes(x = drat, y = hp)) +
geom_point()
P3 <- ggplot(aes(x = hp, y = qsec)) +
geom_point()
I'm getting an error that says the data must be a dataframe. I tried to specify the data from the na.omit()
pipe using a .
but I received the same result.
Any advice is appreciated!
Overview
I condensed all the separate
ggplot(...)
calls into one custom function:ScatterPlot()
.Then I created another custom function
ManyScatterPlots()
- which usespurrr::map()
- that stores the individual scatter plot for each particular column indf
on the x-axis and every column on the y-axis in a list. This process repeats itself for every column indf
.The result from
ManyScatterPlots()
is a list of lists, where each individual list contains many scatter plots. I've labeled both the list of lists and the individual plots to make it easier to find what you're looking for later on.