Mapping Historic US Presidential Election Results

Today I will demonstrate how to map historic US Presidential Election results in R. If you want to skip ahead and play with a shiny app that lets you visualize this dataset, then click here.

The dataset we will use comes from wikipedia’s List of United States presidential election results by state and is packaged in the choroplethr package as df_president_ts:



df_president_ts[1:8, 1:4]
       region 1789 1792 1796
1     alabama   NA   NA   NA
2      alaska   NA   NA   NA
3     arizona   NA   NA   NA
4    arkansas   NA   NA   NA
5  california   NA   NA   NA
6    colorado   NA   NA   NA
7 connecticut   GW   GW    F
8    delaware   GW   GW    F

The first column is a state name; subsequent columns are years that had presidential elections. NA means that the state did not yet exist. Note that by default choroplethr renders NA values as black. Here “GW” means “George Washington” and “F” means “Federalist”. You can see the legend at both the wikipedia link above as well as the help for ?df_president_ts.

In order to map data with choroplethr your data needs a column named region and a column named value. Here we are given the region column but not the value column. We can create a value column like this:

df_president_ts$value = df_president_ts$"1789"

                 title = "1789 Presidential Election Map")


I originally packaged up this dataset to demonstrate the animation functionality of choroplethr (see ?choroplethr_animate). But since I have recently been doing a lot of work with Shiny, I thought that it would be nice to create an app that lets users view all of these maps with a simple dropdown menu. You can see the running app here. The source code for the app is available here. Here is a screenshot of the running app:



From Around the Web

Alyssa Briggs Hislop did an interesting series of blog posts that use choroplethr to map the behavior of library patrons in America:

Deborah Nedelman says August 14, 2015

What a great idea! I thought I knew everything there was to know about the Presidents but you can always learn something new. Thanks!D G Nedelman

    Ari Lamstein says August 14, 2015

    Thanks Deborah! I was particularly surprised to see how recently Alaska became a state.

Chris von Csefalvay says August 15, 2015

Great stuff, Ari! That said, perhaps if you have a lot of time to improve on this a little, you could use different shapefiles per year that corresponds to state shapes. Thus for example what’s Tennessee today used be part of NC, which backed on to the Spanish Territory of Louisiana until 1790. Then it became an unorganized territory, and was so until Tennessee’s statehood and admission to the Union in 1796. The effort required to do this is, of course, pretty big… there are almost a hundred stages of territorial evolution, although you would only need the status quo at the election, whittling it down to about 35. Nonetheless, this is really great work! Congratulations!
Ps. really enjoyed your census explorer tutorial!

    Ari Lamstein says August 15, 2015

    Chris, thanks for the thoughtful comment – and I’m glad that you enjoyed my tutorial! Do you know where I can get shapefiles of US historic boundaries by year? Even if I just posted a shiny app of the evolution over time (ignoring the voting records), I think that it would be a fun, informative app that people might enjoy playing with.

Comments are closed