Working with Maps

1. Map presidential elections results with the maps package

In this exercise, you will explore regression discontinuity designs, based the following paper: Carpenter and Dobkin (2009) The Effect of Alcohol Consumption on Mortality: Regression Discontinuity Evidence from the Minimum Drinking Age AEJ: Applied Economics 1(1)164-82

1. Download the elections file from harvard database

2. Load the data, and clean it up

my_data_HW7 <- read.csv("C:/Users/diego/Documents/UGA/UGA_4/Metrics_II/HW7/1976_2020_president.csv") 
my_data_HW7 <-mutate(my_data_HW7,
                     color = ifelse(party_detailed == "DEMOCRAT", "blue", 
                                    ifelse(party_detailed == "REPUBLICAN","red",NA)))

my_data_HW7$color<-as.character(my_data_HW7$color)
my_data_HW7_win<-my_data_HW7%>%
                  group_by(year, state_fips)%>%
                      slice_max(candidatevotes)

3. Try to make a map just for one year

my_data_HW7_2020<-filter(my_data_HW7_win, year ==2020)
Q3<-maps::map("state", col=my_data_HW7_2020$color, fill=TRUE)

4. Now loop that code and map it over time

year_vector=seq(1976,2020,by=4)

par(mfrow=c(4,3))
for (year_ind in year_vector){
  my_data_HW7_2020<-filter(my_data_HW7_win, year == year_ind)
  maps::map("state", col=my_data_HW7_2020$color, fill=TRUE)
}

Part 2: Interactive Maps with Leaflet

1. Get familiar with the leaflet package

myMapa <- leaflet() %>%
  addProviderTiles(providers$OpenStreetMap)
myMapa
#Montevideo
#Uruguaynoma

myMapb <- leaflet() %>%
  addProviderTiles(providers$OpenTopoMap)%>%
    setView(lat=-34.901112, lng=-56.164532, zoom = 13)
myMapb
#UGA
myMapc <- leaflet() %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap)%>%
    setView(lat=33.947474, lng=-83.373671, zoom = 10)
myMapc

2. Add your own shapefiles

library(sf)
sp <- shapefile("C:/Users/diego/Documents/UGA/UGA_4/Metrics_II/HW7/200908_RRC_Outline_Block_AL2.shp")
# Projection is necessary for R to place the coordinates correctly
campShapeFile <- spTransform(sp, CRS("+proj=longlat +datum=WGS84 +no_defs"))
head(campShapeFile)

Block_Let Camp_SSID Block_Name Block_SSID SMSD_Cname Camp_Alias 0 I CXB-232 C04X_I CXB-232_I163 Camp 04X Camp 4 Extension 1 B CXB-232 C04X_B CXB-232_B165 Camp 04X Camp 4 Extension 2 F CXB-232 C04X_F CXB-232_F161 Camp 04X Camp 4 Extension 3 C CXB-232 C04X_C CXB-232_C166 Camp 04X Camp 4 Extension 4 E CXB-232 C04X_E CXB-232_E160 Camp 04X Camp 4 Extension 5 H CXB-232 C04X_H CXB-232_H162 Camp 04X Camp 4 Extension NPM_Cname Area_Acres CampName Area_SqM 0 Camp 04 Extension 17.597196 Camp 4 Extension 71213.3263972732 1 Camp 04 Extension 19.816614 Camp 4 Extension 80194.9934341609 2 Camp 04 Extension 8.901736 Camp 4 Extension 36024.0480281611 3 Camp 04 Extension 40.230092 Camp 4 Extension 162805.40781136 4 Camp 04 Extension 17.447146 Camp 4 Extension 70606.0954539348 5 Camp 04 Extension 8.247218 Camp 4 Extension 33375.3063270588

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

3. Add tiles from the web

#ASU
myMape <- leaflet() %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap)%>%
    setView(lng = -111.928001, lat=33.424564, zoom = 12)%>%
    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"
)
myMape