Part 1: Map presidential elections results with the maps package

Alternative option: map whatever you want with the maps package, as long as it involves merging some data with some geography and visualizing it in some interesting way.

1. Download the elections file from harvard database:

https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/42MVDX. (you can download directly into R, but thatโ€™s for another day. For now do it by hand and save the file on your disk)

2. Load the data, and clean it up:

You should aim to make a dataset at the state*year level, with infomation on the party who won in that state and that year. Something like 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, repos="http://cran.us.r-project.org")
library(dplyr)
library(maps)
library(tidyverse)


election<-read.csv("1976-2020-president.csv")
head(election)
##   year   state state_po state_fips state_cen state_ic       office
## 1 1976 ALABAMA       AL          1        63       41 US PRESIDENT
## 2 1976 ALABAMA       AL          1        63       41 US PRESIDENT
## 3 1976 ALABAMA       AL          1        63       41 US PRESIDENT
## 4 1976 ALABAMA       AL          1        63       41 US PRESIDENT
## 5 1976 ALABAMA       AL          1        63       41 US PRESIDENT
## 6 1976 ALABAMA       AL          1        63       41 US PRESIDENT
##                 candidate             party_detailed writein candidatevotes
## 1           CARTER, JIMMY                   DEMOCRAT   FALSE         659170
## 2            FORD, GERALD                 REPUBLICAN   FALSE         504070
## 3          MADDOX, LESTER AMERICAN INDEPENDENT PARTY   FALSE           9198
## 4 BUBAR, BENJAMIN ""BEN""                PROHIBITION   FALSE           6669
## 5               HALL, GUS        COMMUNIST PARTY USE   FALSE           1954
## 6         MACBRIDE, ROGER                LIBERTARIAN   FALSE           1481
##   totalvotes  version notes party_simplified
## 1    1182850 20210113    NA         DEMOCRAT
## 2    1182850 20210113    NA       REPUBLICAN
## 3    1182850 20210113    NA            OTHER
## 4    1182850 20210113    NA            OTHER
## 5    1182850 20210113    NA            OTHER
## 6    1182850 20210113    NA      LIBERTARIAN
election <- election %>% group_by(year, state)
election <- election %>% filter(candidatevotes == max(candidatevotes))
hw7 <- election %>% select(year, state, state_fips, party_detailed, candidate)

head(hw7)
## # A tibble: 6 x 5
## # Groups:   year, state [6]
##    year state      state_fips party_detailed candidate    
##   <int> <chr>           <int> <chr>          <chr>        
## 1  1976 ALABAMA             1 DEMOCRAT       CARTER, JIMMY
## 2  1976 ALASKA              2 REPUBLICAN     FORD, GERALD 
## 3  1976 ARIZONA             4 REPUBLICAN     FORD, GERALD 
## 4  1976 ARKANSAS            5 DEMOCRAT       CARTER, JIMMY
## 5  1976 CALIFORNIA          6 REPUBLICAN     FORD, GERALD 
## 6  1976 COLORADO            8 REPUBLICAN     FORD, GERALD