Hi David,
Putting a print statement around your spplot command solves your
problem. This behaviour is common for trellis graphics plot methods
(such as spplot). This is the command including print:
print(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))))
These kinds of questions maybe are not specific to sp, but still might
generate more response from the r-sig-geo mailing list.
cheers,
Paul
David wrote:> 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
>
>
>
>
--
Drs. Paul Hiemstra
Department of Physical Geography
Faculty of Geosciences
University of Utrecht
Heidelberglaan 2
P.O. Box 80.115
3508 TC Utrecht
Phone: +31302535773
Fax: +31302531145
http://intamap.geo.uu.nl/~paul