Working with Maps

1. Map presidential elections results with the maps package

1. Download the elections file from harvard database

2. Load the data, and clean it up

my_data_HW7 <- read.csv("1976_2020_president.csv") 
my_data_HW7 <-mutate(my_data_HW7,
                     color = ifelse(party_detailed == "DEMOCRAT", "navyblue", 
                                    ifelse(party_detailed == "REPUBLICAN","red3",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)
temp <- my_data_HW7_2020[my_data_HW7_2020$year == 2020, c("state_fips","color","year")]
temp <- temp[match(paste(state.fips$fips),paste(temp$state_fips)),] #Checking if the map matches the data
Q3<-maps::map("state", col=temp$color, fill=TRUE)
legend("bottomright", legend=c("democrat", "republican"), fill=c("navyblue", "red3"))
mtext("Election Results - 2020", outer=TRUE,  cex=2, line=-3)

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_year<-filter(my_data_HW7_win, year == year_ind)
  temp <- my_data_HW7_year[my_data_HW7_year$year == year_ind, c("state_fips","color","year")]
  temp <- temp[match(paste(state.fips$fips),paste(temp$state_fips)),] #Checking if the map matches the data
  maps::map("state", col=temp$color, fill=TRUE)
  mtext(year_ind,side=3,line=1, cex=1.4)
}

# add title for the page
legend("bottomright", legend=c("democrat", "republican"), fill=c("navyblue", "red3"), cex=0.9)
mtext("Election Results by year", outer=TRUE,  cex=2, line=-2)

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("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)
FALSE   Block_Let Camp_SSID Block_Name   Block_SSID SMSD_Cname       Camp_Alias
FALSE 0         I   CXB-232     C04X_I CXB-232_I163   Camp 04X Camp 4 Extension
FALSE 1         B   CXB-232     C04X_B CXB-232_B165   Camp 04X Camp 4 Extension
FALSE 2         F   CXB-232     C04X_F CXB-232_F161   Camp 04X Camp 4 Extension
FALSE 3         C   CXB-232     C04X_C CXB-232_C166   Camp 04X Camp 4 Extension
FALSE 4         E   CXB-232     C04X_E CXB-232_E160   Camp 04X Camp 4 Extension
FALSE 5         H   CXB-232     C04X_H CXB-232_H162   Camp 04X Camp 4 Extension
FALSE           NPM_Cname Area_Acres         CampName         Area_SqM
FALSE 0 Camp 04 Extension  17.597196 Camp 4 Extension 71213.3263972732
FALSE 1 Camp 04 Extension  19.816614 Camp 4 Extension 80194.9934341609
FALSE 2 Camp 04 Extension   8.901736 Camp 4 Extension 36024.0480281611
FALSE 3 Camp 04 Extension  40.230092 Camp 4 Extension  162805.40781136
FALSE 4 Camp 04 Extension  17.447146 Camp 4 Extension 70606.0954539348
FALSE 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