Mapping the 2011 Chicago Mayoral Democratic Primary
Chicago held a Democratic primary on Feb 22nd, and I wanted to visualize the results of the 6-way Mayoral race. I’m a huge fan of mapping election results with R, but I was never able to get my favorite graphing library ggplot2 working correctly with shapefiles. After some light googling I found an excellent ggplot2 wiki page that described the whole process. Thanks to whomever put that page together, it was a really helpful resource.
In this blog post I will detail how I joined election results and maps to create a graphical summary of the mayoral primary election.
Chicago is split into 50 wards, and those wards are each split into a number of precincts. The city of Chicago helpfully provides precinct-level boundary shape files on their GIS data page.
I pulled the precinct results from the Chicago board of elections results page. These data were provided in easily parsed HTML pages, unlike so many other states (Virginia I’m looking at you).
The precinct shapefiles needed no preprocessing, which is quite surprising. Usually municipality names are off, or data are missing, but not this time.
I used Ruby to scrape each precincts results and saved them off to a CSV file. The individual files were loaded into Nokogiri and data was easily extracted with Xpath queries. The Ruby file to perform the preprocessing can be viewed here
I made use of several external libraries when building the maps. The shapefiles were loaded with the maptools package, and then converted and plotted with ggplot2 and colored by RColorBrewer. The full R code w/ some inline comments can be found here
Given the multi-party nature of this election I chose to create a turnout map for each candidate. This will allow us to more easily see the regional strengths of each candidate. Click an image below to view full-size.
Grab the code from Github: Chicago Mayoral 2011 code. Comments, forks, and pull requests, are greatly appreciated.
Edit: If you enjoyed this post please check out my basic precinct analysis on the Chicago Mayoral election.