我不确定我可以在空间平滑方面提供多少帮助,因为这是一项我几乎没有经验的任务,但我花了一些时间在 R 中制作地图,所以我希望我下面添加的内容将对这部分有所帮助你的问题。
我已经开始编辑你的代码# # # shapefile read-in # # #
;你会注意到我把地图放在SpatialPolygonsDataFrame
类和我依靠raster
and gstat
用于构建网格并运行空间平滑的软件包。空间平滑模型是我最不熟悉的部分,但这个过程使我能够制作栅格并演示如何对其进行遮罩、投影和绘制。
library(rgdal)
library(raster)
library(gstat)
# read in a base map
m <- getData("GADM", country="United States", level=1)
m <- m[!m$NAME_1 %in% c("Alaska","Hawaii"),]
# specify the tiger file to download
tiger <- "ftp://ftp2.census.gov/geo/tiger/TIGER2010/CBSA/2010/tl_2010_us_cbsa10.zip"
# create a temporary file and a temporary directory
tf <- tempfile() ; td <- tempdir()
# download the tiger file to the local disk
download.file( tiger , tf , mode = 'wb' )
# unzip the tiger file into the temporary directory
z <- unzip( tf , exdir = td )
# isolate the file that ends with ".shp"
shapefile <- z[ grep( 'shp$' , z ) ]
# read the shapefile into working memory
cbsa.map <- readOGR( shapefile, layer="tl_2010_us_cbsa10" )
# remove CBSAs ending with alaska, hawaii, and puerto rico
cbsa.map <- cbsa.map[ !grepl( "AK$|HI$|PR$" , cbsa.map$NAME10 ) , ]
# cbsa.map$NAME10 now has a length of 933
length( cbsa.map$NAME10 )
# extract centroid for each CBSA
cbsa.centroids <- data.frame(coordinates(cbsa.map), cbsa.map$GEOID10)
names(cbsa.centroids) <- c("lon","lat","GEOID10")
# add lat lon to popualtion data
nrow(x)
x <- merge(x, cbsa.centroids, by="GEOID10")
nrow(x) # centroids could not be assigned to all records for some reason
# create a raster object
r <- raster(nrow=500, ncol=500,
xmn=bbox(m)["x","min"], xmx=bbox(m)["x","max"],
ymn=bbox(m)["y","min"], ymx=bbox(m)["y","max"],
crs=proj4string(m))
# run inverse distance weighted model - modified code from ?interpolate...needs more research
model <- gstat(id = "trinary", formula = trinary~1, weights = "weight", locations = ~lon+lat, data = x,
nmax = 7, set=list(idp = 0.5))
r <- interpolate(r, model, xyNames=c("lon","lat"))
r <- mask(r, m) # discard interpolated values outside the states
# project map for plotting (optional)
# North America Lambert Conformal Conic
nalcc <- CRS("+proj=lcc +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs")
m <- spTransform(m, nalcc)
r <- projectRaster(r, crs=nalcc)
# plot map
par(mar=c(0,0,0,0), bty="n")
cols <- c(rgb(0.9,0.8,0.8), rgb(0.9,0.4,0.3),
rgb(0.8,0.8,0.9), rgb(0.4,0.6,0.9),
rgb(0.8,0.9,0.8), rgb(0.4,0.9,0.6))
col.ramp <- colorRampPalette(cols) # custom colour ramp
plot(r, axes=FALSE, legend=FALSE, col=col.ramp(100))
plot(m, add=TRUE) # overlay base map
legend("right", pch=22, pt.bg=cols[c(2,4,6)], legend=c(0,1,2), bty="n")