# [R] persp plot question..

Ross Ihaka ihaka at stat.auckland.ac.nz
Thu Oct 26 13:42:41 CEST 2000

```On Thu, Oct 26, 2000 at 11:46:43AM +0100, Colin Gillespie wrote:
> Dear All,
> 	I have been trying to to do this for a few days now. I can generate
> persp plot OK and can generate it in colour using the command
>
> persp(x,y,z,col=terrain.colors(20))
>
> However, I was wondering if it is possible to shade the 3d surface like a
> contour plot. i.e. black for large z, white for small z, say

Here is a quick and trivial example which shows you how.

#  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 <- zmid / max(zmid)

#  Plot the surface with the specified facet colours.

persp(x, y, z, col = gray(1 - hgt))

You can use something similar to select colours from one of the
other standard palettes.  E.g

persp(x, y, z, col=cm.colors(20)[floor(19*hgt+1)])

Hope this helps.

Ross

--
+----------------------------------------------------------------------+
| Ross Ihaka                         Email:  ihaka at stat.auckland.ac.nz |
| Department of Statistics           Phone:  (64-9) 373-7599 x 5054    |
| University of Auckland             Fax:    (64-9) 373-7018           |
| Private Bag 92019                                                    |
| Auckland, New Zealand                                                |
+----------------------------------------------------------------------+
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```