[R] persp & colors

Michael Grant mwgrant2001 at yahoo.com
Mon Jun 2 22:34:37 CEST 2003


Timur is this what you are looking for?

from Rtips at

http://lark.cc.ukans.edu/~pauljohn/R/statsRus.html#5.42

---------------------------------- Question: is it
possible to shade the 3d surface like a contour plot?
i.e. black for large z, white for small z, say 

Answer: 

# Create a simple surface f(x,y) = x^2 - y^2 

        nx <- 21
        ny <- 21
        x <- seq(-1, 1, length = nx)
        y <- seq(-1, 1, length = ny)
        z <- outer(x, y, function(x,y) x^2 - y^2)

        #  Average the values at the corner of each
facet
        #  and scale to a value in [0, 1].  We will
use this
        #  to select a gray for colouring the facet.

        hgt <- 0.25 * (z[-nx,-ny] + z[-1,-ny] +
z[-nx,-1] + z[-1,-1])
        hgt <- (hgt - min(hgt))/ (max(hgt) - min(hgt))

        #  Plot the surface with the specified facet
colours.

        persp(x, y, z, col = gray(1 - hgt), theta =
35)
        persp(x, y, z, col = cm.colors(10)[floor(9 *
hgt + 1)], theta = 35)

(from Ross Ihaka) 

Regards,
Michael Grant

--- Timur Elzhov <Timur.Elzhov at jinr.ru> wrote:
> On Sun, Jun 01, 2003 at 06:53:55PM +0200, Uwe Ligges
> wrote:
> 
> >> but I'd like to persp()' colors behave like in
> image() function!
> 
> > That's not easy, because you have to redefine x, y
> and z values.
> > 
> > Simple example:
> > 
> >  x <- y <- 1:2
> >  z <- matrix(1:4, 2)
> >  image(x, y, z)    # OK, quite nice
> > 
> > but
> > 
> >  persp(x, y, z)
> > 
> > has only one facet. So the only way is to
> calculate the 9 values for x, 
> > y, and z to get the corners for the 4 facets in
> it.
> > That's easy for x and y, but can be impossible for
> z...
> 
> OK, thank you for answer!
> But, I saw that other mathematic frameworks (CERN
> ROOT for instance)
> can plot 3D surfaces with colors corresponding to
> z-value.
> Is there way to do this in R (with another
> functions/packages)?
> It's not necessary to use _one_ color per facet,
> yes?.. :)
> 
> 
> --
> WBR,
> Timur.
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
>
https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list