-
Notifications
You must be signed in to change notification settings - Fork 13
/
032_thematic_mapping_geom_sf.qmd
108 lines (80 loc) · 3.23 KB
/
032_thematic_mapping_geom_sf.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
---
title: "Thematic Mapping with geom_sf"
---
```{r}
#| message: false
#| warning: false
library(tidyverse)
library(readxl)
library(tigris)
library(sf)
library(viridis)
```
## Shapefiles as sf
Use the `tigris` package to get Census Tiger shapefiles for census geographies. Coastal boundaries can be gathered with the tigris argument: `cb = TRUE`.
```{r}
#| message: false
#| warning: false
#| echo: true
#| results: false
us_geo <- tigris::states(class = "sf", cb = TRUE) %>%
shift_geometry()
```
``` r
us_geo <- tigris::states(class = "sf", cb = TRUE)) %>%
shift_geometry()
```
## Get BLS data
In additon to shapefiles, the wage data used in this example comes from from the Bureau of Labor Statistics. These 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`.
```{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
Using the [`stringr` package](https://stringr.tidyverse.org) we can extract the text of the state names from the state code by leveraging regular expressions (i.e. regex) pattern matching techniques with stringr::str_extract().
```{r}
BlsWage_ToJoin <- Salary4Helpers %>%
rename(wages = "Annual mean wage(2)") %>%
mutate(State = str_extract(`Area Name`, "\\w+.*(?=\\()")) %>%
drop_na(wages) %>%
select(State, wages)
```
## Join data
Use the `dplyr::left_join` function to append BLS variable to the `sf` tibble (data frame).
```{r}
HelperShapeObject <- us_geo %>%
left_join(BlsWage_ToJoin,
by = c("NAME" = "State"))
```
## 50 states
Filter to only the 50 US states + D.C.
Alaska and Hawaii were shifted and rescaled using `tigris::shift_geometry()`, above.
```{r}
contiguous_states <- HelperShapeObject %>%
filter(GEOID < 60)
```
## ggplot2 with geom_sf and viridis
ggplot2 is one of the more popular and broadly distributed graphics packages used in the R community. ([Learn more](https://rfun.library.duke.edu/#portfolio) about ggplot2.
In this plot I **reversed** the *direction* of the color scale. Reversing the color pallette's direction is not a visualization best practice, but I use this approach to demonstrate the `direction` argument.
Use a pleasing projection. In this case assign the CRS projection to '5070'
> `coord_sf(crs = 5070)` Help with [projection/CRS](https://guides.library.duke.edu/r-geospatial/CRS)\
> `coords_sf(datum = NA)` Remove gridlines, i.e [graticules](https://en.wikipedia.org/wiki/Graticule)
```{r}
contiguous_states %>%
ggplot(aes(fill = wages, color = wages)) +
geom_sf() +
coord_sf(crs = 5070, datum = NA) +
scale_fill_viridis(direction = -1, label = scales::dollar) +
scale_color_viridis(direction = -1, label = scales::dollar) +
labs(title = "Annual Mean Wages by State",
subtitle = "Mental Health and Substance Abuse Social Workers(SOC Code211023)",
caption = "Data Source: BLS.gov - Occupational Employment Statistics ; 2016")
```
## End Notes
This session inspired by https://www.computerworld.com/article/3175623/data-analytics/mapping-in-r-just-got-a-whole-lot-easier.html