Part 1: Map presidential elections results with the maps package

1. Download the elections file from harvard database:

hw7<-read.csv("1976-2020-president.csv")

2. Load the data, and clean it up:

clean_hw7 <- hw7 %>% group_by(state, year) %>% top_n(n = 1, wt = candidatevotes)
clean_hw7 <- clean_hw7[-c(3,5:7,9:14)];
clean_hw7$color<-ifelse(clean_hw7$party_simplified=="OTHER","beige",ifelse(clean_hw7$party_simplified=="DEMOCRAT","blue","red"))

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

3. Try to make a map just for one year:

map(“state”, col=temp$quantcolors, fill=TRUE)

clean_hw7_2000<-subset(clean_hw7,year==2000)
clean_hw7_2000 <- clean_hw7_2000[match(paste(state.fips$fips),paste(clean_hw7_2000$state_fips)),]
map("state", col=clean_hw7_2000$color, fill=TRUE)

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

years=c(1976,1980,1984,1988,1992,1996,2000,2004,2008,2012,2016,2020)
par(mfrow=c(4,3), mar=c(0,0,0,0))
for(i in years) {
clean_hw7 <-clean_hw7[match(paste(state.fips$fips),paste(clean_hw7_2000$state_fips)),]
map("state", col=clean_hw7$color, fill=TRUE)
mtext(i,side=3,line=1)
}

Part 2: Interactive Maps with Leaflet

1. Get familiar with the leaflet package

myMap1 <- leaflet() %>%
addProviderTiles(providers$OpenTopoMap) %>%
setView(lat=35, lng=-79, zoom = 8)
myMap1
myMap2 <- leaflet() %>%
addProviderTiles(providers$OpenRailwayMap) %>%
setView(lat=35, lng=-79, zoom = 8)

myMap2
myMap3 <- leaflet() %>%
addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
setView(lat=33.947474, lng=-83.373671, zoom = 12)
myMap3

2. Add your own shapefiles

library(raster)
## Loading required package: sp
## 
## Attaching package: 'raster'
## The following object is masked from 'package:dplyr':
## 
##     select
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
options("rgdal_show_exportToProj4_warnings"="none")
library(rgdal)
## rgdal: version: 1.5-23, (SVN revision 1121)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: C:/Users/SHOUY/Documents/R/win-library/4.0/rgdal/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: C:/Users/SHOUY/Documents/R/win-library/4.0/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading rgdal.
## Overwritten PROJ_LIB was C:/Users/SHOUY/Documents/R/win-library/4.0/rgdal/proj
sp <- shapefile("200908_RRC_Outline_Block_AL2.shp")
# Projection
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
myMap4<-leaflet() %>%
addProviderTiles(providers$OpenTopoMap) %>%
addPolygons(data=campShapeFile,fill=TRUE, stroke=T, weight=1,highlight = highlightOptions(fillOpacity = 0.7),label = campShapeFile$Block_No) %>%
setView(92.14871, 21.18780, zoom = 12)
myMap4

3. Add tiles from the web

myMap5 <- leaflet() %>%
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"
) %>%
addProviderTiles(providers$OpenTopoMap) %>%
setView(-98.5696, 39.8282, zoom =4)
  
myMap5