Skip to content

Instantly share code, notes, and snippets.

@keberwein
Last active October 28, 2016 15:43
Show Gist options
  • Save keberwein/3e2469fbe14997742f27f0af68f814cc to your computer and use it in GitHub Desktop.
Save keberwein/3e2469fbe14997742f27f0af68f814cc to your computer and use it in GitHub Desktop.
Map County Unemployment with ggplot2
library(blscrapeR)
library(ggplot2)
library(dplyr)
# Get the current county data from blscrapeR.
df <- get_bls_county(stateName = "Ohio")
# Grap the internal FIPS codes data set from blscrapeR so we can match on counties.
fips <- blscrapeR::county_fips
# Subset fips list to only Ohio counties.
fips <- subset(fips, state=="OH")
# Do some gusb majic to format the county names so they match those in the map data.
fips$county <- tolower(gsub('County|Municipio|[Cc]ity|Parish|','', fips$county))
# Trim trailing white space from the county names.
fips$county <- sub("\\s+$", "", fips$county)
# Grap map data for Ohio from ggplot package.
map <- map_data("county", "Ohio") %>% rename(county = subregion)
# Join fips codes to map data
map <- left_join(fips, map, by="county")
# Join fips unemployment data to map data
ggmap <- left_join(df, map, by="fips_county")
# The rest is just ggplot. Note: in the cord map argument, we're using an Albers projection.
ggplot(ggmap, aes(long, lat), fill="#ffffff", color="#0e0e0e", size=0.15) +
geom_polygon(aes(group = group, fill = unemployed_rate), color="#0e0e0e", size=0.15) +
scale_fill_gradientn(colors = c("green", "red")) +
coord_map("albers", lat0 = 30, lat1 = 40) +
labs(title="Ohio Unemployment Rate") +
theme(axis.line=element_blank(),
axis.text.x=element_blank(),
axis.text.y=element_blank(),
axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
legend.title=element_blank())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment