Question

I have a 3d dataset that I want to plot with rgl such that

library(rgl)
x = rnorm(2700,0,0.6)
y = rnorm(2700,0,0.7)
z = rnorm(2700,0,0.5)
plot3d(x,y,z)

I would like to allocate different colors to different parts of the data and tried

col1=colorRampPalette(c("black","green","red","blue","cyan"))
colorscheme=col1(2700)[c(c(73:193,409:481,937:1579),229:373,517:721,757:901,1615:1963)]
plot3d(x,y,z,col=colorcheme)

However, the colors are not discretized... which makes sense as colorRampPalette creates a palette rather than discretized color schemes! Basically, I want all values between 73:193 to be exclusively black, all between 229:373 to be exclusively green etc... I fear that I am missing the obvious but I just can't find the answer. Would there be a function just like colorRampPalette than would create discretized color bands? Also, just in case this matters, I don't want some of the values to be plotted (e.g. between 193 and 229).

Thanks for your help.

Was it helpful?

Solution

This seems to be a better approach:

library(rgl)
x = rnorm(2700,0,0.6)
y = rnorm(2700,0,0.7)
z = rnorm(2700,0,0.5)
plot3d(x,y,z)

COLS <- rep(NA, 2700)
COLS[c(73:193,409:481,937:1579)] <- "black"
COLS[229:373] <- "green"
COLS[517:721] <- "red"
COLS[757:901] <- "blue"
COLS[1615:1963] <- "cyan"

plot3d(x,y,z,col=COLS)

NA's remain uncolored.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top