(This article was first published on [R] tricks, and kindly contributed to R-bloggers)
Here is a script that colors a 3D plot according to the z value (height) of each point.
persp.withcol <- function(x,y,z,pal,nb.col,...,xlg=TRUE,ylg=TRUE)
{
colnames(z) <- y
rownames(z) <- x
nrz <- nrow(z)
ncz <- ncol(z)
color <- pal(nb.col)
zfacet <- z[-1, -1] + z[-1, -ncz] + z[-nrz, -1] + z[-nrz, -ncz]
facetcol <- cut(zfacet, nb.col)
par(xlog=xlg,ylog=ylg)
persp(
as.numeric(rownames(z)),
as.numeric(colnames(z)),
as.matrix(z),
col=color[facetcol],
...
)
}
dd
To leave a comment for the author, please follow the link and comment on his blog: [R] tricks.
R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series,ecdf, trading) and more...

Zero Inflated Models and Generalized Linear Mixed Models with R.
Zuur, Saveliev, Ieno (2012).