ffarg - 4 months ago 51

R Question

I have a three column data frame with latitude, longitude, and underground measurements as the columns. I am trying to figure out how to interpolate data points between the points I have (which are irregularly space) and then create a smooth surface plot of the entire area. I have tried to use the 'surface3d' function in the 'rgl' package but my result looks like a single giant spike. I have been able to plot the data with 'plot3d' but I need to take it a step further and fill in the blank spaces with interpolation. Any ideas or suggestions? I'm also open to using other packages, the rgl just seemed like the best fit at the time.

EDIT: here's an excerpt from my data (measurements of aquifer depth) :

`lat_dd_NAD83 long_dd_NAD83 lev_va_ft`

1 37.01030 -101.5006 288.49

2 37.03977 -101.6633 191.68

3 37.05201 -100.4994 159.34

4 37.06567 -101.3292 174.07

5 37.06947 -101.4561 285.08

6 37.10098 -102.0134 128.94

Answer

You could use the deldir package to get a Delaunay triangulation of your points, then convert it to the form of data required by `triangles3d`

for plotting. I don't know how effective this would be on a really large dataset, but it seems to work on 100 points:

```
library(deldir)
library(rgl)
# Create some fake data
x <- rnorm(100)
y <- rnorm(100)
z <- x^2 + y^2
# Triangulate it in x and y
del <- deldir(x, y, z = z)
triangs <- do.call(rbind, triang.list(del))
# Plot the resulting surface
plot3d(x, y, z, type = "n")
triangles3d(triangs[, c("x", "y", "z")], col = "gray")
```

EDITED to add:

The version of `rgl`

on R-forge now has a function to make this easy. You can now produce a plot similar to the one above using

```
library(deldir)
library(rgl)
plot3d(deldir(x, y, z = z))
```

There is also a function to construct `mesh3d`

objects from the `deldir()`

output.