[R] png misbehaving with levelplot when inside a loop

David dpleydel at univ-fcomte.fr
Fri May 2 19:14:30 CEST 2008


I want to use spplot inside a loop  to itteratively produce png files.


for (i in 1:5){
  png(file=paste("myPlot",i,".png",sep=""),bg="white",height=500,width=500)
  library(lattice)
  trellis.par.set(sp.theme()) # sets bpy.colors() ramp
  data(meuse)
  coordinates(meuse) <- ~x+y
  l2 = list("SpatialPolygonsRescale", layout.north.arrow(), offset = c(181300,329800), scale = 400)
  l3 = list("SpatialPolygonsRescale", layout.scale.bar(), offset = c(180500,329800),
    scale = 500, fill=c("transparent","black"))
  l4 = list("sp.text", c(180500,329900), "0")
  l5 = list("sp.text", c(181000,329900), "500 m")
  spplot(meuse, c("ffreq"), sp.layout=list(l2,l3,l4,l5),col.regions="black",pch=c(1,2,3),
         key.space=list(x=0.1,y=.95,corner=c(0,1)))
  dev.off()
}

The above example fails to write the output to the file. When the loop
is finished the file size of each file is 0Kb. If I perform the work
of the loop manually, ie. setting "i <- 1", "i <- 2" etc before
manually running the loop contents then there is no problem.  There is
also no problem if I replace all the spplot stuff with say
"hist(rnorm(100))".

The same behaviour is reproduced with levelplot too so is not sp specific.

for (i in 1:5){
  png(file=paste("sillyPlot",i,".png",sep=""),bg="white",height=500,width=500)
  x <- seq(pi/4, 5 * pi, length = 100)
  y <- seq(pi/4, 5 * pi, length = 100)
  r <- as.vector(sqrt(outer(x^2, y^2, "+")))
  grid <- expand.grid(x=x, y=y)
  grid$z <- cos(r^2) * exp(-r/(pi^3))
  levelplot(z~x*y, grid, cuts = 50, scales=list(log="e"), xlab="",
            ylab="", main="Weird Function", sub="with log scales",
            colorkey = FALSE, region = TRUE)
  dev.off()
}

How should I correctly write these loops to do as intended?

cheers
David



-- 
David Pleydell

Laboratoire de Biologie Environnementale
USC INRA-EA 3184
Université de Franche-Comté
Place Leclerc F
25030
Besançon
France

(0033) 0381665763
dpleydel at univ-fcomte.fr



More information about the R-help mailing list