Mapping the 2011 Chicago Mayoral Democratic Primary

February 25, 2011

(This article was first published on Offensive Politics » R, and kindly contributed to R-bloggers)


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.

Rham Emanuel with 55.2% of the overall vote

Gary Chico with 23.9% of the overall vote

Miguel Del Valle with 9.2% of the overall vote

Carol Moseley Braun with 8.9% of the overall vote

Patricia Van Pelt Watkins with 1.6% of the overall vote

William Walls III with 0.9% of the overall vote

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.

To leave a comment for the author, please follow the link and comment on their blog: Offensive Politics » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)