3 Simple Steps to Map Geospatial Data in R
By: Amanda West
Tags: Visualization, Geospatial, Data Science, Programming, Rstudio

I always wondered how people created beautiful geospatial visualizations in R. I would stare googly-eyed at the perfect gradients which could make a dashboard or presentation pop, wondering just how the hell people were doing this in the programming language I associated with p-values and regression lines.

Surprisingly, you can actually accomplish this with but a few lines of code — I’m here today to show you how.

Step 1: Access the American Community Survey (“acs”) API

First, you’ll need access to the Census Bureau’s API. Go to this link to request an API key. After about 10 seconds you should get an email with your key and a link to confirm you are, indeed, human. Stash your API key somewhere — you’ll need to call it every time you reopen R.

In order for the API to be read correctly, we’ll need to install 3 libraries: “acs”, “choroplethr”, and “choroplethrMaps”. We’ll also install “RColorBrewer” and “ggplot2” for graphing later on.

A choropleth map (from Greek χῶρος “area/region” and πλῆθος “multitude”) is a type of thematic map in which areas are shaded or patterned in proportion to a statistical variable that represents an aggregate summary of a geographic characteristic within each area, such as population density or per-capita income.— Wikipedia

Here is the code you’ll need to input into R to get the API to work:

Step 2: Choose table ID

Once you have the API key installed, you can find available datasets at data.census.gov to play around with (I am using table B19301, per capita income by county, for the tutorial). When you find data you want to use, take note of the table ID. The table ID is all you will need to create a beautiful choropleth graph.

After you’ve settled on a dataset, let’s get graphing!

Step 3: Mapping Geospatial Data

Below I have the code to graph U.S. income per capita by county. This is only a glimpse of what can be toggled; if you have a modification I didn’t exemplify, the R documentation for county_choropleth_acs and ggplot2 Colors will get you the rest of the way there.

Some ACS datasets contain null values; the most colorful graphs will be those with most of their observations present!

….and the related graphs:

\\ 1: Default Values \\ 2: Custom Title \\ 3: Gradient + Custom Title \\ 4: Color Change + Custom Title \\

Bonus: States & Regions Only

Changing our graphs to focus on a state or region is as easy as adding one additional input, the state_zoom() function. In order to graph more than one state at once (but not the whole country), we’ll just concatenate all the states we want to view together.

For the last two graphs, I used some fun palettes provided by ggplot2. This library’s scale_fill_brewer() function contains a set of predetermined palettes which you can find here.

….and here are the four related graphs:

Conclusions

Now you know how to create simple geospatial visualizations in R. You can install the Census API and pick a dataset (from literally hundreds!) you’d like to investigate. You can graph by country, state, or a smattering of states. Finally, you can change the palette, gradient, and titles of your graphs.

This is only the tip of the geospatial iceberg, but I hope it can get you started. Thanks for reading and have a great work week! ❤️