10

I am using the plot3d function to make a 3d plot in my R script. I'd like to add a 4th dimension, which will be color. How can I do this?

Specifically, assume I have the following code:

plot3d(x, y, z, col=cols, size=2, type='s')

how would I populate cols based on a vector of values acting as my 4th dimension.

CodeGuy
  • 28,427
  • 76
  • 200
  • 317

1 Answers1

13

Just make a colormap, and then index into it with a cut version of your c variable:

x = rnorm(100)
y = rnorm(100)
z = rnorm(100)

c = z
c = cut(c, breaks=64)
cols = rainbow(64)[as.numeric(c)]

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

enter image description here

John Colby
  • 22,169
  • 4
  • 57
  • 69
  • I wasn't aware of `rainbow`. Very nice! –  Nov 04 '11 at 17:24
  • 1
    but how could I add a scale on the side showing my range of colors...like a legend for the colors – CodeGuy Nov 04 '11 at 17:27
  • Yea it is very flexible and lets you set the start/end hues, etc.. Under the rainbow help there are also a few other built-in color maps too. Good luck! – John Colby Nov 04 '11 at 17:27
  • and how did you choose 64? is this an arbitrary number? can you explain what this does? – CodeGuy Nov 04 '11 at 17:31
  • 1
    @CodeGuy I believe rgl still doesn't have any legend/HUD ability. I've always just added the legend into the figure after the fact. I use a little convenience function I made (http://www.colbyimaging.com/wiki/statistics/color-bars), which of course you're welcome to have. – John Colby Nov 04 '11 at 17:32
  • 64 is arbitrary...it's just how "smooth" do you want the color transitions. Sometimes smoother is more useful; sometimes not. – John Colby Nov 04 '11 at 17:33
  • can you show me how I could use your function to add a color bar? – CodeGuy Nov 04 '11 at 17:35
  • so to make it very smooth, I could make it a really large number? thanks so much for your help! – CodeGuy Nov 04 '11 at 17:35
  • 1
    `color.bar(rainbow(64), min(c), max(c), title='C', ticks=round(seq(min(c), max(c), len=11), 2))` would be one way to do it. http://i.imgur.com/CGBrQ.png – John Colby Nov 04 '11 at 17:54
  • Correct...a big number will mean very smooth. – John Colby Nov 04 '11 at 17:55
  • when I include the color.bar line you posted, i get the following error:Error in Summary.factor(c(6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, : max not meaningful for factors – CodeGuy Nov 04 '11 at 17:57
  • Make the color bar before you turn c into a factor. Or keep the original version around instead of overwriting it. – John Colby Nov 04 '11 at 18:11
  • You know-- in this answer, color is not a "4th dimension". It's simply highlighting z-values, aka the third dimension. You can do this stuff with the "drape" tool in wireframe(), for example. A true 4th dimension color would assign colors to some other data attribute. E.g., `mydata<-data.frame(x,y,z,t)` , and use `colors=rainbow(42)[cut(t,42)]` -- sort of thing. (And always use 42, since that's the answer to the ultimate question...) – Carl Witthoft Nov 04 '11 at 19:07
  • Right. I only chose to reuse z here so that it was clear it was working. – John Colby Nov 04 '11 at 19:09
  • yeah, instead of cut(z...) i plug in my 4th dimension. is this wrong? – CodeGuy Nov 04 '11 at 23:10
  • Typo...should be `c = cut(c, ...)`, whatever you choose for your `c`. – John Colby Nov 04 '11 at 23:20