[R] How to open more windows to make more graphs at once!

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Mar 7 10:25:08 CET 2007


On Wed, 2007-03-07 at 09:39 +0100, Faramarzi Monireh wrote:
> Dear R users,
> I have a data frame (test) including five columns of upper (numeric),
> lower (numeric), observed (numeric), best_sim (numeric) and stname
> (factor with 80 levels, each level with different length). Now I would
> like to write a short program to draw one graph as follow for each
> level of stname but I would like also to draw each time 12 graphs for
> the 12 levels of stname in the same graphic windows and save it as
> "jpeg' file . This means at the end I will have 7 (80 levels/12=7)
> graphic windows and 7 jpeg files each one with 12 graphs (the last one
> with 8 graphs) for the 12 levels of stname. I already wrote the
> following script to do it each time for 12 levels of stname but I have
> to change script each time for the another 12 levels [line 3 in the
> script for example: for( i in levels(test$stname)[12:24))] and I do
> not know how can I save the obtained graphs (seven graphic windows) as
> jpeg files (e.g. plot1.jpeg, plot2.jpeg and so on). As I have 45
> dataset like this it would be gr!
>  eat if somebody can help me to complete this script to do all
> together for a dataset using a script.
> Thank you very much in advance for your cooperation,
> Monireh
> 

Hi Monireh,

I don't have your data set so I have generated some random data to
illustrate one approach to this.

## generate some data 
set.seed(1234)
dat <- data.frame(upper = rnorm(100), lower = rnorm(100), 
                  observed = rnorm(100), best_sim = rnorm(100), 
                  stname = factor(gl(5, 20), labels = letters[1:5]))

## because this is going to be called 45 times, I've wrapped it in a
## function, foo()
## Note the filename arg. It contains "%03d" which means that R will
## insert a number and produce many jpegs, varying by this number
## e.g. myplot1.jpeg, myplot2.jpeg - see ?jpeg.
## the "..." allow passing of arguments to jpeg
foo <- function(x, filename = "Rplot%03d.jpeg", ...) {
   ## start the jpeg device
   jpeg(filename = filename, ...)
   ## store the parameter defaults and set a 2 by 2 plot regions
   opar <- par(mfrow = c(2,2))
   ## this insures that the device is closed and defaults restored on
   ## function exit
   on.exit({dev.off(); par(opar)})
   ## set up a loop to go over the levels of your factor
   for(i in levels(x$stname)) {
      ## do the plotting - here you need to add the plot commands
      ## you really want to use - these are just examples.
      plot(lower ~ upper, data = x, subset = stname == i)
      ## this just adds a lowess line, I use with() to make it easier
      ## to read.
      with(x, lines(lowess(upper[stname == i], lower[stname == i]), 
           col = "red"))
   }
   invisible()
}

## to use the function on the demo data
## uses default filename
foo(dat)

## or passing arguments to jpeg()
foo(dat, width = 600, height = 600, pointsize = 10)

## or using your own file name
foo(dat, filename = "dataset1_%03d.jpeg", width = 600, height = 600,
pointsize = 10)

See ?jpeg to see why this works - the filename with "%03d" allows R to
produce several jpegs.

>       
> windows(9,9)
> par(mfrow = c(3,4))
> for( i in levels(test$stname)[1:12])
> { 
> data<- test[test$stname==i,]
> xx <- c(1:length(data$upper), length(data$upper):1)
> yy <- c(data$upper, rev(data$lower))
> zz<- data$observed
> tt<- data$Best_Sim
> par(lab =c(10,15,2))

In the line below, where you set the x- and y-limits, it would be
simpler and more readable to use range() instead of c(min(x), max(x) -
so your plot call could be:

plot.jpeg<- plot(xx,yy, type="n", xlim= range(xx),              
                 ylim=range(zz,yy,tt)*1.4), main= i, 
                 xlab="Month (1990-2002)",  
                 ylab="Discharge(m3/s)", 
                 font.axis=6)

Also, you can format the y-label more nicely with:

ylab = expression(paste("Discharge (", m^-3 * s^{-1}, ")"))

HTH

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT                          [w] http://www.freshwaters.org.uk/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list