2016 Texas Primaries: Mapped

The inspiration for this post comes from R. Duncan McIntosh’s post on Florida’s 2016 Primaries, by way of his post as seen on R-Bloggers.
EDIT: GitHub repo: https://github.com/SeaSmith1018/TexasPrimary2016


Let’s take a look at some of the 2016 Texas Primary results.  I will be using McIntosh’s example except I will be working with Texas data (of course), and I will be using one extra library (rvest to gather the data) and one fewer library (reshape2).  First step, I will be loading the libraries and then reading in the Republican primary results.


#Texas election data: http://elections.sos.state.tx.us/index.htm

tex.rep            <- "http://elections.sos.state.tx.us/elchist273_race62.htm" %>%
                      read_html() %>%
                      html_nodes("table") %>%
tex.rep            <- tex.rep[[1]]

Next,  I am going to set up the column names that I want.  Then I am going to eliminate the first three rows, which consists of the fractured candidate names and a totals row.  I will also be setting the appropriate data type for the columns (CountyName = character, EverythingElse = numeric) plus reducing CountyName to all lower-cases (for joining with the geographic data).

#set names
tr.first           <- names(tex.rep)
tr.last            <- tex.rep[1,]
names(tex.rep)     <- c("CountyName", tr.last[2:14], "Uncommitted", "TotalVotes", "TotalVoters", "TurnOut")

#tidy up
tex.rep            <- tex.rep[-(1:3),]
tex.rep$CountyName <- tolower(as.character(tex.rep$CountyName))
tex.rep[,2:17]     <- sapply(tex.rep[,2:17], function(x) as.numeric(gsub(",", "", x)))
tex.rep[,18]       <- sapply(tex.rep[,18], function(x) as.numeric(gsub("%", "", x)))
tex.rep$CountyName <- gsub("lasalle", "la salle", tex.rep$CountyName)


We’re only concerned with winners here…as far as being defined as those candidates who were (successfully) actively-campaigning at the time of the 2016 Texas primary – John Kasich, Marco Rubio, Donald Trump, and Ted Cruz.  Time to calculate their percent winnings.

#add percent columns for top 4
tex.rep <- mutate(tex.rep,
                jk = (Kasich/TotalVotes)*100,
                mr = (Rubio/TotalVotes)*100,
                dt = (Trump/TotalVotes)*100,
                tc = (Cruz/TotalVotes)*100
tex.rep[,19:22] <- round(tex.rep[,19:22], digits = 1)

Tabling the Data

In McIntosh’s example, the data is tabled in knitr tables (which provide a clean look using “|”, “-“, and “:” for spacing. Also, I used library(htmlTable) to create the html table that you see just below the code.

dt.counties <- filter(tex.rep, Trump > Cruz & Trump > Rubio & Trump > Kasich) %>%
kable(dt.counties, caption = "Counties won by Trump")

The output data…

CountyName jk mr dt tc
1 aransas 5.1 14.8 39.3 31.1
2 hudspeth 0 15.1 40.9 38.2
3 sabine 1.9 8 42.6 40.2
4 terrell 0.8 11 42.4 35.6
5 webb 1.5 28.4 34.9 28.2
6 zapata 0 12.8 39.5 37.2


Now it’s time to gather the geographic data to which I will be mapping the above data.

#get state geo data
tx.regions <- filter(county.regions, state.name == "texas") %>%
              select(region, "CountyName" = county.name)
tx.r.results <- left_join(tex.rep, tx.regions)


Now that the data show the winner among the four candidates, I can now assign each candidate’s winnings (percent winnings) to the variable “value” (a required column for the choroplethr mapping functions). Along with “region”, which was assembled via the join function, I can now pass arguments to choroplethr’s county_choropleth() function.

#Ted Cruz
tx.r.tc       <- tx.r.results
tx.r.tc$value <- tx.r.results$tc
choro_tc      <- county_choropleth(tx.r.tc, state_zoom="texas", legend = "%", num_colors=1) +
                 ggtitle("Ted Cruz") +
                 coord_map()  # Adds a Mercator projection
Results for Ted Cruz of the Republican Party

Other Examples


Can you dig it?

4 thoughts on “2016 Texas Primaries: Mapped

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s