Janak
Janak

Reputation: 683

R: How to plot a 3D array

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.

Upvotes: 3

Views: 4391

Answers (1)

Spacedman
Spacedman

Reputation: 94172

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!

Upvotes: 7

Related Questions