This repository was archived by the owner on Feb 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounty_dendist_map.Rmd
More file actions
264 lines (213 loc) · 12.1 KB
/
county_dendist_map.Rmd
File metadata and controls
264 lines (213 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
---
title: "County Density/Distance/Population Map"
author: "Jeff Erickson"
date: "Tuesday, June 17, 2014"
output: html_document
---
### LOAD REQUIRED LIBRARIES
```{r}
library(rgdal)
library(ggmap)
library(ggplot2)
library(rgeos)
library(knitr)
library(sqldf)
```
### IMPORT DATA
```{r}
#County Shapes from US Census
county.shapes <- readOGR(dsn="datasets", "gz_2010_us_050_00_500k")
#County population centroids from US Census
county.pop.center <- read.csv("datasets/CenPop2010_Mean_CO.txt", header=TRUE)
#County population density information from US Census
county.density <- read.csv("datasets/DEC_10_SF1_GCTPH1.CY07_with_ann.csv", header=TRUE, as.is=TRUE)
#Major city (primary statistical areas) from gen.maj.city.long.lat.Rmd
maj.city.long.lat <- read.csv("datasets/maj.city.long.lat.csv", header=TRUE)
#Google driving distances from https://github.com/jefferickson/county-city-driving-dist
county.city.driving.dist <- read.csv("datasets/county-city-driving-dist.csv", header=TRUE, as.is=TRUE)
```
### HELPER FUNCTIONS/DEFINITIONS
```{r}
missoula.fips <- "0500000US30063"
westchester.fips <- "0500000US36119"
deg.2.rad <- function(x) { #Convert vector from degrees to radians
return(sapply(x, function(deg) {
return(deg * (pi/180))
}))
}
euclid.dist <- function(x) { #Simple Euclidean distance
#arg: c(lat1, long1, lat2, long2)
return(sqrt((x[1]-x[3])^2 + (x[2]-x[4])^2))
}
haversine.dist <- function(x) {
#Implementation of the haversine formula
#http://en.wikipedia.org/wiki/Haversine_formula
#http://stackoverflow.com/questions/27928/how-do-i-calculate-distance-between-two-latitude-longitude-points
#arg: c(lat1, long1, lat2, long2)
coords <- deg.2.rad(x)
earth.r <- 6371 #earth radius in km
d.long.rad <- coords[2] - coords[4]
d.lat.rad <- coords[1] - coords[3]
a <- sin(d.lat.rad/2)^2 + cos(coords[1]) * cos(coords[3]) * sin(d.long.rad/2)^2
c <- 2 * atan2(sqrt(a), sqrt(1 - a))
d <- earth.r * c
return(d)
}
google.driving.dist <- function(x) {
#Get Google driving distance
#http://stackoverflow.com/questions/16863018/getting-driving-distance-between-two-points-lat-lon-using-r-and-google-map-ap
#Has the following limitations:
#100 elements per query.
#100 elements per 10 seconds.
#2500 elements per 24 hour period.
#arg: c(lat1, long1, lat2, long2)
library(XML)
library(RCurl)
origin <- paste(x[1], x[2], sep=",")
dest <- paste(x[3], x[4], sep=",")
url <- paste0('http://maps.googleapis.com/maps/api/distancematrix/xml?origins=', origin, '&destinations=', dest, '&mode=driving&sensor=false')
xml.resp <- xmlParse(getURL(url))
d.lab <- xmlValue(xmlChildren(xpathApply(xml.resp, "//distance")[[1]])$text)
d <- as.numeric(sub(" km", "", d.lab))
return(d)
}
z.score <- function(x) { #Calc z-scores
#arg: a vector of values to z-score
return((x - mean(x)) / sd(x))
}
link.func.unwgted.avg <- function(x) { #Unweighted average of each argument (row-by-row), then scaled to [0,1]
#arg: data.frame of numerics to be averaged and scaled
index.unscaled <- apply(x, 1, function(x) { return(mean(x)) })
index.shifted <- index.unscaled - min(index.unscaled)
index.scaled <- index.shifted / max(index.shifted)
return(index.scaled)
}
```
### DATA TRANSFORMATIONS
```{r}
#We only want counties, not census tracts
county.density <- county.density[which(county.density$GEO.id2 == county.density$GCT_STUB.target.geo.id2), ]
#Create key that is used to merge
county.pop.center$GEO_ID <- paste("0500000US", formatC(county.pop.center$STATEFP, width=2, flag="0"), formatC(county.pop.center$COUNTYFP, width=3, flag="0"), sep="")
#For now, only the lower 48 states
county.density <- county.density[which(! (substr(county.density$GEO.id, 10, 11) %in% c("02", "52", "15", "72"))), ] #remove AK, HI, PR (2 codes)
county.pop.center <- county.pop.center[which(! (substr(county.pop.center$GEO_ID, 10, 11) %in% c("02", "52", "15", "72"))), ] #remove AK, HI, PR (2 codes)
#Convert field(s) to numeric. Remove (r[number]) pattern found in some HD01 values.
county.density$SUBHD0401 <- as.numeric(county.density$SUBHD0401)
county.density$HD01 <- as.numeric(gsub("\\(r[0-9]*\\)", "", county.density$HD01))
#clean up the driving distance data
county.city.driving.dist <- county.city.driving.dist[c("fips", "city_id", "driving_distance")]
```
### CALCULATE DISTANCES AND DEN-DIST-POP INDEX
```{r}
#Create a list of all unique combinations of counties and the major cities
county.city.list.long <- merge(county.pop.center$GEO_ID, maj.city.long.lat$id, all.x=TRUE, all.y=TRUE)
names(county.city.list.long) <- c("county.geo.id", "city.id")
#For each combination, calculate the distance
county.city.list.long$dist <- apply(county.city.list.long[c("county.geo.id", "city.id")], 1, function(x) {
dist <- haversine.dist(as.numeric(
c(
county.pop.center[which(county.pop.center$GEO_ID == x[1]), c("LATITUDE", "LONGITUDE")],
maj.city.long.lat[which(maj.city.long.lat$id == x[2]), c("lat", "long")]
)
))
return(dist)
})
#Merge on the county densities/populations
county.city.list.long <- merge(county.city.list.long, county.density[c("GEO.id", "SUBHD0401", "HD01")], by.x="county.geo.id", by.y="GEO.id", all.x=TRUE)
#Merge the driving distances
county.city.list.long <- merge(county.city.list.long, county.city.driving.dist, by.x=c("county.geo.id", "city.id"), by.y=c("fips", "city_id"))
#Find the shortest of the distances (driving and otherwise)
county.city.list.shortest <- sqldf("select `county.geo.id` as county_geo_id, SUBHD0401, HD01, min(dist) as dist, min(driving_distance) as driving_dist from `county.city.list.long` group by `county.geo.id`")
#transform density and population with log
county.city.list.shortest$den.log <- log(county.city.list.shortest$SUBHD0401)
county.city.list.shortest$pop.log <- log(county.city.list.shortest$HD01)
#let's "flip" the distances so that high values of both density and "distance" mean more urban than rural
county.city.list.shortest$dist.flip <- -(county.city.list.shortest$dist - (max(county.city.list.shortest$dist) + 1))
county.city.list.shortest$drive.dist.flip <- -(county.city.list.shortest$driving_dist - (max(county.city.list.shortest$driving_dist) + 1))
#z-score transforms of density and population and distance-flip
county.city.list.shortest$den.log.z <- z.score(county.city.list.shortest$den.log)
county.city.list.shortest$pop.log.z <- z.score(county.city.list.shortest$pop.log)
county.city.list.shortest$dist.flip.z <- z.score(county.city.list.shortest$dist.flip)
county.city.list.shortest$drive.dist.flip.z <- z.score(county.city.list.shortest$drive.dist.flip)
#create the den-dist-pop index
county.city.list.shortest$den.dist.pop.index <- link.func.unwgted.avg(county.city.list.shortest[c("den.log.z", "pop.log.z", "dist.flip.z")])
county.city.list.shortest$den.drivedist.pop.index <- link.func.unwgted.avg(county.city.list.shortest[c("den.log.z", "pop.log.z", "drive.dist.flip.z")])
#what is the difference between the two?
county.city.list.shortest$index.diff <- county.city.list.shortest$den.dist.pop.index - county.city.list.shortest$den.drivedist.pop.index
#create a {-1, 0, 1} flag for index value relative to Missoula County
county.city.list.shortest$rel.missoula <- apply(county.city.list.shortest, 1, function(x) {
if (x["county_geo_id"] == missoula.fips) {
return(0)
} else if (x["den.drivedist.pop.index"] < county.city.list.shortest$den.drivedist.pop.index[which(county.city.list.shortest$county_geo_id == missoula.fips)]) {
return(-1)
} else if (x["den.drivedist.pop.index"] > county.city.list.shortest$den.drivedist.pop.index[which(county.city.list.shortest$county_geo_id == missoula.fips)]) {
return(1)
} else {
return(NA)
}
})
county.city.list.shortest$rel.missoula <- factor(county.city.list.shortest$rel.missoula, levels=c(-1, 0, 1), labels=c("More Rural than Missoula", "Missoula", "Less Rural than Missoula"))
```
### MERGE DEN-DIST-POP DATA WITH MAPPING INFO
```{r}
#Fortify the shapefile and use the FIPS codes as id
county.shapes.f <- fortify(county.shapes, region="GEO_ID")
#Merge on our demo data
county.shapes.f.dendistpop <- merge(county.shapes.f, county.city.list.shortest, by.x="id", by.y="county_geo_id")
```
### GRAPH THE DENSITY OF THE INDEX
```{r}
ggplot(county.city.list.shortest, aes(x=den.dist.pop.index)) + geom_density()
ggplot(county.city.list.shortest, aes(x=den.drivedist.pop.index)) + geom_density()
```
### MAP IT!
```{r}
ggplot(county.shapes.f.dendistpop[order(county.shapes.f.dendistpop$order), ], aes(x=long, y=lat, group=group)) + #careful: order matters here
geom_polygon(aes(fill=den.dist.pop.index), colour="black") +
coord_equal() +
scale_fill_gradientn(values=c(0, median(county.shapes.f.dendistpop$den.dist.pop.index), 1), colours=c("red", "white", "green"), rescaler=function(x, ...) x, oob=identity)
ggplot(county.shapes.f.dendistpop[order(county.shapes.f.dendistpop$order), ], aes(x=long, y=lat, group=group)) + #careful: order matters here
geom_polygon(aes(fill=den.drivedist.pop.index), colour="black") +
coord_equal() +
scale_fill_gradientn("Den-Dist-Pop\nIndex", values=c(0, median(county.shapes.f.dendistpop$den.drivedist.pop.index), 1), colours=c("red", "white", "green"), rescaler=function(x, ...) x, oob=identity) +
ggtitle(expression(atop("Density-Distance-Population Index", atop("By County, Compared to Median", "")))) +
theme(axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank(), panel.grid=element_blank())
ggplot(county.shapes.f.dendistpop[order(county.shapes.f.dendistpop$order), ], aes(x=long, y=lat, group=group)) + #careful: order matters here
geom_polygon(aes(fill=index.diff), colour="black") +
coord_equal() +
scale_fill_gradientn("Difference", values=c(-0.07, 0, 0.07), colours=c("green", "white", "red"), rescaler=function(x, ...) x, oob=identity) +
ggtitle(expression(atop("Driving Distance vs. \"As the Crow Flies\"", atop("Index Difference", "")))) +
theme(axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank(), panel.grid=element_blank())
```
### CONCLUSIONS
```{r}
county.w.index <- merge(county.city.list.shortest[c("county_geo_id", "den.dist.pop.index", "den.drivedist.pop.index")], county.density[c("GEO.id", "GEO.display.label")], by="county_geo_id", by.y="GEO.id")
```
```{r}
ggplot(county.city.list.shortest, aes(x=den.drivedist.pop.index)) +
geom_density() +
geom_vline(xintercept=county.city.list.shortest$den.drivedist.pop.index[which(county.city.list.shortest$county_geo_id == missoula.fips)], linetype="longdash", colour="red") +
geom_text(x=county.city.list.shortest$den.drivedist.pop.index[which(county.city.list.shortest$county_geo_id == missoula.fips)] + 0.02, y=1, label="MISSOULA COUNTY, MT", colour="red", angle=90) +
geom_vline(xintercept=county.city.list.shortest$den.drivedist.pop.index[which(county.city.list.shortest$county_geo_id == westchester.fips)], linetype="longdash", colour="red") +
geom_text(x=county.city.list.shortest$den.drivedist.pop.index[which(county.city.list.shortest$county_geo_id == westchester.fips)] + 0.02, y=1.1, label="WESTCHESTER COUNTY, NY", colour="red", angle=90) +
ggtitle(expression(atop("Density-Distance-Population Index", atop("Density Plot", "")))) +
scale_x_continuous("Den-Dist-Pop Index") +
scale_y_continuous("Density (%)")
```
```{r}
kable(head(county.w.index[order(county.w.index$den.dist.pop.index), ]), format="markdown", row.names=FALSE)
kable(head(county.w.index[order(county.w.index$den.drivedist.pop.index), ]), format="markdown", row.names=FALSE)
```
```{r}
kable(tail(county.w.index[order(county.w.index$den.dist.pop.index), ]), format="markdown", row.names=FALSE)
kable(tail(county.w.index[order(county.w.index$den.drivedist.pop.index), ]), format="markdown", row.names=FALSE)
```
```{r}
ggplot(county.shapes.f.dendistpop[order(county.shapes.f.dendistpop$order), ], aes(x=long, y=lat, group=group)) + #careful: order matters here
geom_polygon(aes(fill=rel.missoula), colour="black") +
coord_equal() +
scale_fill_discrete("Relation to Missoula") +
ggtitle(expression(atop("Density-Distance-Population Index", atop("By County, Relation to Missoula County", "")))) +
theme(axis.title=element_blank(), axis.text=element_blank(), axis.ticks=element_blank(), panel.grid=element_blank())
```