ogawa ogawa - 26 days ago 8
R Question

use ggplot() every group applyed group_by()

I use

facet_wrap
to every group plot,
but I need to save every plot individual and find the under link.

I tried program the answer in the URL link and can save PDFfile,
but present the ERROR MASSAGE.

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"))
})


ERROR MESSAGE:

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


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

Answer

we can make do have a point (or just any data.frame for that matter)

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(.)
  })