2

While working in GeoDa on a data set of the US Census Shapefiles I can quickly create a connectivity histogram shown below:

Connectivity Histogram

Assuming that my data is sourced in the following manner:

# 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")

How can I arrive at a similar connectivity histogram in R? Addittionally, I would be interested in creating a meanigful histogram derived from distance matrix created in the following manner:

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

In practice, I'm interested in achieving the following objectives:

  1. Summarising how often geographies border one another, ideally through a connectivity histogram shown above
  2. Summarising information on distances amongst geographies
Konrad
  • 17,740
  • 16
  • 106
  • 167

2 Answers2

1

I played around with it a bit. This seems to be a start.

For your second point. Can you be more specific? I guess a simple histogram or density plot would summarise just fine? I.e. something like:

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)

Imgur

Regarding your first point, the following should be a very non-fancy version of the histogram you present. (But it doesn't look like it very much?!) It should be a histogram of the number of touching neighbours following 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))

Imgur

I guess the fancy colours can be added relatively easily.

Community
  • 1
  • 1
Anders Ellern Bilgrau
  • 9,928
  • 1
  • 30
  • 37
  • Thanks very much for showing interest in my post. I would guess that the density plot would suffice. I asked as I was wondering whether anyone would be interested in proposing some creative solutions, similar to those discussed [here](http://stackoverflow.com/q/3081066/1655567). – Konrad Oct 01 '15 at 12:30
  • I was wondering whether it would be possible to derive the relevant data from the neighbours object created by the `spdep` package as it enables to use *rook* */* *queen* continuity. – Konrad Mar 13 '16 at 09:37
1

Using spdep you could identify the spatial neighbors of the regions using the the poly2nb function and then plot the histogram using the card function. Ex:

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))
Pedro VCO
  • 11
  • 2