Election Transparency, Part #3: Building an Election App

Robert Kahne
Data for Democracy
Published in
3 min readApr 10, 2017

I built a shiny app that displays county-level Presidential election results back to the year 2000. It’s pretty neat. You should check it out. If you are inclined to do so, here’s my fork of the Data For Democracy Election Transparency Project, where I stored the code.

I’ve been working with the R language for about two years. I originally started using it to replace a lot of really terrible and overloaded spreadsheets that I was using to do my day job as an analyst at an early stage healthcare company in Louisville, KY. My background is in public policy, and as luck would have it, many of the best and brightest users of the R language are very active in using the language to study and improve government. We even had a group in my very city called the Civic Data Alliance which was dedicated to using open data to improve life in our city.

Kentucky (where I live) has gubernatorial elections on odd years, and we had one in 2015. After that election, several media groups put together maps for use in displaying elections results, but I was pretty unimpressed. I had a vision of what a good election results app would look like, and I set out to make one in 2015. It turns out it’s pretty hard.

By the time 2016 rolled around, there was another election. Based on my experience in 2015, I had an idea of what resources were needed to build the election app in my head — I figured out what a ‘shapefile’ was, how to use them in R, how to get them from people, and how to join them to other data. I figured out how Kentucky stores election data, and what it would take to tidy that in a way that was usable. After a lot of hard work…I had about a quarter of what I wanted (you can see the results of my labor here). I wanted to be able to visualize all the elections in all of Kentucky’s 120 counties, I managed to do six (most of the biggest ones, though!). While I’m immensely proud of what I was able to do, and very grateful for the help from numerous government officials and the members of the Civic Data Alliance, I still have a ways to go with that app.

Data, even open data, is a tough thing. It’s tough to get it! Once you’ve got it, it tough to use it! Even if you can use it, sometimes its hard to know what to do next! These issues are one of the many reasons I’m so happy to have found Data For Democracy. When a fellow Civic Data Alliance member alerted me to the group’s existence, I was really excited with what I found.

That excitement started when I opened up the election transparency section of Data for Democracy’s data.world page. There was already a ton of election data, in a clean and usable format, going back 17 years. Plus, it was all open! In perusing what work had been done for the election transparency project, I saw that one of the projects people wanted was “state and county level choropleths for elections going back to 2000”. And that’s when I got to work!

I was able to repurpose most of the code I’d used for the Kentucky app to create the election visualization. I added in some new bells and whistles (aggregated results at the bottom of the page, and shading to show the size of victory in a county), and I had to get new shapefiles (they were easily found on the US Census website). I was able to get something together in about two days and about 6 hours of actual coding, because the hard work of aggregating and tidying the data had mostly already been done by others working on the project. Anyone who is interested in how to use Shiny or the leaflet package for R, get in contact with me and I’ll help you out. It’s surprisingly easy to learn.

I’m proud of my election visualization. It’s gotten some good feedback on twitter from journalists I trust to tell important stories. The mission of data for democracy is to use data to create social good. I hope my work with this data helps that process. There are still projects to do, and I’m excited to work on new ones!

--

--