[R] Generate and store multiple plots

Jim Lemon jim at bitwrit.com.au
Mon Jun 11 06:18:08 CEST 2012


On 06/11/2012 11:47 AM, analyst41 at hotmail.com wrote:
> I have a data set whose rows look like
>
> Item date variable_1 variable_2 variable_3 variable_4
>
>
> Different items may occur over different dates.
>
> During any single study, I might select a subset of the four variables
> or some function of them to be plotted against time (date).
>
> For each item, I would select a date range and I want a plot of the
> selected variables over that range for that item.
>
> I need a method that would do this at one shot and put the plot
> objects out to disk, one for each item.
>
Hi analyst41,
You probably want to write a custom function something like this:

plot_varying<-function(x,which_vars=3:6,which_item=NA,
  start_date,end_date) {

  # open an output device with an unique name
  png(paste(which_item,format(start_date,"%d%m%Y"),
   format(end_date,"%d%m%Y"),".png",sep="_")
  # get the plot dimensions
  ylim<-range(x[x$item==which_item,which_vars[1]])
  # plot all the requested variables
  matplot(x[x$item==which_item,which_vars],ylim=ylim,...)
  dev.off()
}

Then call your function for each item, variables and dates.

Jim



More information about the R-help mailing list