bcorso
bcorso

Reputation: 47078

RGL Plot3D: More Colors, No Repeats

When plotting using rgl plot3D I use something like this:

col = as.integer(as.factor(data[[input$colorBy]])))
plot3d(d[,1], d[,2], d[,3], type=type, col=col, add=TRUE)

However, this will repeat colors if there are more than 10 or so different factors. Is there any way to extend this so that there are never repeating colors, and (ideally) the colors span a large range of a colorspace?

Upvotes: 1

Views: 861

Answers (1)

eipi10
eipi10

Reputation: 93841

There are a number of ways to set the color yourself. I've used the z-value to set the colors in the example below, but you can of course set the colors using grouping variables, transformations, etc. Here are a few options:

# Fake data
dat = data.frame(x=sort(runif(100,0,100)), y=runif(100,0,100))
dat$z = dat$x + dat$y
dat = dat[order(dat$z), ]

# hcl colors (I've scaled z so that the h value ("hue") ranges from 
# zero to 350 (where the hue scale ranges from 0 to 360 degrees))
plot3d(dat, col=hcl(350*(dat$z - min(dat$z))/(max(dat$z) - min(dat$z)), 100, 65))

# Create a color ramp function (from red to green to blue in this case)
colFunc = colorRamp(c(rgb(1,0,0), rgb(0,1,0), rgb(0,0,1)))
cols=colFunc(dat$z/max(dat$z))

plot3d(dat, col=rgb(cols, maxColorValue=255))

Upvotes: 1

Related Questions