Part 1: Map presidential elections results with the maps package
1. Download the elections file from harvard database:
library(dplyr)
library(maps)
library(leaflet)
library(sf)
library(rgdal)
library(raster)
mydata <- read.csv("1976-2020-president.csv")
summary(mydata)
## year state state_po state_fips
## Min. :1976 Length:4287 Length:4287 Min. : 1.00
## 1st Qu.:1988 Class :character Class :character 1st Qu.:16.00
## Median :2000 Mode :character Mode :character Median :28.00
## Mean :1999 Mean :28.62
## 3rd Qu.:2012 3rd Qu.:41.00
## Max. :2020 Max. :56.00
## state_cen state_ic office candidate
## Min. :11.00 Min. : 1.00 Length:4287 Length:4287
## 1st Qu.:33.00 1st Qu.:22.00 Class :character Class :character
## Median :53.00 Median :42.00 Mode :character Mode :character
## Mean :53.67 Mean :39.75
## 3rd Qu.:81.00 3rd Qu.:61.00
## Max. :95.00 Max. :82.00
## party_detailed writein candidatevotes totalvotes
## Length:4287 Mode :logical Min. : 0 Min. : 123574
## Class :character FALSE:3807 1st Qu.: 1177 1st Qu.: 652274
## Mode :character TRUE :477 Median : 7499 Median : 1569180
## NA's :3 Mean : 311908 Mean : 2366924
## 3rd Qu.: 199242 3rd Qu.: 3033118
## Max. :11110250 Max. :17500881
## version notes party_simplified
## Min. :20210113 Mode:logical Length:4287
## 1st Qu.:20210113 NA's:4287 Class :character
## Median :20210113 Mode :character
## Mean :20210113
## 3rd Qu.:20210113
## Max. :20210113
2. Load the data, and clean it up:
#define color in the dataset
mydata$color <- ifelse(mydata$party_detailed == "DEMOCRAT", "blue",
ifelse(mydata$party_detailed == "REPUBLICAN", "red","white"))
#select the winning observations
winner <- mydata %>% group_by(state, year) %>% top_n(1, candidatevotes)
winner <- winner[, c("year", "state", "state_fips", "party_detailed", "candidate", "color")]
state <- winner[winner$year == 2000,]
state <- state[match(paste(state.fips$fips),paste(state$state_fips)),]
3. Try to make a map just for one year:
map("state", col = state$color, fill = TRUE)
legend("bottomright", legend = c("DEMOCRAT", "REPUBLICAN"), fill = c("blue", "red"), cex = 0.7)
title("Presidential election results by state in 2000")

4. Now loop that code and map it over time:
# create loop and map over time
years = seq(1976, 2020, 4)
par(mfrow=c(4,3), mar=c(0,0,0,0))
for (i in years) {
temp <- winner[winner$year == i, c("state", "state_fips", "party_detailed", "color")]
temp <- temp[match(paste(state.fips$fips), paste(temp$state_fips)),]
map("state", col = temp$color, fill = TRUE)
mtext(i, side = 3, line = 1, cex = 0.8)
}

Part 2: Interactive Maps with Leaflet
1. Get familiar with the leaflet package
myMap <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
setView(lat=33.947474, lng=-83.373671, zoom = 12)
myMap
2. Add your own shapefiles
library(sf)
sp <- shapefile("/Users/harikrishnansanthosh/Desktop/UGA/AQME/Assignments_R/220130_RRC_Outline_block_AL2/T220130_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)
## OBJECTID_1 OBJECTID Block_Let Camp_SSID Block_Name Block_SSID SMSD_Cname
## 0 1 1 I CXB-232 C04X_I CXB-232_I163 Camp 04X
## 1 2 2 B CXB-232 C04X_B CXB-232_B165 Camp 04X
## 2 3 3 F CXB-232 C04X_F CXB-232_F161 Camp 04X
## 3 4 4 C CXB-232 C04X_C CXB-232_C166 Camp 04X
## 4 5 5 E CXB-232 C04X_E CXB-232_E160 Camp 04X
## 5 6 6 H CXB-232 C04X_H CXB-232_H162 Camp 04X
## Camp_Alias NPM_Cname Area_Acres CampName
## 0 Camp 4 Extension Camp 04 Extension 17.579304 Camp 4 Extension
## 1 Camp 4 Extension Camp 04 Extension 19.796469 Camp 4 Extension
## 2 Camp 4 Extension Camp 04 Extension 8.892700 Camp 4 Extension
## 3 Camp 4 Extension Camp 04 Extension 40.189147 Camp 4 Extension
## 4 Camp 4 Extension Camp 04 Extension 17.429451 Camp 4 Extension
## 5 Camp 4 Extension Camp 04 Extension 8.238809 Camp 4 Extension
## Area_SqM Shape_Leng Shape_Le_1 Shape_Area
## 0 71140.9196299387 0.012361753 0.012361755 6.192077e-06
## 1 80113.4668634516 0.010098287 0.010098287 6.973411e-06
## 2 35987.4806593324 0.007094379 0.007094378 3.132450e-06
## 3 162639.706686226 0.023266098 0.023266094 1.415641e-05
## 4 70534.4857807752 0.013253804 0.013253802 6.139350e-06
## 5 33341.2781127569 0.006681391 0.006681390 2.901994e-06
Map2 <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
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)
Map2
3. Add tiles from the web
Map3 <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
setView(lat=37.0902 ,lng= -95.7129,zoom = 4) %>%
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"
)
Map3