Choropleth tutorial and regression coefficient plots

About two weeks ago, I gave short talk at Duke, wherein I presented a brief tutorial on creating choropleth maps in R using ggplot2. Since the code is already written, and the data and shapefiles already hosted online, I thought I would share the tutorial more widely.

A .ZIP file containing all the files necessary to follow the tutorial is available at:

The script goes very briefly through the loading of shapefiles and presidential election returns, and ends with the production of the choropleth below.

Click to enlarge

I don’t get into further customization of the map, as there are other more authoritative and complete sources for that. Further, much more detailed instruction on reading shape files are available from CSISS and NCEAS.

Included at the very end of the script is a brief example of a regression coefficient plot, something like a ggplot2 version of the coefplot() in Andrew Gelman‘s arm package.

I decided to develop the example into a function that takes as input a list() of model objects, and returns a ggplot2 object, which can be further modified by the user if so desired.

A coefficient plot comparing three models.

The script for the above plot can be found here. I also wrote a function that eschews arbitrarily discrete confidence bounds, instead attempting to suggest a sense of our confidence in the estimate without choosing a specific interval, since the difference between significance and insignificance is not itself significant. Code for the function is available here, and an example can be seen below.

Smoothed standard error coefficient plots

7 Responses to “Choropleth tutorial and regression coefficient plots”
  1. Johannes Beller says:

    Thank you very much. I have been searching for exactly this – a ggplot style regression coef plot.

  2. ek says:

    Nicely done, love the coef plots.

  3. ek fan says:

    What ek said.

  4. anthony damico says:

    any luck adding in alaska and hawaii?

  5. bshor says:

    David, great tutorial. What about doing a faceted grid of maps, a la Gelman?

    • d sparks says:

      On nice thing about Dr. Wickham’s ggplot2 package is that it makes faceting very straightforward (see facet_grid). If some time becomes available, I might take the opportunity to put together a tutorial on it — I have some data for which the small multiples approach would be useful.

Check out what others are saying...
  1. […] posted on the excellent blog aggregator R-Bloggers.  It was on R-Bloggers that I happened upon this excellent post that outlines a quick example involving mapping the results of the 2008 election by […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: