-
Notifications
You must be signed in to change notification settings - Fork 1
/
spatial_maps.Rmd
166 lines (119 loc) · 3.72 KB
/
spatial_maps.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
---
title: "Spatial trends/visualization"
---
```{r setup, include=FALSE}
library(tidyverse)
library(plotly)
library(rgdal)
library(plotly)
library(maps)
library(devtools)
library(leaflet)
library(BAMMtools)
library(spdep)
library(maptools)
```
```{r include = FALSE}
# import dog_bite_12.4
dog_bite <- read_csv('./data/dog_bite_12.4.csv') %>%
mutate(
zip_code_imputed = as.character(zip_code_imputed)
)
```
```{r echo = FALSE}
# count the number of dog bite incidence by zip code
bite_count <- dog_bite %>%
group_by(zip_code_imputed) %>%
summarize(n_bite = n())
# for pit bulls
pit_bite <- dog_bite %>%
filter(
str_detect(breed, '[pP][iI][Tt].[Bb][Uu][Ll][Ll]')
) %>%
group_by(zip_code_imputed) %>%
summarize(
n_pit = n()
)
```
```{r include = FALSE}
# zip code tabulation area
# https://data.cityofnewyork.us/Business/Zip-Code-Boundaries/i8iw-xf4u/data
# import the shape file
# https://plotly-book.cpsievert.me/maps.html
zip_map <- readOGR(dsn = './data/ZIP_CODE_040114/ZIP_CODE_040114.shp', encoding = "UTF-8")
zip_map@data <- left_join(zip_map@data, bite_count, by = c('ZIPCODE' = 'zip_code_imputed'))
zip_map@data <- left_join(zip_map@data, pit_bite, by = c('ZIPCODE' = 'zip_code_imputed'))
# assign 0 for zip codes that have no match in the dog bite data
zip_map$n_bite[is.na(zip_map$n_bite)] <- 0
zip_map$n_pit[is.na(zip_map$n_pit)] <- 0
```
```{r echo = FALSE}
# CRS setting
zip_map_crs <- spTransform(zip_map, CRS("+init=epsg:4326"))
# export the json file
writeOGR(zip_map_crs, './data/zip_map_geojson', layer = 'zip_map', driver = 'GeoJSON')
```
```{r include = FALSE}
# Layout
# format of the label that pops up for each polygon
label_popup <- paste0(
"<strong>Zip code: </strong>",
zip_map$ZIPCODE,
"<br><strong>Number of dog bites: </strong>",
zip_map$n_bite
)
# format of the label for each polygon: pit bull bites
label_popup_pit <- paste0(
"<strong>Zip code: </strong>",
zip_map$ZIPCODE,
"<br><strong>Number of dog bites: </strong>",
zip_map$n_pit
)
# get jenks natural break for dog bites
getJenksBreaks(zip_map$n_bite, 6)
# get jenks natural breaks for pit bull bites
getJenksBreaks(zip_map$n_pit, 6)
# set bins
bite_bins <- c(0, 25, 62, 97, 141, 260)
pit_bins <- c(0 , 4, 14, 25, 41, 80)
# set pals
bite_pal <- colorBin('Greens', bins = bite_bins, na.color = '#aaff56')
pit_pal <- colorBin('Greens', bins = pit_bins, na.color = '#aaff56')
```
### Number of dog bites by zip code
```{r echo = FALSE}
# choropleth map for dog bites
leaflet::leaflet(data = zip_map_crs) %>%
addProviderTiles('CartoDB.Positron') %>%
addPolygons(fillColor = ~bite_pal(n_bite),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = label_popup,
highlightOptions = highlightOptions(color = "black", weight = 2,
bringToFront = TRUE)) %>%
addLegend('bottomleft',
pal = bite_pal,
values = ~n_bite,
title = 'Number of dog bite incidents by zip code',
opacity = 1)
```
### Number of pit bull bites by zip code
```{r echo = FALSE}
# choropleth map for pit bull bites
leaflet::leaflet(data = zip_map_crs) %>%
addProviderTiles('CartoDB.Positron') %>%
addPolygons(fillColor = ~pit_pal(n_pit),
fillOpacity = 0.8,
color = "#BDBDC3",
weight = 1,
popup = label_popup_pit,
highlightOptions = highlightOptions(color = "black", weight = 2,
bringToFront = TRUE)) %>%
addLegend('bottomleft',
pal = pit_pal,
values = ~n_pit,
title = 'Number of pit bull bite incidents by zip code',
opacity = 1)
#https://gist.github.com/brianhigh/2efe4d0bedf0eebe70cbcd58f2b894f7
```