Optional task 1: Map presidential elections results with the maps package

Part 1.1 Download the elections file from harvard database & Load the data

I download the data “1976-2016-president” from Harvard dataverse portal. The dataset is in .csv format. I could load it into R using read.table but I prefer to convert it into .xlsx format using Excel so that I can use read_excel as shown below:

library(readxl)
task1Data <- read_excel("1976-2016-president.xlsx", 1)
str(task1Data)
## Classes 'tbl_df', 'tbl' and 'data.frame':    3740 obs. of  14 variables:
##  $ year          : num  1976 1976 1976 1976 1976 ...
##  $ state         : chr  "Alabama" "Alabama" "Alabama" "Alabama" ...
##  $ state_po      : chr  "AL" "AL" "AL" "AL" ...
##  $ state_fips    : num  1 1 1 1 1 1 1 2 2 2 ...
##  $ state_cen     : num  63 63 63 63 63 63 63 94 94 94 ...
##  $ state_ic      : num  41 41 41 41 41 41 41 81 81 81 ...
##  $ office        : chr  "US President" "US President" "US President" "US President" ...
##  $ candidate     : chr  "Carter, Jimmy" "Ford, Gerald" "Maddox, Lester" "Bubar, Benjamin \"\"Ben\"\"" ...
##  $ party         : chr  "democrat" "republican" "american independent party" "prohibition" ...
##  $ writein       : chr  "FALSE" "FALSE" "FALSE" "FALSE" ...
##  $ candidatevotes: num  659170 504070 9198 6669 1954 ...
##  $ totalvotes    : num  1182850 1182850 1182850 1182850 1182850 ...
##  $ version       : num  20171015 20171015 20171015 20171015 20171015 ...
##  $ notes         : chr  "NA" "NA" "NA" "NA" ...
summary(task1Data$candidatevotes)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1    1499    8992  315139  224505 8753788
summarize(task1Data)
## # A tibble: 1 x 0

Part 1.2 Clean up the loaded data

In this section, the first goal is to make a dataset at the state*year level, with infomation on the party who won in that state and that year.

task1Data <- data.frame(task1Data)

winnerStateYear <- task1Data %>%
  group_by(state, year) %>%
  top_n(n = 1, wt = candidatevotes)
winnerStateYear
## # A tibble: 561 x 14
## # Groups:   state, year [561]
##     year state state_po state_fips state_cen state_ic office candidate party
##    <dbl> <chr> <chr>         <dbl>     <dbl>    <dbl> <chr>  <chr>     <chr>
##  1  1976 Alab~ AL                1        63       41 US Pr~ Carter, ~ demo~
##  2  1976 Alas~ AK                2        94       81 US Pr~ Ford, Ge~ repu~
##  3  1976 Ariz~ AZ                4        86       61 US Pr~ Ford, Ge~ repu~
##  4  1976 Arka~ AR                5        71       42 US Pr~ Carter, ~ demo~
##  5  1976 Cali~ CA                6        93       71 US Pr~ Ford, Ge~ repu~
##  6  1976 Colo~ CO                8        84       62 US Pr~ Ford, Ge~ repu~
##  7  1976 Conn~ CT                9        16        1 US Pr~ Ford, Ge~ repu~
##  8  1976 Dela~ DE               10        51       11 US Pr~ Carter, ~ demo~
##  9  1976 Dist~ DC               11        53       55 US Pr~ Carter, ~ demo~
## 10  1976 Flor~ FL               12        59       43 US Pr~ Carter, ~ demo~
## # ... with 551 more rows, and 5 more variables: writein <chr>,
## #   candidatevotes <dbl>, totalvotes <dbl>, version <dbl>, notes <chr>

The other goal is to define the color of each party (blue for democrats, red for republicans, purple for other parties) and select a subset of the data: the six variables (year, state, state_fips, party, candidate, color).

colorSubsetWinnerStateYear <- winnerStateYear %>% 
  select(year, state, state_fips, party, candidate) %>%
  mutate(color = case_when(
    party == "republican" ~ "red",
    party == "democrat" ~ "blue",
    TRUE ~ "purple"
  )) %>%
  mutate(color = as.character(color))
colorSubsetWinnerStateYear
## # A tibble: 561 x 6
## # Groups:   state, year [561]
##     year state                state_fips party      candidate     color
##    <dbl> <chr>                     <dbl> <chr>      <chr>         <chr>
##  1  1976 Alabama                       1 democrat   Carter, Jimmy blue 
##  2  1976 Alaska                        2 republican Ford, Gerald  red  
##  3  1976 Arizona                       4 republican Ford, Gerald  red  
##  4  1976 Arkansas                      5 democrat   Carter, Jimmy blue 
##  5  1976 California                    6 republican Ford, Gerald  red  
##  6  1976 Colorado                      8 republican Ford, Gerald  red  
##  7  1976 Connecticut                   9 republican Ford, Gerald  red  
##  8  1976 Delaware                     10 democrat   Carter, Jimmy blue 
##  9  1976 District of Columbia         11 democrat   Carter, Jimmy blue 
## 10  1976 Florida                      12 democrat   Carter, Jimmy blue 
## # ... with 551 more rows

Part 1.3 Making a map just for year 2000 in three steps

# Step 1: subsetting data for year 2000
colorSubsetWinnerStateYear2000 <- filter(colorSubsetWinnerStateYear, year == 2000) 

# Step 2: Selecting observations that match state.fips $fips
mapyear2000 <-  colorSubsetWinnerStateYear2000[match(paste(state.fips$fips),paste(colorSubsetWinnerStateYear2000$state_fips)),]

# Step 3: Map outcome with legend
par(mfrow=c(1,1), mar=c(0,0,0,0))
map("state", col=mapyear2000$color, fill=TRUE)
title("Presidential election results by State in 2000",
      adj = 0.25, line = 1)
legend("bottom", legend=c("Democratic","Republican", "Other"), 
       col=c("black","black","black"), pch=c(22, 22, 22), pt.cex=1.5,
       horiz=TRUE, cex=0.75, border=TRUE, box.lty=1, ncol=1, 
       title=NULL,
       pt.bg=adjustcolor(c('blue', 'red', 'purple')))

Part 1.4 Now loop that code and map it over time

The goal of this section is to make a map for all years similar to those in Homework 2.

electionyears <- seq(1976, 2016, by=4)
par(mfrow=c(4,3), mar=c(0,0,0,0), bg = 'gray')
for (y in electionyears) {
  # Step 1
  colorSubsetWinnerStatey <- colorSubsetWinnerStateYear[colorSubsetWinnerStateYear$year==y, ]
  # Step 2
  mapyeary <- colorSubsetWinnerStatey[match(paste(state.fips$fips),paste(colorSubsetWinnerStatey$state_fips)),]
  # Step 3
  map("state", col=mapyeary$color, fill=TRUE)
  mtext(y,side=3,line=0.25)
}
plot(0, axes=FALSE, xlab="", ylab="", type="n", cex=1)
title("Presidential election results \n by State and year", 
      adj = 0.25, line = 0.25)
legend("bottom", legend=c("Democratic","Republican", "Other"), 
       col=c("black","black","black"), pch=c(22, 22, 22), pt.cex=1.5,
       horiz=FALSE, cex=1, border=TRUE, box.lty=1, ncol=1, 
       title=NULL,
       pt.bg=adjustcolor(c('blue', 'red', 'purple')))

Notice how the map for year 2000 with its legend in Part 1.3 is exactly represented in Part 1.4.

Optional task 2: Interactive Maps with Leaflet

Part 2.1. Get familiar with the leaflet package

  • Left for own practice work.

Part 2.2. Add your own shapefiles

  • Go to this page and download the shapefile “190310_Outline_Rohingya_Refugee_CampBlock_A2.shp”. Unzip and load it into R as shown below:
library(sf)
sp <- read_sf("190310_Outline_Rohingya_Refugee_CampBlock_A2.shp")
campShapeFile <- sp %>%
  st_transform(4326)
head(campShapeFile)
## Simple feature collection with 6 features and 9 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 92.13366 ymin: 21.2007 xmax: 92.13933 ymax: 21.21256
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
## # A tibble: 6 x 10
##   OBJECTID Block_No Block_ID Blck_Let CampName CampSSID Shape_Leng Shape_Area
##      <dbl> <chr>    <chr>    <chr>    <chr>    <chr>         <dbl>      <dbl>
## 1      159 163      CXB-232~ H        Camp 4 ~ CXB-232       1336.       7.11
## 2      160 157      CXB-232~ B        Camp 4 ~ CXB-232       1082.       8.01
## 3      161 161      CXB-232~ F        Camp 4 ~ CXB-232        761.       3.60
## 4      162 158      CXB-232~ C        Camp 4 ~ CXB-232       2511.      16.3 
## 5      163 160      CXB-232~ E        Camp 4 ~ CXB-232       1430.       7.05
## 6      164 162      CXB-232~ G        Camp 4 ~ CXB-232        715.       3.33
## # ... with 2 more variables: BlockNam <chr>, geometry <MULTIPOLYGON [°]>
  • Zoom the map onto South-Eastern Bangladesh (setView(92.14871, 21.18780, zoom = 12)) and add the shapefile to the map as follows:
library(leaflet)
leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  setView(92.14871, 21.18780, zoom = 12) %>%
  addPolygons(data=campShapeFile, fill=TRUE, stroke=TRUE, weight=1)
  • Add some highlights and labels
leaflet() %>%
  addProviderTiles(providers$Esri) %>%
  setView(92.14871, 21.18780, zoom = 12) %>%
  addPolygons(data=campShapeFile, fill=TRUE, stroke=TRUE, weight=1, highlight = highlightOptions(fillOpacity = 0.7), label = campShapeFile$Block_No)

Part 2.3 Add tiles from the web

  • Adding tiles on a new leaflet map of the USA.
mapUSA = map("state", fill=T, plot=F)
leaflet() %>%
  addPolygons(data=mapUSA, fill=TRUE, stroke=TRUE, weight=1)  %>%
  addWMSTiles(
"http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
layers = "nexrad-n0r-900913",
options = WMSTileOptions(format = "image/png", transparent = TRUE),
attribution = "Weather data © 2012 IEM Nexrad"
)

### Part 2.4 For more excellent leaflet features

Optional task 3: Maps with STATA

Curious about how to create maps in STATA?