-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path031_thematic_mapping.qmd
176 lines (123 loc) · 4.95 KB
/
031_thematic_mapping.qmd
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
---
title: "Thematic Mapping with tmap"
---
```{r}
#| warning: false
#| message: false
library(tidyverse) # Tidyverse for Tidy Data
library(readxl)
library(tmap) # Thematic Mapping
library(tmaptools)
library(tigris) # Get Census Geography Poloygons
library(sf)
```
## 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.
```
us_geo <- tigris::states(class = "sf")
```
```{r}
#| message: false
#| warning: false
#| echo: true
#| results: false
us_geo <- tigris::states(class = "sf")
```
## Data Structure
The "Simple Features" (`sf`) data structure can easily be viewed and manipulated as a rectangular data frame, before visualizing. As an historical note -- an `sf` predecessor -- the `sp` data structure uses `@data` slots to hold data. We'll focus on the `sf` package. Below are two methods of viewing the structure of the downloaded shapefiles.
```{r}
class(us_geo)
glimpse(us_geo)
```
## sf as data frame
And as noted, here's the data frame view. Notice the geometry (polygon shape) in the far-right column of the data frame.
```{r}
as_tibble(us_geo)
```
### Quick Plotting
If you want to see a very quick view of your mapping data, you can plot the geometry data with the `plot` function. In this case we use the `sf::st_geometry()` function to plot only the geometry. You can quickly generate a faceted map by excluding the `st_geometry` function: e.g. `plot(us_geo)` but that will consume computation cycles (i.e. wait time). Therefore, I recommend trying the smaller layer for now.
> **Note:** Census geography for the USA will span the globe in part becuase Region 9 includes a multitude of pacific islands. Later we will limit to simply the "lower 48" states.
```{r}
plot(st_geometry(us_geo))
```
## Get BLS data
I've already downloaded and stored some data from the Bureau of Labor Statistics. Those data are stored in an excel file in the `data` directory of the [repository](https://github.com/libjohn/mapping-with-R): `data/OES_Report.xlsx`. **The goal is to attach this data to the previously downloaded shapefiles.**
But you may be interested 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
- 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}
#| message: false
#| warning: false
#| echo: true
Salary4Helpers <-
read_excel("data/OES_Report.xlsx",
col_types = c("text", "numeric"),
skip = 4)
Salary4Helpers
```
## Wrangle the data
Before we join the BLS data to the shapefile we need to transform the structure of the downloaded BLS data
```{r}
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
```
## Join data
Use the `dplyr::left_join()` function or the base-R `merge()` function to append BLS data to the previously loaded shape object
```{r}
HelperShapeObject <- left_join(us_geo, BlsWage_ToJoin, by = c("NAME" = "State"))
as_tibble(HelperShapeObject)
```
## Quick Thematic Map
```{r}
qtm(HelperShapeObject, fill = "wages")
```
## 50 states
Filter to only the contiguous 50 states + D.C. Note that `tigris::shift_geometry()` will shift and resize Alaska and Hawaii.
```{r}
contiguous_states <- HelperShapeObject %>%
filter(REGION != 9) %>%
shift_geometry()
```
## Make Choropleth
```{r}
tm_shape(contiguous_states) +
tm_polygons("wages", id = "Name")
```
## Projection
Mark likes the USA_Contiguous_Albers_Equal_Area_Conic_USGS_version projection for the continental US. `EPSG:5070`
```{r}
contiguous_states %>%
st_transform(5070) %>%
tm_shape() +
tm_polygons("wages", id = "Name")
```
### Alternative Syntax
```{r}
tm_shape(contiguous_states, projection = 5070) +
tm_polygons("wages", id = "Name")
```
### Explore `tmap` syntax and functions
```{r}
tm_shape(contiguous_states, projection = 5070) +
tm_borders(col = "black", alpha = 0.4) +
tm_fill(col = "REGION", alpha = 0.6) +
tm_layout(title = "Regions of the USA",
attr.color = "navy",
title.position = c("center", "top"),
title.bg.color = "lightblue")
```
## End Notes
This session inspired by https://www.computerworld.com/article/3175623/data-analytics/mapping-in-r-just-got-a-whole-lot-easier.html