Mapping County Unemployment with blscrapeR
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The blscrapeR package makes it easy to produce choropleth maps of various employment and unemployment rates from the Bureau of Labor Statistics (BLS.) 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.
Limitations: The get_bls_county()
function is only able to pull labor data for the past 12 months at time of query.
Choropleth Mapping
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 bls_map_county()
function.
Maybe you just want one state? That’s alright too.
Custom Mapping
The 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.
If you want more mapping options, there is more information in the blscrapeR
package vignettes.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.