---
title: "Homework 7 - Geoplotting US Wage Data"
output:
  pdf_document:
    latex_engine: xelatex
---

References:

- https://map-rfun.library.duke.edu/032_thematic_mapping_geom_sf.html
- https://www.computerworld.com/article/3175623/mapping-in-r-just-got-a-whole-lot-easier.html

In this homework, we plot the mean wages over the US states.

The purpose of this analysis is to visualize the average wages for the different US states.

## The Data

```{r echo=TRUE, message=FALSE, warning=FALSE}
library(tidyverse)       
library(readxl)
library(tigris)
library(sf)
library(viridis)
```

The data for this homework come from two sources.

1. The `tigris` dataset provides the state geographies.

TODO: This code chunk doesn't work on rstudio.calvin.edu for me, regardless of the versions of tidyverse, sf, tigris being used. Two work-arounds - use a local RStudio or drop the shift_geometry() call entirely.

```{r messages=FALSE, warning=FALSE, error=FALSE, results="hide"}
us_state_geographies <- tigris::states(cb=TRUE, resolution="20m") %>% 
   shift_geometry()
us_state_geographies
```

2. This spreadsheet provides the mean wages by state. You&rsquo;ll need to cleanse this data.

```{r messages=FALSE, warning=FALSE, error=FALSE, results="hide"}
state_mean_wages_raw <- 
  read_excel("data/BLS-wages-by-state.xlsx",
             col_types = c("text", "numeric"), 
             skip = 4)
state_mean_wages_raw
```

```{r}
state_mean_wages <- state_mean_wages_raw %>% 
  rename(wages = "Annual mean wage(2)") %>% 
  mutate(State = str_extract(`Area Name`, "\\w+.*(?=\\()")) %>% 
  drop_na(wages) %>% 
  select(State, wages)
state_mean_wages
```

# Data Wrangling

You&rsquo;ll combine these datasets to produce  geospatially-categorized wage data, and focus only on the rows for the 50 states.

```{r}
us_state_geographies_wages <- us_state_geographies %>% 
  left_join(state_mean_wages,
            by = c("NAME" = "State")) 
us_state_geographies_wages
```

```{r}
contiguous_states <- us_state_geographies_wages %>% 
  filter(GEOID < 60) 
contiguous_states
```

# Data Visualization

Produce a choropleth visualization that shows the average annual wages for all 50 states.

Do you notice any interesting patterns in this data?

```{r}
contiguous_states %>% 
  ggplot(aes(fill = wages, color = wages)) +
  geom_sf() +
  coord_sf(crs = 5070, datum = NA) +
  scale_fill_viridis(label = scales::dollar) + 
  scale_color_viridis(label = scales::dollar) +
  labs(title = "Annual Mean Wages by State",
       caption = "Data Source: BLS.gov - Occupational Employment Statistics ; 2016")
```

High median salaries generally appear in the southwest and northeast. West Virginia is shockingly low, and other states in the mid-south are low as well.
