1

I'm currently using for loop to save my graphs to the working directory what ever it is. OTH, I want to add one more feature that creating the directory with the graph name and store corresponding figures to those folder. For instance if the graph name is setosa create folder named setosa and store setosa graph to inside to that new directory.

There is a one relative how-to-save-plots-inside-a-folder here but no solution is clearly given.

Here is my current working code for saving graphs to working directory.

library(ggplot2)
library(dplyr)

plot_list = list() # Initialize an empty list
     for (i in unique(iris$Species)) {
  p = ggplot(iris[iris$Species == i, ], aes(x=Sepal.Length, y=Sepal.Width)) +
     geom_point(size = 3, aes(colour = Species))
  plot_list[[i]] = p
}

for (i in unique(iris$Species)) {
  file_name = paste( i, ".tiff", sep="")
  tiff(file_name)
  print(plot_list[[i]])
  dev.off()
}
mnm
  • 1,962
  • 4
  • 19
  • 46
Alexander
  • 4,527
  • 5
  • 51
  • 98
  • 2
    See [Check existence of directory and create if doesn't exist](https://stackoverflow.com/questions/4216753/check-existence-of-directory-and-create-if-doesnt-exist) and [How to save a plot in R in a subdirectory of the working directory](https://stackoverflow.com/questions/27817546/how-to-save-a-plot-in-r-in-a-subdirectory-of-the-working-directory). – Henrik Oct 14 '17 at 19:22

1 Answers1

3

Replace file_name = ... with:

...
file_name = paste0("./", i, "/", i, ".tiff")
...
Brian
  • 7,900
  • 1
  • 27
  • 41