When we use mapply in this scenario it will return the actual numbers that resulted from multiplying x
and y
:
mult.fun <- function(x, y) {
x*y
}
res <- mapply(mult.fun, seq(1,3), seq(1,3)) # gives > 1 4 9
Then we can access the results later on using the list index:
res[[1]] # --> 1 (or just res[1])
I want to do exaclty the same however instead of numbers I want to store ggplot2
objects:
plot.fun <- function(x, y) {
ggplot(data = NULL, aes(x,y)) + geom_point()
}
res <- mapply(plot.fun, seq(1,3), seq(1,3))
I thought this would simply store the results similar to the numbers, such that res[[1]]
would give me a ggplot2
object, but is returns this:
list()
attr(,"class")
[1] "waiver"
When I call the function directly it will give me a ggplot2
object:
class(plot.fun(1,1)) # --> "gg" "ggplot"
How can I call the plot function and store the actual plots in a list? And why doesn't mapply do this?