2016-11-14 114 views
2

我使用facet_wrap到每个组图, ,但我需要保存每个情节个体并找到下面的链接。使用ggplot()每组申请group_by()

我试图在URL链接中编程答案,并可以保存PDF文件, 但提出了错误的按摩。

CODE:

iris %>% group_by(Species) %>% 
    do({ 
    p <- ggplot(., aes(x =Sepal.Length, y = Petal.Length)) + geom_point() 
    ggsave(p, filename = paste0("fig/", unique(.$Species), ".pdf")) 
    }) 

错误消息:

Results are not data frames at positions: 1, 2, 3

网址: applying a function to the output of dplyr's group_by

+0

'do'想要你返回一个data.frame – baptiste

+2

也许你想'library(tidyverse); iris%>% split(。$ Species)%>%map(〜ggplot(。,aes(x = (。),〜ggsave(.x,filename = paste0(.y,“.pdf”)))'' – lukeA

回答

2

我们可以do有一个点(或只是任何为此事data.frame )

iris %>% group_by(Species) %>% 
    do({ 
    p <- ggplot(., aes(x =Sepal.Length, y = Petal.Length)) + geom_point() 
    ggsave(p, filename = paste0("fig", unique(.$Species), ".pdf")) 
    invisible(.) 
    })