是否可以将列表通过管道传输到 ggsave

Is it possible to pipe a list into ggsave

这有效,但是需要创建一个变量 (p):

library(tidyverse)
library(gridExtra)

p <- mtcars %>% 
  split(.$cyl) %>% 
    map(~.x %>% ggplot(aes(x=hp,y=qsec)) + geom_point()) 
  
ggsave(filename = "myPlot.pdf", 
       device = "pdf",
       plot = marrangeGrob(p, nrow=1, ncol=1), 
       width = 15, height = 9) 

我想做的是这样的(下面的代码给出了错误):

mtcars %>% 
  split(.$cyl) %>% 
  map(~.x %>% ggplot(aes(x=hp,y=qsec)) + geom_point()) %>% 
    ggsave(filename = "myPlot.pdf", 
           device = "pdf",
           plot = marrangeGrob(., nrow=1, ncol=1), 
           width = 15, height = 9)

Thanks!

您可以在最后一个代码块周围添加 {..} -

library(tidyverse)
library(gridExtra)

mtcars %>% 
  split(.$cyl) %>% 
  map(~.x %>% ggplot(aes(x=hp,y=qsec)) + geom_point()) %>% 
  {  ggsave(filename = "myPlot.pdf", 
         device = "pdf",
         plot = marrangeGrob(., nrow=1, ncol=1), 
         width = 15, height = 9)
  }