Creating Maps in R - week 9 HW - 20191030
#install packages and libraries
#install.packages("rio")
#install.packages("tmap")
#install.packages("tmaptools")
#install.packages("sf")
#install.packages("leaflet")
#install.packages("leaflet.extras")
#install.packages("shiny")
#install.packages("urltools")
#install.packages("scales")
#install.packages("htmlwidgets")
#install.packages("sf")
#install.packages("dplyr")
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tmap)
## Warning: package 'tmap' was built under R version 3.5.3
library(tmaptools)
## Warning: package 'tmaptools' was built under R version 3.5.3
library(sf)
## Warning: package 'sf' was built under R version 3.5.3
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.5.3
library(leaflet.extras)
## Warning: package 'leaflet.extras' was built under R version 3.5.3
library(rio)
## Warning: package 'rio' was built under R version 3.5.3
library(shiny)
## Warning: package 'shiny' was built under R version 3.5.3
library(urltools)
## Warning: package 'urltools' was built under R version 3.5.3
library(scales)
## Warning: package 'scales' was built under R version 3.5.3
library(htmlwidgets)
## Warning: package 'htmlwidgets' was built under R version 3.5.3
getwd()
## [1] "C:/Users/Jennifer/Documents/MC Data Science/Data Science 110 Writing and Comm/R Data Files and Markdown files"
Step 1: Get election results data
nhdatafile <- "NHD2016.xlsx"
nhdatafilecsv <- "NHD2016.csv"
nhfipscode <- "33"
scdatafile <- "SCGOP2016.csv"
SCfipscode <- "45"
#now read in the NH election results file
nhdata <- rio::import(nhdatafilecsv)
#select only columns for county, Clinton, and Sanders
nhdata <- nhdata[,c("County", "Clinton", "Sanders")]
Step 2: Decide what data to map
Add columns to pull the candidates’ margins of victory(or loss) and percent of the vote.
nhdata$SandersMarginVotes <- nhdata$Sanders - nhdata$Clinton
nhdata$SandersPct <- (nhdata$Sanders)/(nhdata$Sanders + nhdata$Clinton)
nhdata$ClintonPct <- (nhdata$Clinton)/(nhdata$Sanders + nhdata$Clinton)
nhdata$SandersMarginPctgPoints <- nhdata$SandersPct - nhdata$ClintonPct
Step 3: Get your geographic data
Store the name of the file in a variable called usshapefile
library(tmaptools)
usshapefile <- "cb_2014_us_county_5m.shp"
usgeo <- read_shape(file = usshapefile, as.sf = TRUE)
## Warning: This function is deprecated and has been migrated to github.com/
## mtennekes/oldtmaptools
## Warning in readOGR(dir, base, verbose = FALSE, ...): Z-dimension discarded
Step 3.5: Plot the shapefile.
library(tmap)
qtm(usgeo)

Step 3.6: Look at the structure.
str(usgeo)
## Classes 'sf' and 'data.frame': 3233 obs. of 10 variables:
## $ STATEFP : Factor w/ 56 levels "01","02","04",..: 1 11 16 37 39 37 28 26 29 10 ...
## $ COUNTYFP: Factor w/ 328 levels "001","003","005",..: 42 76 74 78 78 38 22 137 291 35 ...
## $ COUNTYNS: Factor w/ 3233 levels "00023901","00025441",..: 120 430 738 1911 2024 1880 1399 1373 1490 298 ...
## $ AFFGEOID: Factor w/ 3233 levels "0500000US01001",..: 30 442 844 2189 2302 2158 1669 1589 1764 344 ...
## $ GEOID : Factor w/ 3233 levels "01001","01003",..: 30 442 844 2189 2302 2158 1669 1589 1764 344 ...
## $ NAME : Factor w/ 1921 levels "Abbeville","Acadia",..: 620 592 945 1291 1665 692 320 1683 284 747 ...
## $ LSAD : Factor w/ 11 levels "00","03","04",..: 5 5 5 5 5 5 5 5 1 5 ...
## $ ALAND : Factor w/ 3233 levels "1000508842","1001064387",..: 1199 5 2047 452 1721 2091 1880 1194 2397 1215 ...
## $ AWATER : Factor w/ 3233 levels "0","10017640",..: 1626 414 1940 1718 1118 2724 2916 2228 1613 497 ...
## $ geometry:sfc_MULTIPOLYGON of length 3233; first list element: List of 1
## ..$ :List of 1
## .. ..$ : num [1:9, 1:2] -88.2 -88.2 -88.2 -88.1 -87.5 ...
## ..- attr(*, "class")= chr "XY" "MULTIPOLYGON" "sfg"
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA
## ..- attr(*, "names")= chr "STATEFP" "COUNTYFP" "COUNTYNS" "AFFGEOID" ...
Step 3.7: Filter to pull NH. Then do the quick thematic map again to check we have the right one.
nhgeo <- filter(usgeo, STATEFP=="33")
qtm(nhgeo)

Step 4: Merge spatial and results data. Check the vectors to see if they will match.
Step 4.1: check the NAME in nhgeo dataframe.
str(nhgeo$NAME)
## Factor w/ 1921 levels "Abbeville","Acadia",..: 684 791 416 138 1470 334 1653 1131 282 1657
Step 4.2: Check the County in the nhdata dataframe.
str(nhdata$County)
## chr [1:10] "Belknap" "Carroll" "Cheshire" "Coos" "Grafton" "Hillsborough" ...
Step 4.3: change the geospatial factors to character strings.
nhgeo$NAME <- as.character(nhgeo$NAME)
Step 4.4: Sort both datasets by county name and the compare them.
nhgeo <- nhgeo[order(nhgeo$NAME),]
nhdata <- nhdata[order(nhdata$County),]
identical(nhgeo$NAME,nhdata$County)
## [1] TRUE
Step 5: Create a static map. Use Sanders’ margins by county in number of votes.
qtm(nhmap, "SandersMarginVotes")
## Some legend labels were too wide. These labels have been resized to 0.62, 0.62, 0.62, 0.57, 0.53. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

Step 5.1: Mapping margins by percentage.
qtm(nhmap, "SandersMarginPctgPoints")

Step 5.2: Change the colors, borders, and other aesthetics.
tm_shape(nhmap) +
tm_fill("SandersMarginVotes", title="Sanders Margin, Total Votes", palette = "PRGn") +
tm_borders(alpha=.5) +
tm_text("NAME", size=0.8)
## Some legend labels were too wide. These labels have been resized to 0.62, 0.62, 0.62, 0.57, 0.53. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

Step 5.3: Change the map theme.
tm_shape(nhmap) +
tm_fill("SandersMarginVotes", title="Sanders Margin, Total Votes", palette = "PRGn") +
tm_borders(alpha=.5) +
tm_text("NAME", size=0.8)
## Some legend labels were too wide. These labels have been resized to 0.62, 0.62, 0.62, 0.57, 0.53. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

tm_style_classic()
## Warning in tm_style_classic(): tm_style_classic is deprecated as of tmap version
## 2.0. Please use tm_style("classic", ...) instead
## $tm_layout
## $tm_layout$style
## [1] "classic"
##
##
## attr(,"class")
## [1] "tm"
Step 5.4: Save the static map.
nhstaticmap <- tm_shape(nhmap) +
tm_fill("SandersMarginVotes", title="Sanders Margin, Total Votes", palette = "PRGn") +
tm_borders(alpha=.5) +
tm_text("NAME", size=0.8)
save_tmap(nhstaticmap, filename="nhdemprimary.jpg")
## Warning in save_tmap(nhstaticmap, filename = "nhdemprimary.jpg"): save_tmap is
## deprecated as of tmap version 2.0. Please use tmap_save instead
## Warning in png(tmp, width = width, height = height, res = res): 'width=7,
## height=7' are unlikely values in pixels
## Map saved to C:\Users\Jennifer\Documents\MC Data Science\Data Science 110 Writing and Comm\R Data Files and Markdown files\nhdemprimary.jpg
## Resolution: 1501.336 by 2937.385 pixels
## Size: 5.004452 by 9.791282 inches (300 dpi)