As it is a named
list, we can use imap
where the .y
returns the name
of the list
element and .x
the value of the element
library(ggplot2)
library(purrr)
out <- imap(mylist, ~
ggplot(.x, aes(x = X1, y = X2)) +
geom_point() +
ggtitle(.y)
)
With lapply
, an option is to either loop over the sequence of list
or the names
and then extract the list
element with [[
out <- lapply(names(mylist), function(nm)
ggplot(mylist[[nm]], aes(x = X1, y = X2)) +
geom_point() +
ggtitle(nm)
)
Or use Map
out <- Map(function(x, y)
ggplot(x, aes(x = X1, y = X2)) +
geom_point() +
ggtitle(y),
mylist, names(mylist))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…