-
Notifications
You must be signed in to change notification settings - Fork 2
/
032_thematic_mapping_geom_sf.Rmd
111 lines (76 loc) · 3.47 KB
/
032_thematic_mapping_geom_sf.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
---
title: "Thematic Mapping with geom_sf"
output: html_document
---
from: https://map-rfun.library.duke.edu/032_thematic_mapping_geom_sf.html
```{r load-libraries, message=FALSE, warning=FALSE}
library(tidyverse) # Tidyverse for Tidy Data
library(readxl)
library(tigris)
library(sf)
library(viridis)
library(tigris)
```
## Shapefiles as sf
Repeating steps from the [previous section](031_thematic_mapping.html), using the `tigris` package, get Census Tiger shapefiles for census geographies. Coastal boundaries can be gathered with the tigris argument: `cb = TRUE`.
```{r getuscb, message=FALSE, warning=FALSE, include=FALSE}
us_geo <- tigris::states(class = "sf", cb = TRUE)
```
``` r
us_geo <- tigris::states(class = "sf", cb = TRUE)
```
## Get BLS data
As mentioned before, the data are 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`.
Again, from the previous section you will see how these data were gathered, loaded, transformed, and joined.
```{r getblsdata, message=FALSE, warning=FALSE}
Salary4Helpers <-
read_excel("data/OES_Report.xlsx",
col_types = c("text", "numeric"),
skip = 4)
Salary4Helpers
```
## Wrangle the data
As before...
```{r wrangleblsdata, message=FALSE, warning=FALSE}
BlsWage_ToJoin <- Salary4Helpers %>%
rename(wages = "Annual mean wage(2)") %>%
mutate(State = str_extract(`Area Name`, "\\w+.*(?=\\()")) %>%
drop_na(wages) %>%
select(State, wages)
```
```{r}
as_tibble(us_geo) %>%
arrange(NAME)
```
## Join data
Use the `dplyr::left_join` function to append BLS variable to the `sf` tibble (data frame).
```{r leftjoin_usgeo, warning=FALSE}
HelperShapeObject <- us_geo %>%
left_join(BlsWage_ToJoin,
by = c("NAME" = "State"))
```
## Contiguous 48 states
Filter to only the contiguous 48 states + D.C.
```{r filterlower48, message=FALSE, warning=FALSE}
contiguous_states <- HelperShapeObject %>%
filter(GEOID < 60) %>%
filter(STUSPS != "AK") %>%
filter(STUSPS != "HI")
```
## ggplot2 with geom_sf and viridis
In this section we introduce making shapefiles with ggplot2. 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. After consulting with my visualization colleagues it seems this may have been a non-standard action on my part. But, I leave the `direction` argument here for the curious.
Use a pleasing projection, in this case assigned the crs projection to '5070' (`coord_sf(crs = 5070)`), and removed the gridlines (i.e [graticules](https://en.wikipedia.org/wiki/Graticule)) `coords_sf(datum = NA)`
```{r with-ggplot-geom_sf, message=FALSE, warning=FALSE}
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