[R] How to put different color in some portion of a surface plot?

Duncan Murdoch murdoch at stats.uwo.ca
Tue May 6 16:52:14 CEST 2008


On 5/6/2008 10:31 AM, Megh Dal wrote:
> Hi all,
>    
>   I have following problem :
>    
>   a = b = seq(1, 50000, by=500)
> v = matrix(0, nrow=length(a), ncol=length(a))
> for (i in 1:length(a))
>    {
>     for (j in 1:length(a))
>        {
>         d = c(17989*a[i], -18109*b[j])
>         v[i,j] = t(d) %*% matrix(c(0.0001741, 0.0001280, 0.0001280, 0.0002570), nrow=2) %*% d
>        }
>    }
> library("rgl")
> open3d()
> persp3d(a,b,v,col="green",alpha=0.7,aspect=c(1,1,0.5))
> 
>    
>   Now I want to shed the portion with different color of the surface which satisfy following condition:
>    
>   0 < (a-b) < max(a)
>    
>   Can anyone please tell me how to do that?

The col option can be a matrix of the same shape as v, giving the colour 
at each vertex.  (Note that this is different from persp(), which gives 
the colour in each cell, i.e. one less in each dimension.  The persp3d 
man page tells how to duplicate the persp() behaviour.)

For example,

shade <- outer(a, b, function(x,y) (0 < (x-y)) & ((x-y) < max(a)))
persp3d(a,b,v,col=ifelse(shade, "red", "green"),
         alpha=0.7,aspect=c(1,1,0.5))

Duncan Murdoch



More information about the R-help mailing list