Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

purrr

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

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

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

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

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

like image 795
Lc_decg Avatar asked Nov 25 '17 03:11

Lc_decg


People also ask

How do I plot multiple Ggplots together?

Combine the plots over multiple pagesThe function ggarrange() [ggpubr] provides a convenient solution to arrange multiple ggplots over multiple pages. After specifying the arguments nrow and ncol, ggarrange()` computes automatically the number of pages required to hold the list of the plots.

Which function from the gridExtra package can be used to arrange multiple different kinds of ggplot objects on a single figure?

The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page. marrangeGrob() for arranging multiple ggplots over multiple pages.

What is the name of the function in ggplot2 that you use to start every plot by creating a coordinate system to which laters may be added?

The base function ggplot() is responsible for creating the coordinate system in which the plot will be display.

How do I save a loop in ggplot2?

To save multiple ggplots using for loop, you need to call the function print() explicitly to plot a ggplot to a device such as PDF, PNG, JPG file. Enjoyed this article?


1 Answers

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))
  )
like image 123
r2evans Avatar answered Sep 27 '22 17:09

r2evans