Part 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:
Load data and get single map for 1976
3. Try to make a map just for one year:
library(dplyr)
library(maps)
hw7<-read.csv("1976-2020-president.csv")
head(hw7)
## year state state_po state_fips state_cen state_ic office
## 1 1976 ALABAMA AL 1 63 41 US PRESIDENT
## 2 1976 ALABAMA AL 1 63 41 US PRESIDENT
## 3 1976 ALABAMA AL 1 63 41 US PRESIDENT
## 4 1976 ALABAMA AL 1 63 41 US PRESIDENT
## 5 1976 ALABAMA AL 1 63 41 US PRESIDENT
## 6 1976 ALABAMA AL 1 63 41 US PRESIDENT
## candidate party_detailed writein candidatevotes
## 1 CARTER, JIMMY DEMOCRAT FALSE 659170
## 2 FORD, GERALD REPUBLICAN FALSE 504070
## 3 MADDOX, LESTER AMERICAN INDEPENDENT PARTY FALSE 9198
## 4 BUBAR, BENJAMIN ""BEN"" PROHIBITION FALSE 6669
## 5 HALL, GUS COMMUNIST PARTY USE FALSE 1954
## 6 MACBRIDE, ROGER LIBERTARIAN FALSE 1481
## totalvotes version notes party_simplified
## 1 1182850 20210113 NA DEMOCRAT
## 2 1182850 20210113 NA REPUBLICAN
## 3 1182850 20210113 NA OTHER
## 4 1182850 20210113 NA OTHER
## 5 1182850 20210113 NA OTHER
## 6 1182850 20210113 NA LIBERTARIAN
hw7 <- hw7 %>% group_by(state, year) %>% top_n(1, candidatevotes)
hw7 <- mutate(hw7, partyColor=recode(as.character(party_simplified),
"DEMOCRAT"="blue", "REPUBLICAN"="red",
.default="white"))
hw71976 <- hw7[hw7$year=="1976", c("state_fips", "partyColor")]
hw71976 <- hw71976[match(paste(state.fips$fips), paste(hw71976$state_fips),
paste(hw71976$partyColor)),]
head(hw71976)
## # A tibble: 6 x 2
## state_fips partyColor
## <int> <chr>
## 1 1 blue
## 2 4 red
## 3 5 blue
## 4 6 red
## 5 8 red
## 6 9 red
usMap <- map("state", col = hw71976$partyColor, fill = TRUE)
mtext("Election Results 1976")

4. Now loop that code and map it over time:
years= seq(1976,2020,4)
par(mfrow=c(5,3), mar=c(0,0,0,0), bg = 'white')
for(i in years) {
hwAll <- hw7[hw7$year==i, c("state_fips", "state", "party_simplified", "partyColor")]
hwAll <- hwAll[match(paste(state.fips$fips), paste(hwAll$state_fips),
paste(hwAll$partyColor)),]
map("state", col=hwAll$partyColor, fill=TRUE, main= "Presidential election results by state from 1976 to 2020")
mtext(i,side=3,line=1)
}
plot(0, axes=F, xlab="",ylab="", type="n", cex=1.2)
title("Presidential election results \nby State and year",
adj = 0.5, line = 0.1)
legend("right", legend=c("Republican","Democratic", "Other"),
col=c("red","blue","white"), pch=c(15, 15, 15), pt.cex = 1.5,
horiz=F, cex=1, border=T, box.lty=1, ncol = 1,
pt.bg = adjustcolor(c('blue', 'red', 'yellow')))

Part 2: Interactive Maps with Leaflet.
#install.packages("leaflet", repos="http://cran.us.r-project.org")
library(leaflet)
myMap <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap)
myMap %>%
setView(lat=33.08, lng=-83.29, zoom = 10)
2. Add your own shapefiles
#install.packages("rgdal", repos="http://cran.us.r-project.org")
library(sf)
library(raster)
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)
## 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
#Rohingya Camp
myMapR <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
setView(lng = 92.14871, lat = 21.18780, zoom = 12) %>%
addPolygons(data = campShapeFile, fill=TRUE, stroke = T, weight = 1,
highlight = highlightOptions(fillOpacity = 0.7),
label = campShapeFile$Block_No)
myMapR
3. Add tiles from the web
#USA
myMapUSA <- leaflet() %>%
addProviderTiles(providers$OpenStreetMap) %>%
setView(lng = -92.14871, lat = 32.18780, zoom = 7) %>%
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"
)
myMapUSA