forked from libjohn/mapping-with-R
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path038_facets_wrap_thematic_mapping.Rmd
More file actions
344 lines (247 loc) · 9.13 KB
/
038_facets_wrap_thematic_mapping.Rmd
File metadata and controls
344 lines (247 loc) · 9.13 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
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
---
title: "Faceted Thematic Mapping"
output: html_document
---
```{r load-libraries, message=FALSE, warning=FALSE}
library(tidyverse) # Tidyverse for Tidy Data
library(readxl)
library(tmap) # Thematic Mapping
library(tmaptools)
library(tigris) # Get Census Geography Poloygons
library(sf)
library(tidycensus)
library(viridis) # Loading required package: viridisLite
library(ggplot2) #-- version 2.2.1.90000
#devtools::install_github("tidyverse/ggplot2")
```
## Shapefiles as sf
Using the `tigris` package get Census Tiger shapefiles for census geographies. Tigris will return the shapefile in the `sf`, or simple features, format.
```{r get-data, message=FALSE, warning=FALSE}
us_geo <- tigris::states(class = "sf")
# us_geo_spdf <- states() # spdf uses @data slots -- old school
```
## Get BLS data
I've already downloaded and stored some data from the Bureau of Labor Statistics. Thoses data are stored in an excel file in the `data` directory of the repository: `data/OES_Report.xlsx`. **The goal is to attach this data to the previously downloaded shapefiles.**
But you may be intersted in how I gathered the data. below are some summary notes documenting my steps of gathering the data from the Bureau of Labor Statistics.
https://data.bls.gov/oes/#/occGeo/One%20occupation%20for%20multiple%20geographical%20areas
- One occupation for multiple geographical areas
- Mental Health and Substance Abuse Social Workers (Also, Secondary School Teacher, Waiter, Legislator, and Paralegals)
- State
- All States in this list
- Annual Mean wage
- Excel
- Read the Data in with the RStudio "Import Dataset" wizard available in the *Environment* tab. This will generate the code below and ensure the import
- Skips the first 4 lines
- Coerces the 2nd column to numeric
```{r get-blsdata, message=FALSE, warning=FALSE}
Salary4Helpers <-
read_excel("data/OES_Report.xlsx",
col_types = c("text", "numeric"),
skip = 4)
# Salary4Helpers
```
### Load more BLS Wages
read in the XLXS files for the other occupations: Secondary School Teacher, Waiter, Legislator, and Paralegals
```{r other_occupations_data, message=FALSE, warning=FALSE}
legislators <-
read_excel("data/OES_legislator.xlsx",
col_types = c("text", "numeric"),
skip = 4)
paralegals <-
read_excel("data/OES_paralegals.xlsx",
col_types = c("text", "numeric"),
skip = 4)
teachers <-
read_excel("data/OES_secondary_school_teacher.xlsx",
col_types = c("text", "numeric"),
skip = 4)
waiters <-
read_excel("data/OES_waiters.xlsx",
col_types = c("text", "numeric"),
skip = 4)
```
### Wrangle the data
Before we join the BLS data to the shapefile we need to transform the structure of the downloaded BLS data
```{r wrangle_bls_data, message=FALSE, warning=FALSE}
BlsWage_ToJoin <- Salary4Helpers %>%
rename(Area = "Area Name") %>%
rename(wages = "Annual mean wage(2)") %>%
mutate(State = gsub("\\(\\d{7}\\)", "", Area)) %>%
filter(wages != "NA_character_") %>%
select(State, wages)
#BlsWage_ToJoin
```
```{r wrangle_other_ocupations}
legislators <- legislators %>%
rename(Area = "Area Name") %>%
rename(wages = "Annual mean wage(2)") %>%
mutate(State = gsub("\\(\\d{7}\\)", "", Area)) %>%
filter(wages != "NA_character_") %>%
select(State, wages)
paralegals <- paralegals %>%
rename(Area = "Area Name") %>%
rename(wages = "Annual mean wage(2)") %>%
mutate(State = gsub("\\(\\d{7}\\)", "", Area)) %>%
filter(wages != "NA_character_") %>%
select(State, wages)
teachers <- teachers %>%
rename(Area = "Area Name") %>%
rename(wages = "Annual mean wage(2)") %>%
mutate(State = gsub("\\(\\d{7}\\)", "", Area)) %>%
filter(wages != "NA_character_") %>%
select(State, wages)
waiters <- waiters %>%
rename(Area = "Area Name") %>%
rename(wages = "Annual mean wage(2)") %>%
mutate(State = gsub("\\(\\d{7}\\)", "", Area)) %>%
filter(wages != "NA_character_") %>%
select(State, wages)
```
### Append data
Using the `append_data()` function of the `tmaptools` package, append BLS data to the previously loaded shape object
```{r make_nice_df}
paralegals <- append_data(us_geo, paralegals,
key.shp = "NAME",
key.data = "State")
waiters <- append_data(us_geo, waiters,
key.shp = "NAME",
key.data = "State")
teachers <- append_data(us_geo, teachers,
key.shp = "NAME",
key.data = "State")
legislators <- append_data(us_geo, legislators,
key.shp = "NAME",
key.data = "State")
```
```{r append_bls_data}
HelperShapeObject <- append_data(us_geo, BlsWage_ToJoin,
key.shp = "NAME",
key.data = "State")
```
### Contiguous 48 states
Filter to only the contiguous 48 states + D.C.
```{r subset-data}
contiguous_states <- HelperShapeObject %>%
filter(REGION != 9) %>%
filter(STUSPS != "AK") %>%
filter(STUSPS != "HI")
paralegals <- paralegals %>%
filter(REGION != 9) %>%
filter(STUSPS != "AK") %>%
filter(STUSPS != "HI")
waiters <- waiters %>%
filter(REGION != 9) %>%
filter(STUSPS != "AK") %>%
filter(STUSPS != "HI")
teachers <- teachers %>%
filter(REGION != 9) %>%
filter(STUSPS != "AK") %>%
filter(STUSPS != "HI")
legislators <- legislators %>%
filter(REGION != 9) %>%
filter(STUSPS != "AK") %>%
filter(STUSPS != "HI")
```
## Get Population Data -- Tidycensus
### identify and pick census variables
B01003_001E = Total Population
B06011_001E = Median income in the past 12 months --!!Total:
```{r identify_ACS_variables}
variables_census <- load_variables(2015, "acs5", cache = TRUE)
```
### Get Median Income
```{r get_median_income}
us_pop <-
get_acs(geography = "state",
variables = "B06011_001E",
# state = "NC",
geometry = FALSE)
us_pop <- us_pop %>%
filter(NAME != "Alaska") %>%
filter(NAME != "Hawaii") %>%
filter(NAME != "Puerto Rico")
```
### Append Census Data
```{r make_nice_df2}
appended_population_and_states_shapefile <- append_data(contiguous_states, us_pop,
key.shp = "GEOID",
key.data = "GEOID")
```
## Make Tall
Wrangle the data into a tall format and single data frame
```{r make_tall}
contiguous_states_slim <- contiguous_states %>%
select(GEOID, NAME, wages, geometry) %>%
mutate(category = "Substance Abuse Soc.Wrk") %>%
rename(variable = wages)
```
```{r wrangle_pop}
population_states_slim <- appended_population_and_states_shapefile %>%
select(GEOID, NAME, estimate, geometry) %>%
mutate(category = "Median Income") %>%
rename(variable = estimate)
```
```{r wrangle_other_bls}
paralegals <- paralegals %>%
select(GEOID, NAME, wages, geometry) %>%
mutate(category = "Paralegals") %>%
rename(variable = wages)
waiters <- waiters %>%
select(GEOID, NAME, wages, geometry) %>%
mutate(category = "Waiters") %>%
rename(variable = wages)
teachers <- teachers %>%
select(GEOID, NAME, wages, geometry) %>%
mutate(category = "Teachers") %>%
rename(variable = wages)
legislators <- legislators %>%
select(GEOID, NAME, wages, geometry) %>%
mutate(category = "Legislators") %>%
rename(variable = wages)
```
### Bind Rows
To combine all the smaller data frames into one tall format, bind the rows.
base::`rbind` works. `dplyr::bind_rows` did not work.
```{r bind_tall}
tall <- rbind(contiguous_states_slim,
population_states_slim,
paralegals,
waiters,
teachers,
legislators)
```
### More Munging
Make the category variable a categorical factor with levels. This will improve the order of the facets when displayed.
```{r}
display_levels <- c("Median Income", "Legislators",
"Paralegals", "Substance Abuse Soc.Wrk",
"Teachers", "Waiters")
tall <- tall %>%
mutate(category = factor(category, display_levels)) %>%
rename(Wages = variable)
```
## Facet_grid
`facet_wrap` works better than `facet_grid` because there is more control over the display of the rows and columns. The first example, below, does not run. I keep it only to document something that could work.
```{}
tall %>%
ggplot(aes(fill = variable, color = variable)) +
geom_sf() +
coord_sf(crs = 5070) +
scale_fill_viridis(option = "viridis") +
scale_color_viridis(option = "viridis") +
facet_grid(. ~ category)
```
### Display Map
```{r facet_wrap_it_up}
tall %>%
ggplot(aes(fill = Wages, color = Wages)) +
geom_sf() +
coord_sf(crs = 5070, datum = NA) +
scale_fill_viridis(option = "viridis") +
scale_color_viridis(option = "viridis") +
facet_wrap(~ category, nrow = 3, ncol = 2)
```
### Save Map
```{r save_it}
ggsave("facet_map.png", width = 8, height = 8, units = "in")
```