<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/mapping-us-counties-in-r-with-fips/"></div></div><div><a data-url="http://www.r-bloggers.com/mapping-us-counties-in-r-with-fips/" data-counturl="http://www.r-bloggers.com/mapping-us-counties-in-r-with-fips/" data-count="horizontal" data-text="Mapping US Counties in R with FIPS" 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/mapping-us-counties-in-r-with-fips/kris/"> R Tricks – Data Science Riot!</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Anyone who’s spent any time around data knows primary keys are your friend. Enter the FIPS code. FIPS is the Federal Information Processing Standard and appears in most data sets published by the US government.</p>
<p><strong>Name Matching</strong><br>
The map below is an example as the “wrong way” to do something like this. This map uses a string matching technique to match US county names with the county names in the maps package. The map below can be replicated with this <a target="_blank" href="https://gist.github.com/keberwein/a61651d1de90476d5fb28dc08bbbc617">GitHub gist</a>, but I don’t recommend it. I’m using an air <a target="_blank" href="http://the%20centers%20for%20disease%20control%20and%20prevention/">quality data set</a> from the<span> Centers for Disease Control and Prevention.</span></p>
<p><strong>Problems with string matching:</strong><br>
County names change<br>
Louisiana often has “parishes” or “bayous”<br>
Alaska often has “territories” or “census areas”</p>
<p><a target="_blank" href="https://www.datascienceriot.com/wp-content/uploads/2016/06/str_map.png"><img sizes="(max-width: 1584px) 100vw, 1584px" srcset="https://www.datascienceriot.com/wp-content/uploads/2016/06/str_map.png 1584w, https://www.datascienceriot.com/wp-content/uploads/2016/06/str_map-300x173.png 300w, https://www.datascienceriot.com/wp-content/uploads/2016/06/str_map-768x443.png 768w, https://www.datascienceriot.com/wp-content/uploads/2016/06/str_map-1024x590.png 1024w" height="913" alt="str_map" width="1584" class="wp-image-874" src="https://www.datascienceriot.com/wp-content/uploads/2016/06/str_map.png"></a></p>
<p>As you can see from above, many counties are missing. It’s possible to fix this with some fancy regex work, but if may take quite some time before you realize why <a target="_blank" href="http://www.argusleader.com/story/davidmontgomery/2014/11/05/shannon-county-name-change/18534269/">Oglala Lakota County</a> is missing from your base map!</p>
<p><strong>FIPS Matching</strong><br>
The maps package contains a built-in data set that you can call with `county.fps`. The only problem with this is, you still end up string matching with your data set. I’ve found the best way to get a map with baked-in FIPS codes is to download (one of many) shape files provided by the Census Bureau. <strong>NOTE:</strong> There are also shape files for zip codes, congressional districts, census tracts, etc. The shape file we’re using can be <a target="_blank" href="https://www.census.gov/geo/maps-data/data/cbf/cbf_counties.html">downloaded here</a>. Just unzip and place it in your working dir.</p>
<p>Note that these data don’t contain values for Alaska and Hawaii.</p>

<div data-gist="keberwein/815cca98e7d99fc360ab27faa651e5cc.json"></div>
<p><a target="_blank" href="https://www.datascienceriot.com/wp-content/uploads/2016/06/fips_map.png"><img sizes="(max-width: 1591px) 100vw, 1591px" srcset="https://www.datascienceriot.com/wp-content/uploads/2016/06/fips_map.png 1591w, https://www.datascienceriot.com/wp-content/uploads/2016/06/fips_map-300x160.png 300w, https://www.datascienceriot.com/wp-content/uploads/2016/06/fips_map-768x408.png 768w, https://www.datascienceriot.com/wp-content/uploads/2016/06/fips_map-1024x545.png 1024w" height="846" alt="fips_map" width="1591" class="wp-image-875" src="https://www.datascienceriot.com/wp-content/uploads/2016/06/fips_map.png"></a></p>
<p><strong>Results</strong></p>
<p>The data are taken from 2000 to 2010, and the shape file we’re using is from 2013. But since FIPS codes remain constant, even when county names change, every thing matches up just fine.</p>
<p><strong>Other Advantages</strong><br>
Leaflet anyone? Another plus to shape files is, they are easily rendered to a leaflet map. I threw the below map together “quick and dirty.” I’m not really pleased with the green-to-red color ramp, but I’m sure that could be fixed by manually assigning color buckets. GGplot seems to have a better handle on color ramping straight out of the box.</p>
<p><iframe height="300" width="100%" src="https://www.datascienceriot.com/wp-content/uploads/2016/06/leaflet_counties.html">This would be a cool map if your browser allowed Javascript. 
<p> </p>
<p></p>

<div data-gist="keberwein/05f633a2be293b01b52bf05553d24b93.json"></div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/mapping-us-counties-in-r-with-fips/"></div></div><div><a data-url="http://www.r-bloggers.com/mapping-us-counties-in-r-with-fips/" data-counturl="http://www.r-bloggers.com/mapping-us-counties-in-r-with-fips/" data-count="vertical" data-text="Mapping US Counties in R with FIPS" 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/mapping-us-counties-in-r-with-fips/kris/"> R Tricks – Data Science Riot!</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/a9AFx6Ri69g"></a></div></iframe>