Mapping the 2011 Chicago Mayoral Democratic Primary

2 minute read


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.

Data Sources

Precinct Boundaries

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.

Election Results

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).

Data Preprocessing

Shape files

The precinct shapefiles needed no preprocessing, which is quite surprising. Usually municipality names are off, or data are missing, but not this time.

Election Results

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.

[caption id="attachment_496" align="aligncenter" width="300" caption="Rham Emanuel with 55.2% of the overall vote"][/caption] [caption id="attachment_494" align="aligncenter" width="300" caption="Gary Chico with 23.9% of the overall vote"][/caption]
[caption id="attachment_495" align="aligncenter" width="300" caption="Miguel Del Valle with 9.2% of the overall vote"][/caption] [caption id="attachment_493" align="aligncenter" width="300" caption="Carol Moseley Braun with 8.9% of the overall vote"][/caption]
[caption id="attachment_498" align="aligncenter" width="300" caption="Patricia Van Pelt Watkins with 1.6% of the overall vote"][/caption] [caption id="attachment_497" align="aligncenter" width="300" caption="William Walls III with 0.9% of the overall vote"][/caption]

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.