[R] colour by z value, persp in raster package

David Winsemius dwinsemius at comcast.net
Tue Feb 28 20:47:38 CET 2012

On Feb 28, 2012, at 4:34 AM, Omphalodes Verna wrote:

> Hi all!
> My question is how to colour pixels by z value in persp plot in  
> raster package. Here is an example:
> x <- seq(-1.95, 1.95, length = 30)
> y <- seq(-1.95, 1.95, length = 35)
> z <- outer(x, y, function(a,b) a*b^2)
> r1 <- raster(nrows=35, ncols=30, xmn=0, xmx=30, ymn = 0, ymx = 35)
> r1[] <- c(z)
> persp(r1)
> There already exist some function to produce persp plot for anothe  
> classes, but I have no idea how deal with RasterLayer object.

Is that really an example? After going to the trouble of correcting  
the error in this code from failing to load the raster package, I now  
see that there is no connection between the values of x, y, or x with  
the raster-call.

 > r1[] <- as.matrix(z)
Error in .local(x, values) : cannot use a matrix with these dimensions
Error in .local(x, i, j, ..., value) :
cannot replace values on this raster (it is too large
 > persp(r1)
Error: hasValues(x) is not TRUE

#   Post some useful code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT

More information about the R-help mailing list