3d scatterplot in R using rgl plot3d - different size for each data point?

Jack Smith picture Jack Smith · Apr 26, 2012 · Viewed 9k times · Source

I am using

plot3d(x,y,z, col=test$color, size=4) 

to plot a 3d scatterplot of my dataset with R, but with rgl the size argument only takes one size.

Is it possible to have different sizes for each data point, perhaps with another library, or is there a simple workaround?

Thanks for your ideas!

Answer

Josh O'Brien picture Josh O'Brien · Apr 27, 2012

Here's a work-around along the same lines suggested by Etienne. The key idea is to set up the plot, then use a separate call to points3d() to plot the points in each size class.

# Break data.frame into a list of data.frames, each to be plotted 
# with points of a different size
size <- as.numeric(cut(iris$Petal.Width, 7))
irisList <- split(iris, size)

# Setup the plot
with(iris, plot3d(Sepal.Length, Sepal.Width, Petal.Length, col=Species, size=0))

# Use a separate call to points3d() to plot points of each size
for(i in seq_along(irisList)) {
    with(irisList[[i]], points3d(Sepal.Length, Sepal.Width, 
                                 Petal.Length, col=Species, size=i))
}

(FWIW, it does appear that there's no way to get plot3d() to do this directly. The problem is that plot3d() uses the helper function material3d() to set point sizes and as shown below, material3d() only wants to take a single numeric value.)

material3d(size = 1:7)
# Error in rgl.numeric(size) : size must be a single numeric value