Site icon R-bloggers

Mapping the 2011 Chicago Mayoral Democratic Primary

[This article was first published on Offensive Politics » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

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

Graphing

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

Results

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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.