I was curious about creating maps with ggplot2 – and was happy to find that great minds have long since tackled this topic. Folks who are interested in how to learn about R (and ggplot2) might be interested in the process. (There should be a “greatest hits” listing related to R topics like this – the best I can think of to do is revive them in a forum like this).
After looking through the ggplot2 book (which had some simple examples) and the online documentation, I did a few searches and came across the topic on stackoverflow. At the end of this post there is a reference to previous discussions in the R community. It seems that an article on Flowing Data (using Python) inspired a bit of discussion of implementations of choropleth maps with R. And so a challenge was issued to do this in R. Several solutions were created and reviewed – and I decided to replicate the first response. Fortunately Hadley Wickham included his solution in his github repository. A slightly modified version of his original chart is shown below.
My revised version included a title, legend name and call to coord_map. I followed the same process as the original challenge – downloaded the data from the Bureau of Labor Statistics and saved the relevant sheet as a csv.
m=ggplot(choropleth, aes(long, lat, group = group)) +
geom_polygon(aes(fill = rate_d), colour = alpha(“white”, 1/2), size = 0.2) +
geom_polygon(data = state_df, colour = “white”, fill = NA) +
scale_fill_brewer(pal = “PuRd”, name=”Rate”)
m + coord_map() + opts(title=”US Unemployment Data (2009)”)
Worked like a charm. Note that there are some known problems with this solution. Some additional mapping/data cleanup is required to get a complete view of the data set (which explains why Louisiana is missing). I thought it would be neat to animate the results. So I subcontracted out animation using Gimp to a local expert. He created the following – a quick attempt to illustrate the changes over time. It is not ideal, you kind of have to pick a point stare at it and watch for changes over time.
Incidentally, blogger does not support animated gifs directly,
I know that there is an R animation package, but was not sure of its applicability in this scenario. How would you create an animation that illustrates changes on a map over time?