library(tidyverse)       # Tidyverse for Tidy Data

Shapefiles as sf

Repeating steps from the previous section, using the tigris package, get Census Tiger shapefiles for census geographies. Coastal boundaries can be gathered with the tigris argument: cb = TRUE.

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: data/OES_Report.xlsx.

Again, from the previous section you will see how these data were gathered, loaded, transformed, and joined.

Salary4Helpers <- 
             col_types = c("text", "numeric"), 
             skip = 4)


Wrangle the data

As before…

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).

HelperShapeObject <- us_geo %>% 
            by = c("NAME" = "State"))

Contiguous 48 states

Filter to only the contiguous 48 states + D.C.

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 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) coords_sf(datum = NA)

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: - Occupational Employment Statistics ; 2016")

R We Having Fun Yet‽ -- Learning Series
Data & Visualization Services
Duke University Libraries
C bn
Shareable via Creative Commons: CC By-NC