开发者

Loop in R to create and save series of ggplot2 plots with specified names

I have a data frame in R with POSIXct variable sessionstarttime. Each row is identified by integer ID variable of a specified location . Number of rows is different for each location. I plot overall graph simply by:

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

Is it possible to create a loop that will create and save such plot for each location separately?

Preferably with a file name the same as value of开发者_如何学C ID variable?

And preferably with the same time scale for each plot?


Not entirely sure what you're asking but you can do one of two things.

a) You can save each individual plot in a loop with a unique name based on ID like so:

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) Just assign each plot to an element of a list. Then write that list to disk using save That would make it very easy to load and access any individual plot at a later time.


I am not sure if I get what you want to do. From what I guess, i suggest to write a simple function that saves the plot. and then use lapply(yourdata,yourfunction,...) . Since lapply can be used for lists, it´s not necessary that the number of rows is equal.

HTH

use something like this in your function:

    ggsave(filename,scale=1.5)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜