Search code examples
rplotrglbubble-chartscatter3d

rgl 3D scatterplot - controlling size of spheres from 4th dimension (bubble plot)


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?


Solution

  • 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