-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_2_leaflet_map.Rmd
More file actions
221 lines (170 loc) · 5.74 KB
/
task_2_leaflet_map.Rmd
File metadata and controls
221 lines (170 loc) · 5.74 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
---
title: "Task 2: Plot a New York State Map showing income status by counties"
author: "Miao Wang"
date: "9/22/2019"
output: html_document
---
```
Task 2: Plot a New York State Map showing income status by counties.
```
- __Data__: 2017 - 5 year estimate for Medium Household Income. Downloaded from American Community Survey (ACS) [here] (https://factfinder.census.gov/faces/nav/jsf/pages/index.xhtml)
```{r, include=FALSE}
require(sf)
require(tidyverse)
require(leaflet)
here::here() %>% setwd()
```
### Step 1. Download Data
```{r}
# load nys shape file
nys_shape <- st_read(
dsn = "data/cugir-007865/",
layer = "cty036"
)
# income data
dt <- read.csv("data/aff_download/ACS_17_5YR_S1902_with_ann.csv",
skip = 1) %>% janitor::clean_names()
# head(dt, 5)
# understand the colnames by
# meta <- read.csv("data/aff_download/ACS_17_5YR_S1902_metadata.csv")
# clean up data
dt_cln <- dt %>%
transmute(
COUNTY = substr(id2, 3, 5),
county_name = gsub("(County)|(,)|(New York)|( )", "", geography),
county_name = if_else(county_name == "", "New York", county_name),
N_pop = number_estimate_all_households,
medium_income = mean_income_dollars_estimate_all_households_with_earnings_with_wages_or_salary_income)
head(dt_cln)
```
### Step 2. Cleaning Shape File
```{r}
# This part od codes are hidden
# main goal is to de-duplicate rows
# so that nys_shape only has 1 row for 1 unique county
# very similar idea of task 1
nys_shape %>% count(NAME) %>% filter(n > 1)
# get the ny part only
ny_shape <- nys_shape %>% filter(NAME == "New York") %>%
distinct(NAME, COUNTY, geometry)
# dissolved new york
ny_shape_dissolved <- st_union(ny_shape) %>%
st_sf() %>%
cbind(NAME = "New York", COUNTY = "061")
# get the bronx part only
bronx_shape <- nys_shape %>% filter(NAME == "Bronx") %>%
distinct(NAME, COUNTY, geometry)
# dissolved new york
bronx_shape_dissolved <- st_union(bronx_shape) %>%
st_sf() %>%
cbind(NAME = "Bronx", COUNTY = "005")
# get the sufflok part only
li_shape <- nys_shape %>% filter(NAME == "Suffolk") %>%
distinct(NAME, COUNTY, geometry)
# dissolve suffolk (3 rows originally)
li_shape_dissolved <- st_union(li_shape) %>%
st_sf() %>%
cbind(NAME = "Suffolk", COUNTY = "103")
# get the sufflok part only (2 rows originally)
wc_shape <- nys_shape %>% filter(NAME == "Westchester") %>%
distinct(NAME, COUNTY, geometry)
# dissolve suffolk
wc_shape_dissolved <- st_union(wc_shape) %>%
st_sf() %>%
cbind(NAME = "Westchester", COUNTY = "119")
# get the rest of state part
ros_shape <- nys_shape %>%
filter(!NAME %in% c("Suffolk", "Westchester", "New York", "Bronx")) %>%
distinct(NAME, COUNTY, geometry)
# union with ros
nys_shape <- rbind(st_sf(ros_shape),
st_sf(ny_shape_dissolved),
st_sf(bronx_shape_dissolved),
st_sf(li_shape_dissolved),
st_sf(wc_shape_dissolved)
)
# check nrow
nrow(nys_shape) # 62
```
### Step 3. Join with Shape File
- Note that to join a data frame with a sf object, it seems that the joining column has to have the same name
- Do not forget to turn the data back to sf object
```{r}
class(nys_shape)
class(dt_cln)
# merge the data with shape file
dt_poly <- left_join(dt_cln, nys_shape, by = "COUNTY") %>%
st_sf() # turn back to st_sf
head(dt_poly)
```
### Step 4. Plot in Map using leaftlet
```{r}
# color
hist(dt_poly$medium_income)
quantile(dt_poly$medium_income, seq(0.1, 1, 0.1), na.rm = TRUE) %>% round()
pal <- colorBin("Blues",
domain = dt_poly$medium_income,
bins = quantile(dt_poly$medium_income, seq(0.1, 1, 0.1), na.rm = TRUE))
# title
title <- htmltools::HTML("<big><strong>Medium Income By County</strong></big>")
# caption
caption <- htmltools::HTML("<small><strong>Notes:</strong/><br/><small>(1). Shape File Downloaded from https://cugir.library.cornell.edu/catalog/cugir-007865.<br/>(2). Income Data Donwloaded from ACS for 2017 5 year estimate.</small></small>")
# label
labels <- sprintf(
"<strong>%s</strong><br/>$medium income: %g<br/>%g population (used in the estimate)",
dt_poly$NAME,
dt_poly$medium_income,
dt_poly$N_pop
) %>% lapply(htmltools::HTML)
# plot on leaflet
m1 <- leaflet() %>%
addProviderTiles(providers$Hydda.RoadsAndLabels) %>%
addPolygons(
data = dt_poly,
fillColor = ~pal(medium_income),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
# highlight interation
highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
# labels
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")
) %>%
# add legend
addLegend(data = dt_poly,
title = "Medium Household Income",
pal = pal,
values = ~(medium_income),
opacity = 0.7,
labFormat = labelFormat(prefix = "$"),
position = "bottomright") %>%
# # add back the opoid treatment
# addMarkers(data = opioid_add,
# ~longitude,
# ~latitude,
# popup = ~sprintf(
# "<strong>Provider: </strong> %s<br/><strong>Program: </strong> %s",
# opioid_add$PROVIDER_NAME,
# opioid_add$PROGRAM_NAME
# )) %>%
# add Title
addControl(html = title, position = "topright") %>%
# add caption
addControl(html = caption, position = "bottomleft") %>%
# remove leaflet ink
htmlwidgets::onRender("var map = L.map('map', { attributionControl:false });")
m1
# save:
# mapview::mapshot(m1, url = "map_v1.html")
```