HOMEWORK 5: Map presidential elections results with the maps package

Reference and disclaimer: For this homework, I have used the election files from the “Harvard Database”. They have data on candidate votes for several years for different states in the United States. In order to do this exercise, I use Maps package in R and match the states by their fips code (a unique code that represents each state).

Initially I load the necessary packages and do some data cleaning to get only the data on candidates who won the elections in each State across different years. The following code does this.

list.of.packages <- c("maps", "dplyr", "tidyverse", "car", "RColorBrewer")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
data <- read.table("D:\\2020_Spring\\AAEC8610\\HW5\\1976-2016-president.csv", header = TRUE, sep = ",")
library(maps)
library(dplyr)
library(tidyverse)
library(RColorBrewer)
y <- data %>% group_by(state,year) %>% arrange(desc(candidatevotes)) %>% slice(1) %>% ungroup()
y <- data.frame(y)
y <- y %>%
  mutate(color = case_when(
    party == "republican" ~ 1,
    party == "democrat" ~ 7,
    TRUE                      ~ 3
  ))

Now, I plot the election results for year 2000.

election2000_winners <- y[y$year==2000,c("state", "state_fips","party","color")]
election2000_winners <- election2000_winners[match(paste(state.fips$fips),paste(election2000_winners$state_fips)),]
colpal <- RColorBrewer::brewer.pal(7, 'RdBu')
election2000_winners$quantcolors <- colpal[election2000_winners$color]
str(election2000_winners)
## 'data.frame':    63 obs. of  5 variables:
##  $ state      : Factor w/ 51 levels "Alabama","Alaska",..: 1 3 4 5 6 7 8 9 10 11 ...
##  $ state_fips : int  1 4 5 6 8 9 10 11 12 13 ...
##  $ party      : Factor w/ 148 levels "","6 million jobs",..: 113 113 113 36 113 36 36 36 113 113 ...
##  $ color      : num  1 1 1 7 1 7 7 7 1 1 ...
##  $ quantcolors: chr  "#B2182B" "#B2182B" "#B2182B" "#2166AC" ...
par(mfrow=c(1,1), mar=c(0,0,0,0))
maps::map("state", col=election2000_winners$quantcolors, fill=TRUE)
title("Presidential election results by State in 2000",adj = 0.4, line = 0.1)

Now, I use loops to do the same but for several years together.

years=c(1976,1980,1984,1988,1992,1996,2000,2004,2008,2012,2016)
par(mfrow=c(4,3), mar=c(0,0,0,0), bg ="gray80")
for (i in years) {
  election_winners <- y[y$year==i,c("state", "state_fips","party","color")]
  election_winners <- election_winners[match(paste(state.fips$fips),paste(election_winners$state_fips)),]
  election_winners$quantcolors <- colpal[election_winners$color]
  maps::map("state", col=election_winners$quantcolors, fill=T)
  mtext(i,side=3,line=1)
}
plot(1, type="n", xlab="", ylab="",axes=FALSE,ann=FALSE)
title("Presidential election results \n by state and year",cex.main=1.2, adj = 0.4, line = 0.1)
legend("bottom", legend=c("Republican","Democrat", "Others"), 
       col=c("black","black","black"), pch=c(22, 22, 22),
       pt.bg=adjustcolor(c('blue', 'red', 'beige')),  cex=1.3)