Loop in R to create and save a series of ggplot2 graphs with the specified names

I have a data frame in R with the POSIXct sessionstarttime variable. Each line is identified by an integer identifier variable of the specified location. The number of rows is different for each location. I draw a general graph simply:

myplot <- ggplot(bigMAC, aes(x = sessionstarttime)) + geom_freqpoly()

Is it possible to create a cycle that will create and save such a schedule for each location separately?
Preferably if the file name matches the value of the ID variable?
And preferably with the same timeline for each chart?

+3
source share
2 answers

, , .

a) , ID, :

ggsave(myplot,filename=paste("myplot",ID,".png",sep="")) # ID will be the unique identifier. and change the extension from .png to whatever you like (eps, pdf etc).

b) . , save .

+7

, , . , , , . lapply (yourdata, yourfunction,...). lapply , , .

- :

    ggsave(filename,scale=1.5)
+1

Source: https://habr.com/ru/post/1753427/


All Articles