[R-sig-Geo] plotting raster* objects: controlling the display scale

Barry Rowlingson b.rowlingson at lancaster.ac.uk
Thu Nov 10 13:59:50 CET 2011


> Is it possible to force 'plot' to scale the display according to the specified zlim?

 I've just had a fairly deep look at the code and (pending response
from the master himself) I don't think you can.

 I assume the reason you want to do it is to get the nice legend? The
code for that is in raster:::.imageplot, and it gets the range
directly from the raster, bypassing any zlim arguments (which are
ignored by plot, as you notice). You can get round that ignoring by
going straight to .imageplot:

 f1=raster(matrix(runif(100),10,10))    # range (0,1)
 f2=raster(matrix(1+runif(100),10,10)) # range(1,2)
 par(mfrow=c(1,2))
 raster:::.imageplot(1:10,1:10,t(as.matrix(f1)),zlim=c(0,2))
 raster:::.imageplot(1:10,1:10,t(as.matrix(f2)),zlim=c(0,2))

that does the plots in the right colours for a zlim of 0,2, but the
legends are wrong, and at that point you may as well just use image:

 image(f1,zlim=c(0,2))
 image(f2,zlim=c(0,2))

 and then do a legend by a bit of bodging with .imageplot:

raster:::.imageplot(1:2,1:2,c(0,2),zlim=c(0,2),legend.only=TRUE)

some tweaking needed to get it in the right position....

This is all horrible hackery. The real answer will, I don't doubt,
follow shortly...

Barry



More information about the R-sig-Geo mailing list