Mapping Historic US Presidential Election Results

August 13, 2015
By

(This article was first published on AriLamstein.com » R, and kindly contributed to R-bloggers)

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:

library(choroplethr)
library(choroplethrMaps)

data(df_president_ts)
?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"

state_choropleth(df_president_ts, 
                 title = "1789 Presidential Election Map")

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:

shiny-presidential-screenshot

 

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:

LEARN TO MAP CENSUS DATA
Subscribe and get my free email course: Mapping Census Data in R!

100% Privacy. We don’t spam.


The post Mapping Historic US Presidential Election Results appeared first on AriLamstein.com.

To leave a comment for the author, please follow the link and comment on their blog: AriLamstein.com » R.

R-bloggers.com 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.

Search R-bloggers


Sponsors

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)