I have survey data of species richness that was taken at various sites in the Chesapeake Bay, USA, and I would like to graphically present the data as a "heat map."
I have a dataframe of lat/long coordinates and richness values, which I converted into a SpatialPointsDataFrame
and used the autoKrige()
function from the automap package to generate the interpolated values.
First, can anyone comment as to whether I am correctly implementing the autoKrige()
function?
Second, I am having trouble plotting the data and overlaying a map of the region. Alternately, could I specify the interpolation grid to reflect the borders of the Bay (as suggested here)? Any thoughts on how I might do that and where I might get that information? Supplying the grid to autoKrige()
appears easy enough.
EDIT: Thanks to Paul for his super helpful post! Here is what I have now. Having trouble getting ggplot to accept both the interpolated data and the map projection:
require(rgdal)
require(automap)
#Generate lat/long coordinates and richness data
set.seed(6)
df=data.frame(
lat=sample(seq(36.9,39.3,by=0.01),100,rep=T),
long=sample(seq(-76.5,-76,by=0.01),100,rep=T),
fd=runif(10,0,10))
initial.df=df
#Convert dataframe into SpatialPointsDataFrame
coordinates(df)=~long+lat
#Project latlong coordinates onto an ellipse
proj4string(df)="+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
#+proj = the type of projection (lat/long)
#+ellps and +datum = the irregularity in the ellipse represented by planet earth
#Transform the projection into Euclidean distances
project_df=spTransform(df, CRS("+proj=merc +zone=18s +ellps=WGS84 +datum=WGS84")) #projInfo(type="proj")
#Perform the interpolation using kriging
kr=autoKrige(fd~1,project_df)
#Extract the output and convert to dataframe for easy plotting with ggplot2
kr.output=as.data.frame(kr$krige_output)
#Plot the output
#Load the map data for the Chesapeake Bay
cb=data.frame(map("state",xlim=range(initial.df$long),ylim=range(initial.df$lat),plot=F)[c("x","y")])
ggplot()+
geom_tile(data=kr.output,aes(x=x1,y=x2,fill=var1.pred))+
geom_path(data=cb,aes(x=x,y=y))+
coord_map(projection="mercator")
See Question&Answers more detail:os