Reference and disclaimer: This homework is attempting to reproduce figures,using leaflet and map packages.
Attempt to reproduce figures using R code and R markdown syntax exclusively:
The first chart is the figure on Presidential election results by State in 2000.The Dataverse on this figure is maintained by the MIT Election Data and Science Lab (MEDSL).
data <- read.table("C:/Users/Ag User/Desktop/AAEC/5. 2020 Spring/AAEC 8610/Homework/HW5/data.csv", header = TRUE, sep = ",")
data <- data.frame(data)
data <- data %>% group_by(year, state)
data <- data %>% filter(candidatevotes == max(candidatevotes))
my_data <- data %>% select(year, state, state_fips, party, candidate)
my_data <- my_data %>%
mutate(color = case_when(
party == "republican" ~ 1,
party == "democrat" ~ 7,
TRUE ~ 3
))
my_data2000 <- filter(my_data, year == 2000)
my_data2000 <- my_data2000[match(paste(state.fips$fips),paste(my_data2000$state_fips)),]
colpal <- brewer.pal(7, "RdBu")
my_data2000$partycol <- colpal[my_data2000$color]
# 2000 map
par(mfrow=c(1,1), mar=c(0,0,0,0))
map("state", col=my_data2000$partycol, fill=TRUE)
title("Presidential election results by State in 2000",
adj = 0.5, line = 0.2)
The second chart shows the Presidential election results by State and year.
years <- c(1976, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008, 2012, 2016)
par(mfrow=c(4,3), mar=c(0,0,0,0), bg = 'gray95')
for (i in years) {
my_data_m <- my_data[my_data$year==i, ]
my_data_m <- my_data_m[match(paste(state.fips$fips),paste(my_data_m$state_fips)),]
my_data_m$partycol <- colpal[my_data_m$color]
map("state", col=my_data_m$partycol, fill=T)
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("bottom", legend=c("Republican","Democratic", "Other"),
col=c("black","black","black"), pch=c(22, 22, 22), pt.cex=2,
horiz=F, cex=1.2, border=T, box.lty=1, ncol=1,
title=NULL,
pt.bg=adjustcolor(c('blue', 'red', 'beige')))
Chart Three is the figure for Rohingya refugee camps. You can download the data on the website for HDX (The Humanitarian Data Exchange).
addPolygons(map=myMap, data=campShapeFile, fill=TRUE, stroke=T, weight=1,
highlight = highlightOptions(fillOpacity = 0.7),
label = campShapeFile$Block_No)
Chart Four is the figure for a raster image of rainfall in US. The data source is from Iowa State University Iowa Environmental Mesonet.
myMap %>% setView(lat=38, lng=-96, 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")