How to use purrr::pmap to plot multiple ggplot in nested.data.frame

风流意气都作罢 提交于 2019-12-03 06:09:10

purrr::pmap takes (at least) two arguments:

 pmap(.l, .f, ...)

where

  .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.

Further, .x and .y work well for only two arguments, but (in the same man page) it says For more arguments, use '..1', '..2', '..3' etc.

For readability (and a little efficiency), I'll combine all of the individual calls to mutate into one; you can keep them separate if needed (esp if there is more to the code than you show in this reduced example):

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))
  )
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!