<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/using-blscraper-to-map-county-unemployment-data/"></div></div><div><a data-url="https://www.r-bloggers.com/using-blscraper-to-map-county-unemployment-data/" data-counturl="https://www.r-bloggers.com/using-blscraper-to-map-county-unemployment-data/" data-count="horizontal" data-text="Using blscrapeR to Map County Unemployment Data" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://www.datascienceriot.com/using-blscraper-to-map-county-unemployment-data/kris/"> R Tricks – Data Science Riot!</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>The <a target="_blank" href="https://github.com/keberwein/blscrapeR"><code>blscrapeR</code></a> package makes it easy to produce choropleth maps of various employment and unemployment rates from the Bureau of Labor Statistics (BLS.)</p>
<h4>Install blscrapeR from CRAN:</h4>
<pre>install.packages(&quot;blscrapeR&quot;)
</pre>
<p>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.</p>
<pre>
library(blscrapeR)
df &lt;- bls_api(&quot;LAUCN120950000000003&quot;,
              startyear = 2016, endyear = 2016)
</pre>
<p>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.</p>
<p>To resolve the issue, the <code>blscrapeR</code> 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.</p>
<p><strong>NOTE:</strong> 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.</p>
<pre>
library(blscrapeR)
df &lt;- get_bls_county()
</pre>
<p><strong>Limitations:</strong> The <code>get_bls_county()</code> function is only able to pull labor data for the past 12 months at time of query.</p>
<h4>Choropleth Mapping</h4>
<p>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 <code>bls_map_county()</code> function.</p>
<pre>
library(blscrapeR)
bls_map_county(map_data = df, fill_rate = &quot;unemployed_rate&quot;, 
               labtitle = &quot;Unemployment Rate by County&quot;)

</pre>
<p><a target="_blank" href="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/blscrape_docfig3.png?ssl=1"><img sizes="(max-width: 450px) 100vw, 450px" srcset="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/blscrape_docfig3.png?resize=450%2C398&amp;ssl=1 450w, https://www.datascienceriot.com/wp-content/uploads/2016/07/blscrape_docfig3-300x265.png 300w" alt="blscrape_docfig3" class="wp-image-905" src="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/blscrape_docfig3.png?resize=450%2C398&ssl=1" data-recalc-dims="1"></a></p>
<p>Maybe you just want one state? That's alright too.</p>
<pre>
library(blscrapeR)
# Map the unemployment rate for the Southeastern United States.
df &lt;- get_bls_county(stateName = &quot;Florida&quot;)

bls_map_county(map_data=df, fill_rate = &quot;unemployed_rate&quot;, 
               stateName = &quot;Florida&quot;)
</pre>
<p><a target="_blank" href="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/bus_fl_map.png?ssl=1"><img sizes="(max-width: 500px) 100vw, 500px" srcset="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/bus_fl_map.png?resize=456%2C310&amp;ssl=1 500w, https://www.datascienceriot.com/wp-content/uploads/2016/07/bus_fl_map-300x204.png 300w" alt="bus_fl_map" class="wp-image-924" src="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/bus_fl_map.png?resize=456%2C310&ssl=1" data-recalc-dims="1"></a></p>
<p>Why not use packages such as <code>maps</code> or <code>choroplethr</code>? 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 <code>get_bls_county()</code> will only return data from the previous 12 months, the FIPS codes may not match exactly.</p>
<h4>Roll Your Own: Custom Mapping</h4>
<p>The <code>bls_map_county()</code> 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 <code>blscrapeR</code> 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.</p>
<p>First, call the internal map data set and have a look:</p>
<pre>
library(blscrapeR)
us_map &lt;- county_map_data
head(us_map)
</pre>
<p>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.</p>
<p>Next, produce your custom map.</p>
<pre>
library(blscrapeR)
library(ggplot2)
# Get the most recent unemployment rate for each county on a national level.
df &lt;- get_bls_county()
# Get map data
us_map &lt;- county_map_data

# Insert larger breaks in unemployment rates
df$rate_d &lt;- cut(df$unemployed_rate, breaks = c(seq(0, 10, by = 2), 35))
# Plot
ggplot() +
    geom_map(data=us_map, map=us_map,
             aes(x=long, y=lat, map_id=id, group=group),
             fill=&quot;#ffffff&quot;, color=&quot;#0e0e0e&quot;, size=0.15) +
    geom_map(data=df, map=us_map, aes_string(map_id=&quot;fips&quot;, fill=df$rate_d),
             color=&quot;#0e0e0e&quot;, size=0.15) +
    scale_fill_brewer()+
    coord_equal() +
    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())
</pre>
<p><a target="_blank" href="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/bls_custom_map.png?ssl=1"><img sizes="(max-width: 500px) 100vw, 500px" srcset="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/bls_custom_map.png?resize=456%2C310&amp;ssl=1 500w, https://www.datascienceriot.com/wp-content/uploads/2016/07/bls_custom_map-300x204.png 300w" alt="bls_custom_map" class="wp-image-928" src="https://i2.wp.com/www.datascienceriot.com/wp-content/uploads/2016/07/bls_custom_map.png?resize=456%2C310&ssl=1" data-recalc-dims="1"></a></p>
<p>If you want more mapping options, there is more information in the <code>blscrapeR</code> <a target="_blank" href="https://github.com/keberwein/blscrapeR/tree/master/vignettes">package vignettes</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/using-blscraper-to-map-county-unemployment-data/"></div></div><div><a data-url="https://www.r-bloggers.com/using-blscraper-to-map-county-unemployment-data/" data-counturl="https://www.r-bloggers.com/using-blscraper-to-map-county-unemployment-data/" data-count="vertical" data-text="Using blscrapeR to Map County Unemployment Data" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://www.datascienceriot.com/using-blscraper-to-map-county-unemployment-data/kris/"> R Tricks – Data Science Riot!</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/XszDAculQWg"></a></div>