[R] Plotting 3d data?

Duncan Murdoch murdoch at stats.uwo.ca
Mon Jun 16 22:24:28 CEST 2008


On 6/16/2008 1:32 PM, Wesley Tansey wrote:
> Hi all!
> 
>  
> 
> I'm very new to R, and I'm having trouble figuring out how to go from a file
> of points that I have to a 3d surface plot of the data. I typically have
> something like this:
> 
>  
> 
> X          Y          Z
> 
> 0.005    0.023    34.45
> 
> 0.0035  0.63      28.48
> 
> .
> 
>  
> 
> I've tried looking at the persp and wireframe packages, and the rgl package,
> but I can't seem to figure out how to use any of them. I tried to take the
> rgl.surface3d example and use it for myself, and this is what I have so far:
> 
>  
> 
> setwd(".")
> 
>  
> 
> data <- read.csv("data.csv",header=T)
> 
>  
> 
> x <- data$X
> 
> y <- data$Y
> 
> z <- data$Z
> 
>  
> 
> open3d()
> 
> surface3d(x, y, z) 
> 
>  
> 
> That gives me the following error:
> 
>  
> 
> Error in rgl.surface(x = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  : 
> 
>   y length != x rows * z cols
> 
>  
> 
> I have no idea what it is trying to tell me there. Would anyone mind helping
> me out?

surface3d (and rgl.surface) expect you to have data that corresponds to 
a grid defining the surface.  So you need at least one of the parameters 
(z in surface3d, y in rgl.surface) to be a matrix with rows 
corresponding to x values and columns corresponding to the other variable.

If your data is really a bunch of points, not on a grid, then you need 
to use plot3d() to plot it as points, or convert it to a surface.  The 
interp() function in the akima package can do that.  (There are lots of 
other possibilities too.)

Duncan Murdoch



More information about the R-help mailing list