R Visualize many 3D boxes inside a big box


I would need to somehow make an interactive visualization of a 3D box contain many 3D boxes inside it.

My first crude ideas were as following. In 2D I could use

x<-matrix(runif(25),nrow=5,ncol=5);
image(x)

to color every single cell in the matrix to make it look like the big rectangle would contain small rectangles in it.

How could this be translated into 3D? Let's say in 3D the big box is of size 10x10x10. In practice I would like to choose the color of every single one of the 1000 elements in the box.I know that rgl can be used to make interactive 3D plots, but I'm having issues understanding how to color every single element in the 3D array.

If you have some suggestions for some better solutions I would gladly like to hear them.


Answers:


If I understood correctly, I believe that this should work:

library(rgl)
grd <- expand.grid(x=seq(0,10,2), y=seq(0,10,2), z=seq(0,10,2))
grd$dist <- sqrt(grd$x^2 + grd$y^2 + grd$z^2) # distance to coordinate 0,0,0
grd$col <- rainbow(ceiling(max(grd$dist+1)))[ceiling(grd$dist+1)]
grd$alpha <- rep(c(0.2, 1), each=nrow(grd)/2)
open3d()
for(i in seq(nrow(grd))){
  shade3d( translate3d( cube3d(col = grd$col[i]), grd$x[i], grd$y[i], grd$z[i]) , alpha=grd$alpha[i])
}
rgl.snapshot("cube.png")

enter image description here

This example is for a 6x6x6 cube, and colors are based on euclidean distance of their centers to the origen. Hopefully this will show you a way to adapt your colors as you like.