Part 1: Map presidential elections results with the maps package

1. Downloading the elections file from harvard database:

Website: https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/42MVDX

setwd("~/OneDrive - University of Georgia/4th Sem PhD/Adv Econometric Applications_Filipski/Assignments/HW8")
load("1976-2020-president.RData")
data <- x

2. Loading the data, and cleaning it up:

library(dplyr)
# we have data for all the candidates, but we need refine the candidates who won the election (highest vote) in each election in each state.
data2use <- data %>% group_by(year, state) %>% slice_max(candidatevotes, n = 1)
#define color so that we can use it for shading
data2use$color <- as.character(ifelse(data2use$party_detailed=="DEMOCRAT","blue", "red"))
#to make the dataset look like in question:
data2use <- subset(data2use, select= c(year, state, state_fips, party_detailed, candidate, color))

3. Mapping it for 2000:

#install.packages("maps")
library("maps")

data2use2000 <- subset(data2use, year == 2000)
#data2use2000$matched <- ifelse(match(data2use2000$state_fips, state.fips$fips),1,0)
#data2use2000 <- na.omit(data2use2000)   #remove dataset that do not match with fips code; fips is the federal code
sfips = merge(state.fips, data2use2000,
                    sort = FALSE,
                    by.x = "fips", by.y = "state_fips")

map("state", col=sfips$color, fill=TRUE)
title(main ="Presidential Election Results by State in 2000")

4. Now, looping that code and mapping it over time: 1976-2020

yearsseq <- seq(from=1976,to=2020,by=4)

par(mfrow=c(4,5), mar=c(0,0,0,0))

for (i in yearsseq) {
  df1 <- subset(data2use, year == i)    #first, subset the data for each year
  df2 <- merge(state.fips, df1,
                 sort = FALSE,
                 by.x = "fips", by.y = "state_fips")    #use the yearly subset and merge with inbuilt maps attribute table called state.fips and fill the color in state maps.
  map("state", col=df2$color, fill=TRUE)
  mtext(i,side=3)  #print years
}

Part 2: Interactive Maps with Leaflet

2.1. Getting familiar with the leaflet package

#install.packages("leaflet")
library("leaflet")
map1 <- leaflet() %>% addProviderTiles(providers$OpenStreetMap)
map1
map2 <- leaflet() %>% addProviderTiles(providers$Esri.NatGeoWorldMap)
map2
#install.packages("leaflet")
library("leaflet")
map3 <- leaflet() %>% 
  addProviderTiles(providers$OpenStreetMap) %>%
  setView(lat=33.947474, lng=-83.373671, zoom = 12)
map3
map4 <- leaflet() %>% 
  addProviderTiles(providers$OpenStreetMap) %>%
  setView(lat=33.930646644903014, lng=-83.37132916023171, zoom = 15) %>%
   addMarkers(lat=33.930646644903014, lng=-83.37132916023171, popup="Named for Allyn M. Herrick, former Dean of the School of Forest Resources, Lake Herrick was commissioned by the School in 1982 as a recreational resource for Athens and UGA. After 20 years of use, Lake Herrick was closed to swimming and boating in 2002. In 2017 work began to enhance Lake Herrick as a public amenity within Oconee Forest Park and provide a respite amidst the hustle and bustle of daily life, and by October 2018 the lake and nearby facilities were re-dedicated for public use. Oconee Forest Park and Lake Herrick remain under the management of the Warnell School of Forestry and Natural Resources. From: https://sustainability.uga.edu/community-engagement/lake-herrick/")
map4

2. Adding shapefiles

Please visit this link to see from where the shapefile are obtained for Rohingya refugee camps.

#install.packages("~/Downloads/sf_1.0-12.tgz", repos = NULL, type = .Platform$pkgType)
# if anyone have a hardtime to install sf package directly from CRAN repository, try manual download for MacOS/Windows and install it from your device.
library("sf")
library("terra")
library(raster)
library(rgdal)
library(rgeos)
sp <- shapefile("220130_RRC_Outline_block_AL2/T220130_RRC_Outline_Block_AL2.shp")

campShapeFile <- spTransform(sp, CRS("+proj=longlat +datum=WGS84 +no_defs"))
head(campShapeFile)
##   OBJECTID_1 OBJECTID Block_Let Camp_SSID Block_Name   Block_SSID SMSD_Cname
## 1          1        1         I   CXB-232     C04X_I CXB-232_I163   Camp 04X
## 2          2        2         B   CXB-232     C04X_B CXB-232_B165   Camp 04X
## 3          3        3         F   CXB-232     C04X_F CXB-232_F161   Camp 04X
## 4          4        4         C   CXB-232     C04X_C CXB-232_C166   Camp 04X
## 5          5        5         E   CXB-232     C04X_E CXB-232_E160   Camp 04X
## 6          6        6         H   CXB-232     C04X_H CXB-232_H162   Camp 04X
##         Camp_Alias         NPM_Cname Area_Acres         CampName
## 1 Camp 4 Extension Camp 04 Extension  17.579304 Camp 4 Extension
## 2 Camp 4 Extension Camp 04 Extension  19.796469 Camp 4 Extension
## 3 Camp 4 Extension Camp 04 Extension   8.892700 Camp 4 Extension
## 4 Camp 4 Extension Camp 04 Extension  40.189147 Camp 4 Extension
## 5 Camp 4 Extension Camp 04 Extension  17.429451 Camp 4 Extension
## 6 Camp 4 Extension Camp 04 Extension   8.238809 Camp 4 Extension
##           Area_SqM  Shape_Leng  Shape_Le_1   Shape_Area
## 1 71140.9196299387 0.012361753 0.012361755 6.192077e-06
## 2 80113.4668634516 0.010098287 0.010098287 6.973411e-06
## 3 35987.4806593324 0.007094379 0.007094378 3.132450e-06
## 4 162639.706686226 0.023266098 0.023266094 1.415641e-05
## 5 70534.4857807752 0.013253804 0.013253802 6.139350e-06
## 6 33341.2781127569 0.006681391 0.006681390 2.901994e-06
#Here is the map of South-Eastern Bangladesh with given lat-long, and we added the shapefile to this map with addPolygons command 
map5 <- leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  setView(92.14871, 21.18780, zoom = 12) %>%
  addPolygons(data=campShapeFile, fill=TRUE, stroke=T, weight=1)
map5

Let’s explore the options within addPolygons: The following interactive map shows name for each camps.

map6 <- leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  setView(92.14871, 21.18780, zoom = 13) %>%
  addPolygons(data=campShapeFile, fill=TRUE, stroke=T, color="red", weight=1, highlight = highlightOptions(fillOpacity = 0.7), label = campShapeFile$CampName)
label = campShapeFile$Block_No
map6

Move your arrow over the red region to see camp names.

With other label:

map6 <- leaflet() %>%
  addProviderTiles(providers$Esri.WorldStreetMap) %>%
  setView(92.14871, 21.18780, zoom = 13) %>%
  addPolygons(data=campShapeFile, fill=TRUE, stroke=T, color="orange", weight=1, highlight = highlightOptions(fillOpacity = 0.7), label = campShapeFile$Block_No)
map6

3. Adding tiles from the web:

New leaflet map with USA; Lets overlay a realtime raster image of current rainfall in the USA:

map7 <- leaflet() %>%
  addProviderTiles(providers$Esri.WorldTopoMap) %>%
  setView(lat=38.470504048991, lng= -100.57852944840563, zoom = 3.8) %>%
  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"
              )
map7

We can try lots of different raster files online from websites like the one from Iowa State University.