[R] Question about graphics in R

Jim Lemon bitwrit at ozemail.com.au
Mon Jan 9 15:47:02 CET 2006


Martin Erwig wrote:
> Considering the R function/plot shown below, I wonder whether
> it is possible to do the following changes:
> 
> (1) Change the color of each point to be picked from
> list of colors according to its z-value. (The range
> should be from blue (z=0) to red (z=1).) The grid
> should then be omitted. [I have seen "terrain.colors", but
> don't know how to use it for this purpose.]
> 
> (2) Add two lines to the surface for, say z=0.8 and
> z=0.3. [Can contour or contourLines be used?]
> 
> 
> ---
> 
> x <- seq(0, 1, length = 50)
> y <- x
> f <- function(x,y) { sin ((1-x)*y) }
> z <- outer(x,y,f)
> 
> persp(x, y, z,
> 	theta = 30, phi = 30,
> 	shade = 0.3, col = "red"
> 	)
> 
> ---
> 
> 
> Finally, I would also produce a flattened 2D map
> of the same function, i.e. a map in which each point
> (x,y) is mapped to a color in a range according to
> f(x,y). Also two lines for f(x,y)=c1 and f(x,y)=c2
> should be added.
> 
> 
> Is this possible?
> 
Hi Martin,

The function color.scale will linearly transform numeric values into
colors between arbitrary color endpoints specified as red, green and blue:

Your example would be
redrange=c(0,1),greenrange=c(0,0),bluerange=c(1,0).

For the 2D plot, have a look at color2D.matplot. Both are in the plotrix
package.

You might also want to look at colorRampPalette in the grDevices package.

Jim




More information about the R-help mailing list