3

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=""))
     })
zach
  • 29,475
  • 16
  • 67
  • 88
  • You say you are "running into issues." What exactly are these issues? It's more helpful if you give the exact problem/error message. – MrFlick Dec 18 '14 at 22:24

1 Answers1

10

Well, you have a parenthesis problem and a file naming problem so maybe it's one of those that you are referring to. I'm assuming

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

would fix your problem.

MrFlick
  • 195,160
  • 17
  • 277
  • 295