Part 1: Map presidential elections results with the maps package
1/2 Download the elections file from Harvard database and clean it up
# Retrieve downloaded dataset and rename it
load("1976-2020-president.RData"); elections <- x; remove (x)
colnames(elections)
## [1] "year" "state" "state_po" "state_fips"
## [5] "state_cen" "state_ic" "office" "candidate"
## [9] "party_detailed" "writein" "candidatevotes" "totalvotes"
## [13] "version" "notes" "party_simplified"
library(dplyr) # for data wrangling
winners = elections %>%
select(year, state, state_fips, candidate,candidatevotes, party_detailed) %>%
# select top vote-getter for every year in each state
group_by(year, state) %>% slice_max(candidatevotes) %>%
# assign the usual colors based on winner's political affiliation
# assign 'white' to three winners outside of the two main parties
mutate(color = ifelse(party_detailed == "DEMOCRAT", "blue",
ifelse(party_detailed == "REPUBLICAN", "red", "white")))
3. Election results map for 2020
library(maps) # for mapping
# try mapping state-level winners for the 2020 presidential election
win20 <- winners[winners$year == 2020, ]
win20 <- win20[match(state.fips$fips, win20$state_fips),]
map('state', col = win20$color, fill = TRUE)
legend("bottomright", legend = c("Democrat", "Republican"), fill = c("blue", "red"), cex = 0.7)
title("2020 Presidential Elections Results", line = 1)

4. Elections results for all the years
# initialize a 4 x 3 grid to place maps on
par(mfrow = c(4,3),mar=c(0,0,0,0))
# loop over all years
for (i in seq(min(winners$year),max(winners$year),4)) {
time <- paste("win", i) # create a temporary dataframe for each year
time <- winners[winners$year == i,] # select year as defined by 'i' counter
time <- time[match(state.fips$fips, time$state_fips),] # match fips
map('state', col = time$color, fill = T) # and plot
title(i, line =1)
}
legend("bottom", legend = c("Democrat", "Republican"), fill = c("blue", "red"), cex = 0.7)

Part 2. Interactive Maps with Leaflet
library(leaflet) # advanced mapping
leaflet() %>%
addProviderTiles(providers$OpenStreetMap.Mapnik) %>%
# approx. location to a World Heritage site in my hometown famous for one-horned rhinos and Bengal tigers
setView(lat=27.58626, lng=84.44096, zoom = 10)
Adding shapefiles for Rohingya refugee camps in Cox Bazaar, Bangladesh
# for interactive mapping with shapefiles
library(leaflet)
library(sf)
library(raster)
# import shapefile
coxBazaar <- shapefile("200908_RRC_Outline_Block_AL2/200908_RRC_Outline_Block_AL2.shp")
# transform the shapefile to make it render-ready
coxBazaarT <- spTransform(coxBazaar, CRS("+proj=longlat +datum=WGS84 +no_defs"))
head(coxBazaarT)
## 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
# map the data with OpenStreetMap
leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
setView(lat = 21.18780, lng=92.14871, zoom = 13) %>% # zooms into SE Bangladesh
# to map Rohingya refugee campsites and add interactive labels
addPolygons(data= coxBazaarT, fill = T, stroke = T, weight = 1, highlight=highlightOptions(fillOpacity = 0.2),label = coxBazaarT$Block_Name)
Adding Rainfall data from Iowa Environmental Mesonet
leaflet() %>% addProviderTiles(providers$OpenStreetMap) %>%
setView(lat=38 ,lng= -100,zoom = 4) %>%
addWMSTiles(
"http://mesonet.agron.iastate.edu/cgi-bin/wms/nexrad/n0r.cgi",
layers = "nexrad-n0r-900913",
options = WMSTileOptions(format = "image/png", transparent = T),
attribution = "Weather data © 2012 IEM Nexrad"
)