-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdc-r-presentation.Rmd
436 lines (319 loc) · 7.8 KB
/
dc-r-presentation.Rmd
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
---
title: "Data Science? Make it Spatial"
author: "DC R Conference 2018 <br />Angela Li <br />University of Chicago<br />"
date: '`r Sys.Date()`'
output:
xaringan::moon_reader:
lib_dir: libs
css: ["xaringan-themer.css", "footer.css"]
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
library(tidyverse)
library(sf)
```
```{r xaringan-themer, include=FALSE}
library(xaringanthemer)
duo_accent(
primary_color = "#767676",
secondary_color = "#800000",
title_slide_text_color = "#FEFEFE",
header_font_google = google_font("Montserrat"),
text_font_google = google_font("Montserrat", "400", "400i"),
code_font_google = google_font("Source Code Pro"),
table_row_even_background_color = "#D6D6CE"
)
```
# About me
![](img/rladies_angela.jpg)
--
- R Spatial Advocate at Center for Spatial Data Science, UChicago
--
- Founder of R-Ladies Chicago
--
- Wrote a thesis using spatial econometrics...
--
- About property values in Detroit...
--
- Under Luc Anselin, who literally wrote the book on spatial econometrics
---
class: inverse, center, middle
# Geography?
---
![](img/middle-earth.jpg)
---
class: inverse, center, middle
# Geography!
---
![](img/csds-cdph.png)
---
class: inverse, middle, center
# What's exploratory *spatial* data analysis (ESDA)?
---
class: inverse, middle, center
# Making lots of maps!
![](https://media.giphy.com/media/3ov9kbxzfzV6HtnnwI/giphy-downsized.gif)
---
<!-- # Read in data -->
<!-- I can read in straight GeoJSON files with `sf` -->
# Data: crimes in DC in the last 30 days
```{r warning=FALSE, message=FALSE}
library(sf)
dc_crime <- st_read("https://opendata.arcgis.com/datasets/dc3289eab3d2400ea49c154863312434_8.geojson")
```
---
# Look at observations
```{r echo=FALSE, warning=FALSE}
library(janitor)
library(DT)
dc_crime <- clean_names(dc_crime) %>%
select(-census_tract) # For expository purposes, getting rid of this
```
```{r eval=FALSE}
head(dc_crime)
```
```{r echo=FALSE}
dc_crime %>%
mutate(attributes = "...") %>%
select(report_dat, offense, attributes, geometry) %>%
head()
```
---
# Map it!
```{r out.width='100%', fig.height=4.5, eval=require('leaflet')}
library(leaflet)
leaflet() %>%
addTiles() %>%
addCircles(data = dc_crime, color = "#800000")
```
---
# Change into a heatmap
```{r out.width='100%', fig.height=4, eval=require('leaflet'), warning=FALSE}
library(leaflet.extras)
leaflet() %>%
addTiles() %>%
addHeatmap(data = dc_crime, max = 0.7, blur = 60)
```
---
class: center
# The problem with heat maps
![](img/xkcd-heatmap.png)
---
class: inverse, middle, center
# I want to adjust for population
--
## But, population data is collected in specific geographic units
---
# Census tract polygons!
```{r echo=FALSE, warning=FALSE, message=FALSE}
dc_tracts <- readRDS("dc_tracts.Rda") %>%
rename(census_tract = tractce)
```
```{r out.width='100%', fig.height=5, eval=require('leaflet'), warning=FALSE}
leaflet() %>%
addTiles() %>%
addPolygons(data = dc_tracts, weight = 2)
```
---
# Add crimes as a layer
```{r warning = FALSE, out.width='100%', fig.height=4.5, eval=require('leaflet')}
leaflet() %>%
addTiles() %>%
addPolygons(data = dc_tracts, weight = 2) %>%
addCircles(data = dc_crime, col = "red", radius = 0.01)
```
---
class: inverse, middle, center
# How many crimes happened in each tract?
--
## Spatial aggregation
--
## Point-in-polygon analysis
---
# Count points in polygons
```{r eval=FALSE, error=TRUE}
st_join(dc_crime, dc_tracts["census_tract"])
```
```{r}
# Error: st_crs(x) == st_crs(y) is not TRUE
```
--
## What's the error???
---
class: inverse, center, middle
# Warning: worst part of working with spatial data ahead
---
class: center
# Projections!
![](img/julia-projection.png)
---
class: center, middle, inverse
# Dealing with projections = spatial data munging
---
class: center
# What's actually going on
![](img/projection.png)
---
# Check the projections!
--
```{r}
st_crs(dc_crime)
```
--
```{r}
st_crs(dc_tracts)
```
---
# Make them the same
```{r}
dc_crime <- st_transform(dc_crime, 102285)
dc_tracts <- st_transform(dc_tracts, 102285)
```
--
```{r}
st_crs(dc_crime)
```
--
```{r}
st_crs(dc_tracts)
```
---
# Try the spatial join again
```{r }
dc_crime <- st_join(dc_crime, dc_tracts["census_tract"])
```
```{r echo=FALSE}
dc_crime %>%
mutate(attributes = "...") %>%
select(census_tract, report_dat, offense, attributes, geometry) %>%
head(4)
```
---
class: inverse, middle, center
# It worked!
---
# Map crime counts
```{r echo=FALSE, warning=FALSE}
crimes_by_tract <- dc_crime %>%
group_by(census_tract) %>%
tally() %>%
`st_geometry<-`(NULL) %>%
left_join(dc_tracts, ., by = "census_tract") %>%
rename(number_crimes = n)
```
```{r echo=FALSE, warning=FALSE, fig.height=6}
library(tmap)
tm_shape(crimes_by_tract) +
tm_polygons("number_crimes")
```
---
class: inverse, center, middle
# This map makes a cartographer sad :(
--
## We're mapping a spatially extensive variable
--
## Aka the count is related to the area of the tract
---
# Fix this by dividing by population
```{r warning=FALSE, message=FALSE}
library(tidycensus)
dc_pop <- get_decennial(geography = "tract",
variables = c("H0100001"),
state = "DC")
```
```{r echo=FALSE, warning=FALSE}
dc_pop <- dc_pop %>%
mutate(census_tract = str_sub(GEOID, 6, 11)) %>%
rename(pop = value)
```
--
```{r warning=FALSE}
head(dc_pop, 4)
```
---
# Map crime rates per capita
```{r echo=FALSE, warning=FALSE, fig.height=6}
crime_per_cap <- dc_pop %>%
left_join(crimes_by_tract, ., by = "census_tract") %>%
mutate(crime_by_capita = number_crimes/pop)
```
```{r echo=FALSE, fig.height=5}
tm_shape(filter(crime_per_cap, crime_by_capita < .1)) +
tm_polygons("crime_by_capita", style = "jenks")
```
---
class: inverse, middle, center
# Are crime rates and median income related?
---
# Get median income by tract
```{r}
dc_inc <- get_acs(geography = "tract",
variables = c("B19013_001"),
state = "DC")
```
---
# Get median income by tract
```{r}
head(dc_inc)
```
---
# Map crimes and median income
```{r echo=FALSE}
dc_inc <- clean_names(dc_inc) %>%
mutate(census_tract = str_sub(geoid, 6, 11)) %>%
select(med_inc = estimate, census_tract) %>%
left_join(crime_per_cap, .,
by = "census_tract")
```
```{r echo=FALSE, fig.height=5}
tm_shape(dc_inc) +
tm_polygons("med_inc") +
tm_shape(dc_crime) +
tm_dots(size = 0.05)
```
<!-- --- -->
<!-- # Add a basemap and a legend -->
<!-- ```{r eval=FALSE} -->
<!-- tmap_mode("view") -->
<!-- tm_shape(dc_inc) + -->
<!-- tm_polygons("med_inc") + -->
<!-- tm_shape(dc_crime) + -->
<!-- tm_dots() + -->
<!-- tm_view(alpha = 1, -->
<!-- basemaps = "CartoDB.Positron", -->
<!-- legend.position = c("left", "bottom")) -->
<!-- ``` -->
<!-- --- -->
<!-- # Add a basemap and a legend -->
<!-- ```{r echo=FALSE, fig.height=5} -->
<!-- tmap_mode("view") -->
<!-- tm_shape(dc_inc) + -->
<!-- tm_polygons("med_inc") + -->
<!-- tm_shape(dc_crime) + -->
<!-- tm_dots() + -->
<!-- tm_view(alpha = 1, -->
<!-- basemaps = "CartoDB.Positron", -->
<!-- legend.position = c("left", "bottom")) -->
<!-- ``` -->
---
class: center, middle, inverse
# I'll end it here, but...
---
class: center, middle
## There might be other local demographic & socioeconomic factors that affect crime rates
### (spatial regression, modeling)
---
class: center, middle
## Crime in one area may be linked to crime in another area
### (spatial autocorrelation)
---
class: center, middle
## I might be interested in spots where crime is abnormally high or low
### (spatial clustering)
---
class: inverse, center, middle
# Thanks!