[R] png misbehaving with levelplot when inside a loop

From: David <dpleydel_at_univ-fcomte.fr>
Date: Fri, 02 May 2008 19:14:30 +0200

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){

  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)

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


David Pleydell

Laboratoire de Biologie Environnementale
UniversitÚ de Franche-ComtÚ
Place Leclerc F

(0033) 0381665763

R-help_at_r-project.org mailing list
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 02 May 2008 - 18:30:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 02 May 2008 - 23:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive