-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-analysis-part1.R
More file actions
55 lines (45 loc) · 1.84 KB
/
02-analysis-part1.R
File metadata and controls
55 lines (45 loc) · 1.84 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
# This script generates a couple simple maps from the CTPP Part 1 data that were
# read into a Monet database in the 01 script.
# Specifically, it pulls down mode share information for Harris County, TX and
# plots a couple choropleth maps.
library(DBI)
library(ggplot2)
library(dplyr)
library(tidyr)
library(tigris)
# Connect to the local Monet database
dbdir <- "monet_ctpp"
con <- dbConnect(MonetDBLite::MonetDBLite(), dbdir)
harris_res <- dbGetQuery(
con,
"SELECT lookupres.geoid, st, cty, tr, lineno, est, se FROM a102106
INNER JOIN lookupres ON
a102106.geoid = lookupres.geoid
WHERE sumlevel = 'C11' AND
st = '48' AND
cty = '201'")
harris_res$geoid10 <- paste0(harris_res$st, harris_res$cty, harris_res$tr)
# Get "lineno" information from the table shell
mode_shares_res <- dbGetQuery(
con, "
SELECT * FROM tableshell
WHERE tblid = 'A102106'")
# https://stackoverflow.com/questions/28100780/use-with-replacement-functions-like-colnames
harris_res_wide <- harris_res %>%
select(geoid10, lineno, est) %>%
spread(lineno, est, fill = 0) %>%
`colnames<-`(c("geoid10", "total", "da", "cp2", "cp3", "cp4", "cp56", "cp7p",
"bus", "streetcar", "subway", "railroad", "ferry", "bike", "walk",
"taxi", "motorcycle", "other", "telecommute")) %>%
mutate(carpool = cp2 + cp3 + cp4 + cp56 + cp7p,
transit = bus + streetcar + subway,
dashare = da / total,
transhare = transit / total)
# Retreive Harris county tracts from tigris
tracts_harris <- tracts("TX", county = "201")
# Join and map part 1 data
tracts_wgeo <- inner_join(tracts_harris, harris_res_wide,
by = c("GEOID" = "geoid10"))
ggplot() + geom_sf(data = tracts_wgeo, aes(fill = transhare))
ggplot() + geom_sf(data = tracts_wgeo, aes(fill = dashare))
dbDisconnect(con, shutdown = TRUE)