[R] 3d plot with different levels done in different colors

Jerome Asselin jerome at hivnet.ubc.ca
Fri Jul 11 22:49:37 CEST 2003


Hi,

Consider this example which I have modified from the persp() help file.
It uses topo.colors() to create a series of colors.

Cheers,
Jerome

     x <- seq(-10, 10, length= 30)
     y <- x
     f <- function(x,y) { r <- sqrt(x^2+y^2); 10 * sin(r)/r }
     z <- outer(x, y, f)
     z[is.na(z)] <- 1
     op <- par(bg = "white")
     zz <- (z[-1,-1] + z[-1,-ncol(z)] + z[-nrow(z),-1] + 
              z[-nrow(z),-ncol(z)])/4
     cols <- topo.colors(length(zz))
     cols[order(zz)] <- cols
     persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = cols)


On July 11, 2003 08:26 am, Tamas Papp wrote:
> Content-Length: 1223
> Status: R
> X-Status: N
>
> I would like a 3d plot of a matrix such that individual trapezoids
> that make up the surface are colored according to the z-value of that
> point (or preferably the midpoint of its four corners, or something
> similar). MS Excel has something like that.
>
> I know that persp can have an nx by ny matrix its "col" argument, I
> just don't know how to generate that matrix. To calculate the midpoint
> of the tiles for the matrix z, I could use something like
>
> zz <- (z[-1,-1] + z[-1,-nrow(z)] + z[-ncol(z),-1] +
> z[-ncol(z),-nrow(z)])/4
>
> but I don't know how to assign a color to that value. For example if I
> have
>
> boundaries <- c(0, .5, 1, 1.5)
>
> and
>
> colors <- c("red", "green", "blue")
>
> I am looking for a function that assigns red to the elements of zz
> between 0 and .5, etc. Alternative solutions are welcome, maybe
> somebody has already wrote a library that does the whole thing neatly.
>
> Thanks,
>
> Tamas




More information about the R-help mailing list