# [R] color scale in rgl plots

Stephen Tucker brown_emu at yahoo.com
Sat Jul 7 10:31:50 CEST 2007

```Hi David,

I'm not an expert in 'rgl', but to determine data-dependent color for points
I often use cut().

# using a very simple example,
x <- 1:2; y <- 1:2; z <- matrix(1:4,ncol=2)

# the following image will be a projection of my intended 3-D 'rgl' plot
# into 2-D space (if we don't consider color to be a dimension):
library(fields)
image.plot(x,y,z)

# the 3-D rgl plot will be as follows:
df <- data.frame(x=rep(x,times=length(y)),
y=rep(y,each=length(x)),
z=as.vector(z))
plot3d(x=df,col=1:4,type="s")

## looks okay so moving onto bigger example:
x <- 1:10; y <- 1:10; z <- matrix(1:100,ncol=10)

# 2-D projection:
image.plot(x,y,z)

# 3-D plot in rgl
df <- data.frame(x=rep(x,times=length(y)),
y=rep(y,each=length(x)),
z=as.vector(z))
# This is how I determine color:
nColors <- 64
colindex <- as.integer(cut(df\$z,breaks=nColors))
plot3d(x=df,type="s",col=tim.colors(nColors)[colindex])

===
tim.colors(nColors)[colindex] will return a vector of colors the same length
as 'df'.

I don't think as.integer() on cut() is entirely necessary because cut()
returns a factor... in any case, I use these integers as indices for
tim.colors() [you will need the 'fields' package for this set of colors].

Hope this helps.

ST

--- David Farrelly <david.farrelly at gmail.com> wrote:

> Hello,
>
> I'm trying to make a 3d plot using rgl in which the size and color of
> each point corresponds to certain attributes of each data point. The color
> attribute, let's call it X, is scaled to go from 0 to 1. The
> rainbow(64,start=0.7,end=0.1) palette is perfect for what I want but I
> don't know how to take that palette and pick a color from it based on
> the value of X for a given data point. I'm fairly new to R and any
> suggestions would be greatly appreciated.
>
> Here's what I do - it's how to do the color mapping that has me stumped.
>
>
plot3d(th1,ph,th2,type='s',size=wd1,col=????(rp1,0,0,1),cex=2,ylab=NULL,xlab=NULL,zlab=NULL,xlim=c(0,1),ylim=c(0,2),zlim=c(0,1),box=TRUE,axes=FALSE)
>
> I have also tried the more obvious col = rgb(a,b,c,d) where a,b,c,d are
> functions of X but I can't manage to come up with a nice looking color
> scale.
>
>
> David
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help