[R] "Over-coloring" facets on persp() plot

David Winsemius dwinsemius at comcast.net
Sun Nov 22 15:42:11 CET 2009


On Nov 22, 2009, at 9:18 AM, David Winsemius wrote:

>
> On Nov 22, 2009, at 7:07 AM, Duncan Murdoch wrote:
>
>> On 22/11/2009 1:07 AM, Marc Chiarini (Tufts) wrote:
>>> Dear R Community:
>>> Recently, I have managed to plot some really useful graphs of my  
>>> research data using persp().  I have even figured out how to  
>>> overplot rectangular regions (corresponding to submatrices) with a  
>>> different color.  This is accomplished by using par(new=T).  I am  
>>> now searching for a way to "highlight" a set of (possibly non- 
>>> contiguous) facets with a specific color, e.g., the facet between  
>>> each set of four points whose values are all above a certain  
>>> threshold.  An example would be coloring the raised corners of the  
>>> classic sombrero (found in example(persp)) differently from the  
>>> rest of the sombrero.  I feel like the last example in persp() is  
>>> pointing me in the right direction, but I'm not quite getting it.   
>>> Any help is much appreciated.
>>
>> Think of the facets as an nx-1 by ny-1 matrix.  Pass the col arg by  
>> creating a matrix of this shape.  (A vector version of the data in  
>> the matrix would also be good enough.)
>
> I came close to coloring the "top" facet, but did not quite get  
> there with:
>
> 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")
>
> zcol <- as.vector( z[-1,-1] == max(z) )  # Need to exclude two side  
> edges, I think
>
> persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col =  
> ifelse(zcol , "red", "lightblue"),
>      ltheta = 120, shade = 0.75, ticktype = "detailed",
>      xlab = "X", ylab = "Y", zlab = "Sinc( r )"
>     )
> par(op)
>
> I did not get precisely the top facet in part, because there are 4 z  
> elements at the max.

Specifying row and column == 15 for z[-1,-1] does color just the top  
facet. Conditional level coloring can be achieved as above with  
suitable limits on the z values:

zcol <- as.vector( z[-1,-1] >1 & z[-1,-1] <3 )


>
> --
> David.
>
>
>>
>> If you pass something shorter, it will be recycled to that length.
>>
>> You could also use persp3d from the rgl package, but an important  
>> difference is that it colours all nx by ny vertices, and  
>> interpolates colours on the facets.  So you can't use the same  
>> colour matrix as in persp.
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list