Homework 8- Maps

Duflo’s Plumber Economist

2023-02-14

#Loading required packages (might not end up using all of the loaded ones)
library(prettydoc) #For the theme used in this document
library(reshape2) #Required for reshaping the data
library(dplyr)
library(maps)
library(leaflet)
library(sf)
library(maptools)
library(raster)
library(rgdal)
library(rgeos)

Part 1: Part 1: Map presidential elections results with the maps package

1. Download the elections file from harvard database:

Downloaded and saved.

2. Load the data, and clean it up:

#Setting up the directory
setwd("D:/UGA Coursework/Second Year/AAEC 8610/HWs/HW8")
getwd()
## [1] "D:/UGA Coursework/Second Year/AAEC 8610/HWs/HW8"
df_maps <- read.csv("1976-2020-President.csv")

just_winners <- df_maps %>% 
  group_by(state, year) %>% 
  top_n(n=1, candidatevotes)

just_winners <- just_winners %>% 
  mutate(color = case_when(
    party_simplified == "DEMOCRAT" ~ "blue",
    party_simplified == "REPUBLICAN" ~ "red",
    TRUE ~ "white"
  ))

head(just_winners)
## # A tibble: 6 × 16
## # Groups:   state, year [6]
##    year state     state…¹ state…² state…³ state…⁴ office candi…⁵ party…⁶ writein
##   <int> <chr>     <chr>     <int>   <int>   <int> <chr>  <chr>   <chr>   <lgl>  
## 1  1976 ALABAMA   AL            1      63      41 US PR… CARTER… DEMOCR… FALSE  
## 2  1976 ALASKA    AK            2      94      81 US PR… FORD, … REPUBL… FALSE  
## 3  1976 ARIZONA   AZ            4      86      61 US PR… FORD, … REPUBL… FALSE  
## 4  1976 ARKANSAS  AR            5      71      42 US PR… CARTER… DEMOCR… FALSE  
## 5  1976 CALIFORN… CA            6      93      71 US PR… FORD, … REPUBL… FALSE  
## 6  1976 COLORADO  CO            8      84      62 US PR… FORD, … REPUBL… FALSE  
## # … with 6 more variables: candidatevotes <int>, totalvotes <int>,
## #   version <int>, notes <lgl>, party_simplified <chr>, color <chr>, and
## #   abbreviated variable names ¹​state_po, ²​state_fips, ³​state_cen, ⁴​state_ic,
## #   ⁵​candidate, ⁶​party_detailed
us_map <- just_winners[just_winners$year==2000, c("state_fips","party_simplified", "color")]
map("state", col=us_map$color, fill=T) 
title(main ="Presidential election results by State in 2000", line =1, cex.main=0.8)

#I dont know why but I am not getting many states right.

4. Now loop that code and map it over time:

years <- unique(just_winners$year)
par(mfrow=c(4,3), mar=c(0,0,0,0))
for (i in years) {
us_map <- just_winners[just_winners$year==2000, c("state_fips","party_simplified", "color")]
map("state", col=us_map$color, fill=T)
}

Part 2: Interactive Maps with Leaflet

1. Get familiar with the leaflet package

myMap_OpenStreetMap <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap)
myMap_OpenStreetMap
myMap_Esri <- leaflet() %>%
addProviderTiles(providers$Esri.NatGeoWorldMap)
myMap_Esri
myMap_Esri1 <- leaflet() %>%
addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
  setView(lat=33.947474, lng=-83.373671, zoom = 12)
myMap_Esri1
download.file("https://data.humdata.org/dataset/1a67eb3b-57d8-4062-b562-049ad62a85fd/resource/9d5693ec-eeb8-42ed-9b65-4c279f523276/download/220130_rrc_outline_block_al2.zip", destfile = "RohingyRrefugeeCamps.zip")
unzip("RohingyRrefugeeCamps.zip")
file.rename(from = "220130_RRC_Outline_block_AL2", to = "RohingyRrefugeeCamps")
## [1] FALSE
map_path <- file.path("RohingyRrefugeeCamps", "T220130_RRC_Outline_Block_AL2.shp")
sp <- shapefile(map_path)
campShapeFile <- spTransform(sp, CRS("+proj=longlat +datum=WGS84 +no_defs"))

RefugeeCamps <- leaflet() %>% 
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
  setView(92.14871, 21.18780, zoom = 12) %>%
  addPolygons(data=campShapeFile, fill=TRUE, stroke=T, weight=1,
        highlight = highlightOptions(fillOpacity = 0.7),
        label = campShapeFile$Block_No)
RefugeeCamps

3. Add tiles from the web

myMap <- leaflet() %>% 
  setView(-92, 38, 4) %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
  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 (c) 2012 IEM Nexrad"
  )
myMap