[R] surface3d grid from xyz dataframe
Duncan Murdoch
murdoch at stats.uwo.ca
Mon Dec 18 02:49:46 CET 2006
On 12/17/2006 7:56 PM, Alexander.Herr at csiro.au wrote:
> Hi List,
>
> I am trying to plot a grid with an overlayed height. I have a dataframe
> with four variables:
> x,y,gridvalue,height. The dataframe has 2.5mio observations (ie grid
> points),
>
> I assign colors through the gridvalue using map_color_gradient thus
> producing:
> x,y,gridvalue,height,gridcol as variables of the dataframe. The grid
> dimensions are 1253 x 2001 (=2507253 data points).
>
> My attempts with surface3d fail, mainly because I cannot produce the
> matrix required for the height input.
>
> elev.to.list{CTFS} fails with: "Error in matrix(elevfile$elev, nrow=
> 1+ydim/gridsize, ncol=1+xdim/gridsize. : attempt to set an attribute on
> NULL" which I assume means it requires a square grid (=quadrates).
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
More information about the R-help
mailing list