Spatial exploratory data analysis
In addition to maps, a number of basic statistics and plots for spatial data can be obtained through the application of some of standard plotting functions and descriptive statistics. Such plots are essentially aspatial, in the sense that they do not require specific locational or dimensional variables, but the locational variables can be used for conditioning, or for the construction of simple maps.
As a prelude to any analysis with spatial data, construction of a basic map is called for.
Simple maps of a gridded climate data set for Oregon [orgrid.csv]
# set up -- do these once
library(maptools)
library(gpclib)
library(RColorBrewer)
# attach climate data
orgrid <- read.csv(file.choose())
attach(orgrid)
# read a county outline shape file
orotl.shp <- readShapeLines(file.choose(),
proj4string=CRS("+proj=longlat"))
# set up for maps of individual variables
plotvar <- tann # pick a variable to plot
plottitle <- "Annual Temperature"
nclr <- 8 # number of colors
plotclr <- brewer.pal(nclr,"PuOr") # get the colors
plotclr <- plotclr[nclr:1] # reorder colors if appropriate
brks <- round(quantile(plotvar, probs=seq(0,1,1/nclr)), digits=1)
colornum <- as.integer(cut(plotvar, brks, include.lowest=T))
colcode <- plotclr[colornum] # assign color
# plot the county outline shape file and the variable
plot(orotl.shp, xlim=c(-125, -115), ylim=c(42,47), axes=T)
points(lon, lat, pch=15, col=colcode, cex=1.0)
text(-120, 46.5, plottitle)
# add legend
legend(-116.15, 44, legend=leglabs(brks), fill=plotclr, cex=0.6, bty="o")
plot(orotl.shp, add=T)
Exploratory data analysis -- more elaborate plots
A basic description of the spatial variations in a variable can be constructed using lattice plots, with location or coordinate data as conditioning variables. When the data are arranged on a grid (i.e. in rows and columns), the conditioning can be done by row or column, and the resulting plots examined for their overall shapes and trends. Three types of plots include lattice plots of a variable "by" a locational variable, "marginal" plots of a variable by rows or columns, or scatter diagrams that are constructed to show the relationships between adjacent rows or columns. Once again, as befits descriptive plots for visualizing data, the plots themselves can be used to illustrate their application more efficiently that a verbal description can.
library(lattice)
trellis.device(color=TRUE, theme = "col.whitebg")
# lattice boxplots by strata
bwplot(lat ~ tann, ylab="Row")
bwplot(tann ~ lon, xlab="Col", horizontal=F)
# row and column marginal plots
par(mfrow=c(2,2))
plot(tapply(tann, lat, median), 1:51, xlab="tann", ylab="Rows", pch=1)
points(tapply(tann, lat, mean), 1:51, pch=2)
plot(1:49, tapply(tann, lon, median), xlab="tann", ylab="Columns", pch=1)
points(1:49, tapply(tann, lon, mean), pch=2)
# bivariate plots between adjacent rows and columns
grid.mat <- tapply(tann, list(factor(lon), factor(lat)), function(x)x)
plot(grid.mat[,-1],grid.mat[,-51], xlab="tann in column Z", ylab="tann in co1umn Z+1")
plot(grid.mat[-1,],grid.mat[-49,], xlab="tann in row Z",
ylab="tann in row Z+1")
detach(orgrid)