r语言 - 是否可以将列表管道到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)
}

相关内容

  • 没有找到相关文章

最新更新