使用 shapefile 在 R 中创建连通性直方图

Creating a connectivity histogram in R using shapefiles

GeoDa on a data set of the US Census Shapefiles I can quickly create a connectivity histogram 中工作时如下所示:

假设我的数据来源如下:

# Download an read US state shapefiles
tmp_shps <- tempfile(); tmp_dir <- tempdir()
download.file("http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip",
              tmp_shps)
unzip(tmp_shps, exdir = tmp_dir)
# Libs
require(rgdal); require(ggplot2)
# Read
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m")

如何在 R 中得到类似的连通性直方图?另外,我有兴趣创建一个从以下列方式创建的距离矩阵派生的有意义的直方图:

require(geospacom)
dzs_distmat <- DistanceMatrix(poly = us_shps, id = "GEOID", 
                              unit = 1000, longlat = TRUE, fun = distHaversine)

在实践中,我有兴趣实现以下目标:

  1. 总结地理区域彼此边界的频率,最好是通过上面显示的连通性直方图
  2. 汇总有关地理区域间距离的信息

我试了一下。这似乎是一个开始。

关于你的第二点。你可以说得更详细点吗?我想一个简单的直方图或密度图就可以很好地概括? IE。类似于:

dists <- dzs_distmat[lower.tri(dzs_distmat)]
hist(dists, xlab = "Dist", 
     main = "Histogram of distances",
     col = "grey")
abline(v = mean(dists), col = "red", lwd = 2)

关于您的第一点,以下应该是您提供的直方图的非常普通的版本。 (但它看起来不太像?!)它应该是跟随this post.

的触摸邻居数量的直方图
library("rgeos")

# Get adjencency matrix
adj <- gTouches(us_shps, byid = TRUE)
# Add names
tmp <- as.data.frame(us_shps)$STATEFP
dimnames(adj) <- list(tmp, tmp)
# Check names
stopifnot(all(rownames(adj) == rownames(dzs_distmat))) # Sanity check

hist(rowSums(adj), col = "grey", main = "Number of neighbours", 
     breaks = seq(-0.5, 8.5, by = 1))

我猜花哨的颜色可以相对容易地添加。

使用 spdep,您可以使用 poly2nb 函数识别区域的空间邻域,然后使用 card 函数绘制直方图。例如:

nb_q <- poly2nb(us_shp, queen = T)

hist(card(nb_q), col = "grey", main = "Number of neighbours", breaks = seq(-0.5, 8.5, by = 1))