I would like to subset a large dataframe and create a ggplot of each grouping. Sounds like a perfect candidate for dplyr but I'm running into issues calling functions on the group_by
results. Any hints would be greatly appreciated.
# what I want to do using base functions: "groupby" the elements in a column
# and create/save a plot for each group
for (i in levels(iris$Species)){
df = iris[iris$Species == i,]
p <- ggplot(df, aes(x=Sepal.Length, y=Sepal.Width) + geom_point())
ggsave(p, filename=paste(i,".pdf",sep=""))
}
# I'm trying to get something like this using dplyr
library(dplyr)
iris %>%
group_by(Species) %>%
do({
p <- ggplot(., aes(x=Sepal.Length, y=Sepal.Width) + geom_point())
ggsave(p, filename=paste(quote(Species),".pdf",sep=""))
})
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…