David thanks!
 
Maybe is better ''orginal'' example from raster package:
 
r <- raster(system.file("external/test.grd", package="raster"))
persp(r)
class(r)
 
It is not a problem to colour plot by z value in perspective plot (persp) in package graphic - there is an example in help files:
 
par(bg = "white")
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)
nrz <- nrow(z)
ncz <- ncol(z)
# Create a function interpolating colors in the range of specified colors
jet.colors <- colorRampPalette( c("blue", "green") ) 
# Generate the desired number of colors from this palette
nbcol <- 100
color <- jet.colors(nbcol)
# Compute the z-value at the facet centres
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
# Recode facet z-values into color indices
facetcol <- cut(zfacet, nbcol)
persp(x, y, z, col=color[facetcol], phi=30, theta=-30)
par(op)

But question is how specify right sequences of colours (e.g. terrain.colors) for RasterLayer object. The idea of code is persp(r, col = terrain.color(n)), where r is class "raster".
 
Thanks all!
 
OV

 


________________________________
From: David Winsemius <dwinsemius@comcast.net>

Cc: "r-help@r-project.org" <r-help@r-project.org> 
Sent: Tuesday, February 28, 2012 8:47 PM
Subject: Re: [R] colour by z value, persp in raster package


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
	[[alternative HTML version deleted]]

