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: http://goo.gl/UrvQo.
The script goes very briefly through the loading of shapefiles and presidential election returns, and ends with the production of the choropleth below.
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.
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.
Comments
7 Responses to “Choropleth tutorial and regression coefficient plots”Trackbacks
Check out what others are saying...-
[…] 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 […]
Thank you very much. I have been searching for exactly this – a ggplot style regression coef plot.
Nicely done, love the coef plots.
What ek said.
any luck adding in alaska and hawaii?
David, great tutorial. What about doing a faceted grid of maps, a la Gelman?
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.