blscrapeR package makes it easy to produce choropleth maps of various employment and unemployment rates from the Bureau of Labor Statistics (BLS.)
Install blscrapeR from CRAN:
It’s easy enough to pull a metric for a certain county. The code below pulls the unemployment rates for Orange County, FL from the BLS API.
The only problem is, there are over 3,000 counties in the United States and an API query of that size would push any user well over the daily query limits of the BLS API.
To resolve the issue, the
blscrapeR package includes a function that allows us to pull county statistics in the form of a text file from the BLS servers, which don’t count against a user’s daily query limit.
NOTE: You can use arguments to get data for a specific month, but if there is no date argument, the function will pull the most recent month in the data set.
get_bls_county() function is only able to pull labor data for the past 12 months at time of query.
Now that we’ve got the data, it’s time for the mapping. There are a few options here, but the simplest option would be to use the package’s
library(blscrapeR) bls_map_county(map_data = df, fill_rate = "unemployed_rate", labtitle = "Unemployment Rate by County")
Maybe you just want one state? That’s alright too.
library(blscrapeR) # Map the unemployment rate for the Southeastern United States. df
Why not use packages such as
choroplethr? While this is possible, both of those packages contain older FIPS codes, which won’t correctly map to 2016 data (as of the date I’m writing this.) While we could manually update the codes, but it involves several extra steps. Since the
get_bls_county() will only return data from the previous 12 months, the FIPS codes may not match exactly.
Roll Your Own: Custom Mapping
bls_map_county() function produces a map that may not be your cup of tea. The function is only provided as a “quick function” to see if your data fit. The
blscrapeR package provides the fortified map data, which includes longitud, latitude and FIPS codes. This data set is suitable for any kind of ggplot2 map you can think of.
First, call the internal map data set and have a look:
Notice the id column looks a lot like one of the FIPS codes returned by the get_bls_county() function? This is actually a concatenation of the state + county FIPS codes. The first two numbers are the state FIPS and the last four are the county FIPS. These boundaries currently represent 20015/2016 and will be updated accordingly so they always represent the current year.
Next, produce your custom map.
library(blscrapeR) library(ggplot2) # Get the most recent unemployment rate for each county on a national level. df
If you want more mapping options, there is more information in the
blscrapeR package vignettes.