Mapping US Counties in R with FIPS

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.

Name Matching
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 GitHub gist, but I don’t recommend it. I’m using an air quality data set from the Centers for Disease Control and Prevention.

Problems with string matching:
County names change
Louisiana often has “parishes” or “bayous”
Alaska often has “territories” or “census areas”


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 Oglala Lakota County is missing from your base map!

FIPS Matching
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. NOTE: There are also shape files for zip codes, congressional districts, census tracts, etc. The shape file we’re using can be downloaded here. Just unzip and place it in your working dir.

Note that these data don’t contain values for Alaska and Hawaii.



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.

Other Advantages
Leaflet anyone? Another plus to shape files is, they are easily rendered to a leaflet map. I changed the data prep a little bit here, but the key is to merge the air quality data to the spatial polygon data frame and leaflet does the rest.

12 thoughts on “Mapping US Counties in R with FIPS

    1. You’re right. These particular data are spread over ten years. There are likely some boundaries that aren’t 100% correct. They have different years in the shape files too, this was 2013, but you’re right, if the observations in your data are spread out, it can be challenging. I still maintain tracking changes in FIPS is easier than regex incantations.

      And thanks for the comment, made me realize I forgot to publish my original data prep;)

  1. Popup data are mapped incorrectly on the leaflet example. Missouri seems to have Iowa counties instead.

  2. Thanks for this tutorial very helpful.

    I’m using the leaflet example as the base for a shiny app where I would like to select a variable and then explore on the map. My question is would it be more resource efficient to merge all 7 numerical variables with the and then filter that based on the user input relative to…

    filtering first the county data and then merging each time as the input is provided with the object? Appreciate any insights.

    1. Mihir, your first solution of merging all variables into the object would work best. Of coarse, your map object will be larger, but probably not by all that much, and a filter on a single data frame would be more efficient than merging two data frames each time a user makes a new selection. Especially assuming that a single user will probably make multiple selections in one session.

  3. Thanks for confirming. Though I realized that once merging everything I didn’t need to filter. I ended up using the shiny input in the fillColor option.

Leave a Reply