Skip to content
Prev 106284 / 398506 Next

surface3d grid from xyz dataframe

On 12/17/2006 7:56 PM, Alexander.Herr at csiro.au wrote:
When you are asking a question about a function from a contributed 
package, please state which package you found it in.  There's a 
surface3d function in the rgl package; is that the one you're using?  It 
takes input in the same format as contour() uses.  That is:  the x 
values should be a vector of values corresponding to the rows of a 
matrix, the y values correspond to the columns, the z values are in a 
matrix.

Since your data is in a dataframe, it's not the right shape.  How to get 
it into the right shape depends a lot on what the pattern of your data 
really is.  Do you have a relatively small number of x and y values, 
corresponding to rows and columns, or are they scattered over the 
region?  If the former, I'd convert them to integer values marking the 
positions, then use those to index into a matrix to place the z values 
there.

e.g. with data like this:

x y z
1 1 1
1 2 2
2 1 3
2 2 4

the x and y values are already integer valued, so you could use

x <- sort(unique(data$x))
y <- sort(unique(data$y))
z <- matrix(NA, length(x), length(y))
z[cbind(data$x, data$y)] <- data$z

Duncan Murdoch