Search code examples
rplot3drgl

Make numerous groups easily distinguishable on 3D plot produced by plot3d function of rgl R package


example of the 3D plot i got in resultI have 23 different groups,each of them consists of from 7 to 20 individual samples (totally approximately 350-400 observations) with their own x,y & z coordinates. I'd like to produce 3D plot based on the data i have by means of plot3d function of rgl R package. It's not a big deal in general. The problem, that i'd like to make each one from the mentioned above 23 groups to be easy distinguishable on the 3D plot. I tried to use different colors for each group, but unfortunately it's not possible to find a 23 well recognizable by human eyes colors. I was thinking about pch parameter like in the plot function of base R library. But, again, as i can see there is not such option in the plot3d function. Besides, i have to explain, that there are too much points in my data set and adding the labels to each point (e.g. with text3d rgl function) is not a good idea (they will overlap with each other and give in result some kind of a mess on the 3D plot). Is there way to figure out it (i gues it's very common problem)? Thank you in advance!
Below is code of some toy example for better explanation:

# generate data

prefix=rep("ID",69)
suffix=rep(1:23,3)
suffix_2=as.character(suffix[order(suffix)])
titles_1=paste(prefix,suffix,sep="_")
titles_2=titles_1[order(titles_1)]
x=1:69
y=x+20
z=x+50
df=data.frame(titles_2,x,y,z)

# load rgl library

library('rgl')

# make 3D plot

plot3d(x,y,z)

Solution

  • If you like living on the bleeding edge, there's a new function rgl::pch3d() that draws symbols using the same codes as points() does in base graphics. It's in rgl 0.95.1475, available on R-forge (and within a few hours on Github; see How do I install the latest version of rgl?). It's not completely working with rglwidget() yet.

    The example code

    open3d()
    i <- 0:25; x <- i %% 5; y <- rep(0, 26); z <- i %/% 5
    pch3d(x, y, z, pch = i, bg = "green")
    text3d(x, y, z + 0.3, i)
    pch3d(x + 5, y, z, pch = LETTERS[i+1])
    text3d(x + 5, y, z + 0.3, i+65)
    

    produces this display (after some resizing and rotation):

    enter image description here