如何使用 purrr::pmap 在 nested.data.frame 中绘制多个 ggplot
How to use purrr::pmap to plot multiple ggplot in nested.data.frame
我有一些关于 purrr::pmap 在 nested.data.frame 中制作多个 ggplot 图的问题。
我可以 运行 通过使用 purrr::map2 来毫无问题地编写代码,并且我可以在 nested.data.frame 中制作多图(2 个图)。
例如,我在 R 中使用了鸢尾花数据集。
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有没有办法解决这个问题?
请给我一些建议或答案。
purrr::pmap
需要(至少)两个参数:
pmap(.l, .f, ...)
哪里
.l: A list of lists. The length of '.l' determines the number of
arguments that '.f' will be called with. List names will be
used if present.
.f: A function, formula, or atomic vector.
此外,.x
和 .y
仅适用于两个参数,但(在同一个手册页中)它说 For more arguments, use '..1', '..2', '..3' etc
.
为了可读性(以及一点效率),我将把对 mutate
的所有单独调用合并为一个;如果需要,您可以将它们分开(特别是如果代码比您在此简化示例中显示的更多):
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)
iris0 <- iris %>%
group_by(Species) %>%
nest() %>%
mutate(
gg1 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Sepal.Width)) + geom_point()),
gg2 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Petal.Width)) + geom_point()),
gg3 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Petal.Length)) + geom_point()),
g = purrr::pmap(list(gg1, gg2, gg3), ~ gridExtra::grid.arrange(..1, ..2, ..3))
)
我有一些关于 purrr::pmap 在 nested.data.frame 中制作多个 ggplot 图的问题。
我可以 运行 通过使用 purrr::map2 来毫无问题地编写代码,并且我可以在 nested.data.frame 中制作多图(2 个图)。
例如,我在 R 中使用了鸢尾花数据集。
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有没有办法解决这个问题? 请给我一些建议或答案。
purrr::pmap
需要(至少)两个参数:
pmap(.l, .f, ...)
哪里
.l: A list of lists. The length of '.l' determines the number of
arguments that '.f' will be called with. List names will be
used if present.
.f: A function, formula, or atomic vector.
此外,.x
和 .y
仅适用于两个参数,但(在同一个手册页中)它说 For more arguments, use '..1', '..2', '..3' etc
.
为了可读性(以及一点效率),我将把对 mutate
的所有单独调用合并为一个;如果需要,您可以将它们分开(特别是如果代码比您在此简化示例中显示的更多):
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)
iris0 <- iris %>%
group_by(Species) %>%
nest() %>%
mutate(
gg1 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Sepal.Width)) + geom_point()),
gg2 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Petal.Width)) + geom_point()),
gg3 = purrr::map(data, ~ ggplot(., aes(Sepal.Length, Petal.Length)) + geom_point()),
g = purrr::pmap(list(gg1, gg2, gg3), ~ gridExtra::grid.arrange(..1, ..2, ..3))
)