如何使用purrr :: pmap在nested.data.frame中绘制多个ggplot [英] How to use purrr::pmap to plot multiple ggplot in nested.data.frame

查看:81
本文介绍了如何使用purrr :: pmap在nested.data.frame中绘制多个ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对purrr :: pmap有一些疑问,以便在nested.data.frame中绘制ggplot的多个图.

I have some questions about purrr::pmap to make multiple plots of ggplot in nested.data.frame.

我可以使用purrr :: map2在代码下面运行,并且可以在nested.data.frame中制作多图(2个图).

I can run below code without problem by using purrr::map2 and I can make multiplots(2 plots) in nested.data.frame.

例如,我在R中使用了虹膜数据集.

As a example, I used the iris dataset in 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)))

但是,当我要绘制两个以上的图时,我无法像下面的代码一样使用purrr :: pmap来解决问题.

But, when I want to plot more than 2 plots, I can't solve using purrr::pmap like below code.

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中是否有解决此问题的方法? 请给我一些建议或答案.

Is there is a way of solving this problem in nested.data.frame ? Please give me some advices or answers.

推荐答案

purrr::pmap接受(至少)两个参数:

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

 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.

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.

为了提高可读性(并提高效率),我将对mutate的所有单个调用合并为一个;您可以根据需要将它们分开(特别是如果代码比在此简化示例中显示的更多):

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

这篇关于如何使用purrr :: pmap在nested.data.frame中绘制多个ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆