library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.2
## -- Attaching packages ----------------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.2 v dplyr 1.0.0
## v tidyr 1.1.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## Warning: package 'dplyr' was built under R version 4.0.2
## -- Conflicts -------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Warning: package 'sf' was built under R version 4.0.2
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(shiny)
## Warning: package 'shiny' was built under R version 4.0.2
library(tmaptools)
## Warning: package 'tmaptools' was built under R version 4.0.2
library(leaflet.extras)
## Warning: package 'leaflet.extras' was built under R version 4.0.2
## Loading required package: leaflet
## Warning: package 'leaflet' was built under R version 4.0.2
library(dplyr)
library(rio)
## Warning: package 'rio' was built under R version 4.0.2
setwd("C:/Users/Jerome/Desktop/GIS/GIS")
getwd()
## [1] "C:/Users/Jerome/Desktop/GIS/GIS"
nhdatafile <- "NHD2016.xlsx"
nhdatafilecsv <- "NHD2016.csv"
usshapefile <- "cb_2014_us_county_5m/cb_2014_us_county_5m.shp"
nhfipscode <- "33"
scdatafile <- "SCGOP2016.csv"
scfipscode <- "45"
getwd()
## [1] "C:/Users/Jerome/Desktop/GIS/GIS"
nhdata <- rio::import(nhdatafilecsv)
nhdata <- nhdata[,c("County", "Clinton", "Sanders")]
nhdata$SandersMarginVotes <- nhdata$Sanders - nhdata$Clinton
nhdata$SandersPct <- (nhdata$Sanders) / (nhdata$Sanders + nhdata$Clinton)
# Will use formatting later to multiply by a hundred
nhdata$ClintonPct <- (nhdata$Clinton) / (nhdata$Sanders + nhdata$Clinton)
nhdata$SandersMarginPctgPoints <- nhdata$SandersPct - nhdata$ClintonPct
library(sf)
usgeo <-st_read("cb_2014_us_county_5m/cb_2014_us_county_5m.shp")
## Reading layer `cb_2014_us_county_5m' from data source `C:\Users\Jerome\Desktop\GIS\GIS\cb_2014_us_county_5m\cb_2014_us_county_5m.shp' using driver `ESRI Shapefile'
## Simple feature collection with 3233 features and 9 fields
## geometry type: MULTIPOLYGON
## dimension: XYZ
## bbox: xmin: -179.1473 ymin: -14.55255 xmax: 179.7785 ymax: 71.35256
## z_range: zmin: 0 zmax: 0
## geographic CRS: NAD83
##library(raster)
##library(rgdal)
##library(tmap)
library(tmaptools)
plot(usgeo)
str(usgeo)
## Classes 'sf' and 'data.frame': 3233 obs. of 10 variables:
## $ STATEFP : chr "01" "13" "19" "40" ...
## $ COUNTYFP: chr "059" "111" "109" "115" ...
## $ COUNTYNS: chr "00161555" "00351094" "00465243" "01101845" ...
## $ AFFGEOID: chr "0500000US01059" "0500000US13111" "0500000US19109" "0500000US40115" ...
## $ GEOID : chr "01059" "13111" "19109" "40115" ...
## $ NAME : chr "Franklin" "Fannin" "Kossuth" "Ottawa" ...
## $ LSAD : chr "06" "06" "06" "06" ...
## $ ALAND : num 1.64e+09 1.00e+09 2.52e+09 1.22e+09 2.13e+09 ...
## $ AWATER : num 32904833 13560697 4154722 35708892 22356541 ...
## $ geometry:sfc_MULTIPOLYGON of length 3233; first list element: List of 1
## ..$ :List of 1
## .. ..$ : num [1:9, 1:3] -88.2 -88.2 -88.2 -88.1 -87.5 ...
## ..- attr(*, "class")= chr [1:3] "XYZ" "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 [1:9] "STATEFP" "COUNTYFP" "COUNTYNS" "AFFGEOID" ...
nhgeo <- dplyr::filter(usgeo,STATEFP==nhfipscode)
plot(nhgeo)
str(nhgeo)
## Classes 'sf' and 'data.frame': 10 obs. of 10 variables:
## $ STATEFP : chr "33" "33" "33" "33" ...
## $ COUNTYFP: chr "009" "011" "007" "001" ...
## $ COUNTYNS: chr "00873178" "00873179" "00873177" "00873174" ...
## $ AFFGEOID: chr "0500000US33009" "0500000US33011" "0500000US33007" "0500000US33001" ...
## $ GEOID : chr "33009" "33011" "33007" "33001" ...
## $ NAME : chr "Grafton" "Hillsborough" "Coos" "Belknap" ...
## $ LSAD : chr "06" "06" "06" "06" ...
## $ ALAND : num 4.43e+09 2.27e+09 4.65e+09 1.04e+09 1.80e+09 ...
## $ AWATER : num 1.05e+08 4.16e+07 9.08e+07 1.77e+08 2.60e+08 ...
## $ geometry:sfc_MULTIPOLYGON of length 10; first list element: List of 1
## ..$ :List of 1
## .. ..$ : num [1:327, 1:3] -72.3 -72.3 -72.3 -72.3 -72.3 ...
## ..- attr(*, "class")= chr [1:3] "XYZ" "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 [1:9] "STATEFP" "COUNTYFP" "COUNTYNS" "AFFGEOID" ...
str(nhgeo$NAME)
## chr [1:10] "Grafton" "Hillsborough" "Coos" "Belknap" "Rockingham" ...
str(nhdata$County)
## chr [1:10] "Belknap" "Carroll" "Cheshire" "Coos" "Grafton" "Hillsborough" ...
( ## Order the data
nhgeo <-nhgeo[order(nhgeo$NAME),]
nhdata <-nhdata[order(nhdata$County),]
identical(nhgeo$NAME,nhdata$County)
## [1] TRUE
nhmap <- merge(nhgeo, nhdata, by.x = "NAME", by.y = "County")
str(nhmap)
## Classes 'sf' and 'data.frame': 10 obs. of 16 variables:
## $ NAME : chr "Belknap" "Carroll" "Cheshire" "Coos" ...
## $ STATEFP : chr "33" "33" "33" "33" ...
## $ COUNTYFP : chr "001" "003" "005" "007" ...
## $ COUNTYNS : chr "00873174" "00873175" "00873176" "00873177" ...
## $ AFFGEOID : chr "0500000US33001" "0500000US33003" "0500000US33005" "0500000US33007" ...
## $ GEOID : chr "33001" "33003" "33005" "33007" ...
## $ LSAD : chr "06" "06" "06" "06" ...
## $ ALAND : num 1.04e+09 2.41e+09 1.83e+09 4.65e+09 4.43e+09 ...
## $ AWATER : num 1.77e+08 1.59e+08 5.80e+07 9.08e+07 1.05e+08 ...
## $ Clinton : int 3495 3230 5132 2013 6918 28147 12250 22829 8813 2497
## $ Sanders : int 6005 5638 12441 3639 14245 39245 18107 31065 15881 5915
## $ SandersMarginVotes : int 2510 2408 7309 1626 7327 11098 5857 8236 7068 3418
## $ SandersPct : num 0.632 0.636 0.708 0.644 0.673 ...
## $ ClintonPct : num 0.368 0.364 0.292 0.356 0.327 ...
## $ SandersMarginPctgPoints: num 0.264 0.272 0.416 0.288 0.346 ...
## $ geometry :sfc_MULTIPOLYGON of length 10; first list element: List of 1
## ..$ :List of 1
## .. ..$ : num [1:33, 1:3] -71.7 -71.7 -71.7 -71.7 -71.7 ...
## ..- attr(*, "class")= chr [1:3] "XYZ" "MULTIPOLYGON" "sfg"
## - attr(*, "sf_column")= chr "geometry"
## - attr(*, "agr")= Factor w/ 3 levels "constant","aggregate",..: NA NA NA NA NA NA NA NA NA NA ...
## ..- attr(*, "names")= chr [1:15] "NAME" "STATEFP" "COUNTYFP" "COUNTYNS" ...
##Start Plotting
plot(nhmap, fill = "SandersMarginVotes")
## Warning: plotting the first 10 out of 15 attributes; use max.plot = 15 to plot
## all
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
## Warning in title(...): "fill" is not a graphical parameter
plot(nhmap, fill = "SandersMarginPctgPoints")
## Warning: plotting the first 10 out of 15 attributes; use max.plot = 15 to plot
## all
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
## Warning: "fill" is not a graphical parameter
##qtm(nhmap, fill = "SandersMarginVotes")
##qtm(nhmap, fill = "SandersMarginPctgPoints")