How to mask or crop IDW results
My question is: how can I mask or crop the results, using R, of IDW interpolation to only the area containing the original set of data points?
In the example below, 20 random points are used to interpolate a surface using the IDW function in gstat. A convex hull is obtained from the points set and plotted on the interpolated map. But, I would like to crop the map so only areas within the point cloud show the interpolation result.
Thanks for any suggestions!
###
# IDW in R
#
library(gstat)
#
set.seed(1234)
x - rnorm(20)+10
y - rnorm(20)+10
z - rnorm(20)+10
#
xyz - data.frame(x,y,z)
coordinates(xyz) - ~x+y
xr - range(x)
yr - range(y)
b - round((xr[2]-xr[1])/100,2)
#
grd - expand.grid(x=seq(from=xr[1],to=xr[2],by=b),
y=seq(from=yr[1],to=yr[2],by=b))
#
coordinates(grd) - ~ x+y
gridded(grd) - TRUE
#
idw-idw(formula=z~1, locations=xyz, newdata=grd)
#
image(idw, col=terrain.colors(16))
contour(idw, add=T)
points(x,y, col=4, pch=19)
#
library(spatstat)
conv-convexhull.xy(x,y)
plot(conv,add=TRUE)
#
Topic interpolation r
Category Data Science