# [R-sig-Geo] convert x,y,z data into a grid

Michal Gallay mgallay01 at qub.ac.uk
Wed May 7 11:51:32 CEST 2008

```On May 7 2008, Meesters, Erik wrote:

> Dear list members,
> I'm looking for a function in R like the function r.in.xyz in GRASS (vs.
> 6.2). I have x, y, z data frame (UTM) with z equals depth and would like
> to make a 3dperspective plot with rgl. My dataframe has 2.5 milion rows,
> so I think it's necessary to convert the data to a grid with something
> like mean depth per grid cell. I know I've seen an answer to a similar
> question somewhere on one of the lists, but I just don't seem to be able
> to find it again. Thanks for any help.
> Erik Meesters
>

Hello Erik,

you could try something like the following code with lattice package.
You don't need to convert the data to grid for this.

require(lattice)

wireframe(z ~ x * y, data = mydata,
scales = list(arrows = FALSE),
drape = TRUE, colorkey = TRUE,
screen = list(z = 30, x = -60))

or with simple graphics function 'persp' but for this you need to convert
the data to matrix/grid. The following code presumes your x,y,z coordinates
are on a grid. It will not work if they are not or at least I am not aware
of any other approach.

require(sp)

coordinates(mydata) <- ~ x+y
gridded(mydata) <- TRUE
m <- as.matrix(mydata)

exag <- 3
spacing <- 5
theta_val <- 120
phi_val <- 35
ltheta_val <- 20
lphi_val <- 10
z <- exag * m      # Exaggerate the relief
x <- spacing * (1:dim(m))
y <- spacing * (1:dim(m))

par(c(0.5,0.5,0.5,0.5))
persp(x, y, z, theta = theta_val, phi = phi_val,
col = "gray", scale = FALSE,
ltheta = ltheta_val, lphi=lphi_val, shade = 0.75,
border = NA, box = TRUE, r=600)

This worked for my 2mil points data.

I hope it helps.

Michal

> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Michal Gallay

School of Geography, Archaeology and Palaeoecology
Queen's University
Belfast BT7 1NN
Northern Ireland

Tel: +44(0)2890 273929
Fax: +44(0)2890 973212
email: mgallay01 at qub.ac.uk
www: www.qub.ac.uk/geog

```