Loess surfaces
A locally determined surface can be constructed using loess, first fitting a model that illustrates the response of the variable being contoured as a function of two (or more) location variables, and then using the predict() function to visualize the resulting surface:
## loess surface
library(RColorBrewer)
library(sp)
attach(ortann)
# tann as a function of latitude and longitude (and interaction)
tann.loess <- loess(tann ~ longitude + latitude, span=0.3)
summary(tann.loess)
# poor man's R-squared value
cor(tann, tann.loess$fitted)^2
# create an interpolation target grid to display predicted values
grid.longitude <- seq(-124.5000, -116.8333, .1667)
grid.latitude <- seq(42.0000, 46.1667, .0833)
grid.mar <- list(longitude=grid.longitude, latitude=grid.latitude)
# get fitted (interpolated) values
tann.interp <- predict(tann.loess, expand.grid(grid.mar))
tann.z <- matrix(tann.interp, length(grid.longitude),
length(grid.latitude))
# plot the interpolated values as shaded rectangles and contours
plotclr <- brewer.pal(8, "PuOr")
plotclr <- plotclr[nclr:1] # reorder colors
plot(orotl.shp)
image(grid.longitude, grid.latitude, tann.z, col=plotclr, add=T)
contour(grid.longitude, grid.latitude, tann.z, add=TRUE)
points(longitude, latitude)
plot(orotl.shp, add=T)