library(dplyr)
library(maps)
library(leaflet)
library(sf)
library(rgdal)
library(terra)
library(raster)
library(rgeos)

Part1. Map presidential elections results with the maps package

  1. Download the elections file from harvard database
  2. Load the data, and clean it up
data<- read.csv("1976-2020-president.csv")
# Add color variable
data$color <- ifelse(data$party_detailed == "DEMOCRAT", "blue",
                        ifelse(data$party_detailed == "REPUBLICAN", "red", "white"))

sub <- data %>% group_by(state, year) %>% slice_max(candidatevotes) 
  1. Try to make a map just for one year
US <- sub[sub$year == 2020,]

# map
map("state", col = US$color, fill = TRUE)
title("Presidential election results by state in 2020")

  1. Now loop that code and map it over time
y4 = seq(1976, 2020, by=4)
par(mfrow=c(4,3), mar=c(0,0,0,0))
for (i in y4) {
  loop <- sub[sub$year == i, c("state","party_detailed", "color")]
  map("state", col = loop$color, fill = TRUE)
  title(i, line =1)
  }
legend("right", legend = c("Democrat", "Republican","Other"), fill = c("blue", "red","white"), cex = 0.3)

Part 2: Interactive Maps with Leaflet

  1. Get familiar with the package
myMap <- leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  setView(lat=33.947474, lng=-83.373671, zoom = 12)
myMap
myMap <- leaflet() %>%
  addProviderTiles(providers$OpenTopoMap) %>%
  setView(lat=33.947474, lng=-83.373671, zoom = 12)
myMap
myMap <- leaflet() %>%
  addProviderTiles(providers$Esri.NatGeoWorldMap) %>%
  setView(lat=33.947474, lng=-83.373671, zoom = 12)
myMap
havard <- leaflet() %>%
  addProviderTiles(providers$OpenStreetMap) %>%
  setView(lat=42.374443, lng=-71.116943, zoom = 12)
havard
  1. Add your own shapefiles
sp <- shapefile("C:/Users/ho643/OneDrive - University of Georgia/UGA/2023 Spring/AAEC8610 Adv Quant Meth Econ/HW/HW8/220130_rrc_outline_camp_al1/220130_RRC_Outline_camp_AL1/T220130_RRC_Outline_Camp_AL1.shp")

# Projection is necessary for R to place the coordinates correctly
campShapeFile <- spTransform(sp, CRS("+proj=longlat +datum=WGS84 +no_defs"))
head(campShapeFile)
##   OBJECTID    District Upazila      Settlement        Union         Name_Alias
## 1        1 Cox's Bazar   Ukhia Collective site Palong Khali Bagghona-Putibonia
## 2        2 Cox's Bazar   Ukhia Collective site Palong Khali               <NA>
## 3        3 Cox's Bazar   Ukhia Collective site Palong Khali    Jamtoli-Baggona
## 4        4 Cox's Bazar   Ukhia Collective site  Raja Palong      Kutupalong RC
## 5        5 Cox's Bazar   Ukhia Collective site Palong Khali               <NA>
## 6        6 Cox's Bazar   Ukhia Collective site Palong Khali               <NA>
##      SSID SMSD__Cnam            NPM_Name Area_Acres PeriMe_Met     Camp_Name
## 1 CXB-224    Camp 16 Camp 16 (Potibonia)  130.57004   4136.730       Camp 16
## 2 CXB-203   Camp 02E            Camp 02E   96.58179   4803.162       Camp 2E
## 3 CXB-223    Camp 15   Camp 15 (Jamtoli)  243.25346   4722.267       Camp 15
## 4 CXB-221   Camp KRC       Kutupalong RC   95.70851   3094.872 Kutupalong RC
## 5 CXB-213    Camp 09             Camp 09  160.39225   4116.455        Camp 9
## 6 CXB-214    Camp 10             Camp 10  122.60020   3732.526       Camp 10
##           Area_SqM         Latitude        Longitude Shape_Leng   Shape_Area
## 1  528946.95881724 21.1563813298438 92.1490685817901 0.03853094 4.597769e-05
## 2 391267.799744003 21.2078084302778 92.1643360947381 0.04481152 3.402118e-05
## 3 985424.393160958 21.1606399787906 92.1428956454661 0.04423700 8.565911e-05
## 4 387729.666427279 21.2120281895357 92.1638095873048 0.02878576 3.371450e-05
## 5 649769.878002447 21.1899911992394 92.1603127317644 0.03853989 5.649182e-05
## 6 496664.566973549 21.1897434585344 92.1542587569213 0.03475873 4.318088e-05
rohingya <- 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)
rohingya

Explore Different Options

rohingya <- leaflet() %>%
  addProviderTiles(providers$OpenTopoMap) %>%
  setView(92.14871, 21.18780, zoom = 12) %>%
  addPolygons(data=campShapeFile, fill=TRUE, stroke=T, weight=3, # thicker border line by changing weight
            highlight = highlightOptions(fillOpacity = 0.5), # transparency of area 
            label = campShapeFile$Block_No)
rohingya
USA <- map("state", fill = TRUE, plot=FALSE)
  leaflet() %>%
  addPolygons(data=USA, fill=TRUE, stroke=T, weight=1) %>%
  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"
  )