我对 purrr::pmap 在nested.data.frame中制作多个ggplot图有一些疑问。
我可以使用 purrr::map2 毫无问题地运行下面的代码,并且可以在nested.data.frame中制作多重图(2个图)。
作为示例,我在 R 中使用了 iris 数据集。
library(tidyverse)
iris0 <- iris
iris0 <-
iris0 %>%
group_by(Species) %>%
nest() %>%
mutate(gg1 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Sepal.Width)) + geom_point())) %>%
mutate(gg2 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Petal.Width)) + geom_point())) %>%
mutate(g = purrr::map2(gg1, gg2, ~ gridExtra::grid.arrange(.x, .y)))
但是,当我想绘制 2 个以上的图时,我无法像下面的代码那样使用 purrr::pmap 来解决。
iris0 <-
iris0 %>%
group_by(Species) %>%
nest() %>%
mutate(gg1 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Sepal.Width)) + geom_point())) %>%
mutate(gg2 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Petal.Width)) + geom_point())) %>%
mutate(gg3 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Petal.Length)) + geom_point())) %>%
mutate(g = purrr::pmap(gg1, gg2,gg3, ~ gridExtra::grid.arrange(.x, .y, .z)))
> Error in mutate_impl(.data, dots) : Index 1 is not a length 1 vector
有没有办法解决nested.data.frame中的这个问题?
请给我一些建议或答案。