Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: How to plot a 3D array

Tags:

r

I want to plot a 3D array M where

M <- array(runif(64),dim=c(4,4,4))

A similar question is here with comments that this can be done using a common 3D plot in R, but I could find no such function in R which can be used to plot multidimensional arrays (say, a 3D array as in the above example). Any suggestion how to do it? Thanks.

like image 445
Janak Avatar asked Jan 23 '26 14:01

Janak


1 Answers

Use melt to create a table of x,y,z,value, and then rgl to do a 3d plot:

library(reshape2)
library(rgl)
M=melt(M)
points3d(M$Var1,M$Var2,M$Var3)

That's just 64 points in a cube. You can scale and colour them:

points3d(M$Var1,M$Var2,M$Var3,size=10,color=rainbow(10)[M$value*10])

Use whatever method of mapping M$value to colour you prefer. Don't use rainbow palettes for real!

like image 134
Spacedman Avatar answered Jan 25 '26 07:01

Spacedman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!