1

I am working on a 3D scatter plot using rgl package in R, with multiple colors for different series. I was wondering if there would be a way to plot a 4th dimension by controlling the size of spheres.

I know it's possible with plotly ("bubble plot") : https://plot.ly/r/3d-scatter-plots/, but Plotly starts to flicker when dealing with lots of datapoints. Can the same result be achieved using Rgl?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
julien.leroux5
  • 969
  • 7
  • 17

1 Answers1

0
set.seed(101)
dd <- data.frame(x=rnorm(100),y=rnorm(100),z=rnorm(100),
                 c=rnorm(100),s=rnorm(100))

Scaling function (I tweaked to keep the values strictly in (0,1), don't know if that's really necessary):

ss <- function(x) scale(x,center=min(x)-0.01,scale=diff(range(x))+0.02)
library(rgl)

Define colours (there may be a better way to do this ...)

cvec <- apply(colorRamp(c("red","blue"))(ss(dd$c))/255,1,
              function(x) rgb(x[1],x[2],x[3]))

The picture (need type="s" to get spheres)

with(dd,plot3d(x,y,z,type="s",radius=ss(s), col=cvec))

enter image description here

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • Thanks, exactly what I needed! I could not make this radius argument working. Now, rgl is getting quite slow with ~30 000 spheres... – julien.leroux5 Dec 22 '18 at 18:43
  • using points instead of spheres would render faster, but AFAICT there's not an obvious way to have variable-size points within a plot ... (also not as pretty) – Ben Bolker Dec 22 '18 at 19:13