already installed (from Steve’s tutorial)

library(dplyr)
## 
## 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)
library(tmaptools)
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(leaflet)
library(leaflet.extras)

Set my working directory

setwd("C:/Users/Don A/Documents/Don's files/MC")
library(rio)

data file names near the top

nhdatafile <- "NHD2016.xlsx"
nhdata <- rio::import(nhdatafile)

ignoring minor candidates, just select county, and clinton and sanders columns

nhdata <- nhdata[,c("County", "Clinton", "Sanders")]

add columns for candidates’ margins of victory (or loss) and percent of the vote

# Add columns for percents and margins
nhdata$SandersMarginVotes <- nhdata$Sanders - nhdata$Clinton
nhdata$SandersPct <- (nhdata$Sanders - nhdata$Clinton) / (nhdata$Sanders + nhdata$Clinton) # Will use formatting later to multiply by a hundred 
nhdata$ClintonPct <- (nhdata$Clinton - nhdata$Sanders) / (nhdata$Sanders + nhdata$Clinton)
nhdata$SandersMarginPctgPoints <- nhdata$SandersPct - nhdata$ClintonPct

moved the zip subdirectory

usshapefile <- "cb_2014_us_county_5m.shp"

Moved the zipped file and deleted the first half of the file name. Not sure if that worked, but I’m moving on

trying read_shape with the tmaptools command because it didn’t work without it

library(tmaptools)
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

Steve said to ignore that, it was just a warning

testing the map

qtm(usgeo)

Always shocked when anything works. Moving on. Just hit save because I don’t want to lose this.

Now trying str(usgeo) to see the usgeo data 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" ...

Extracting data for NH only - 33

nhgeo <- filter(usgeo, STATEFP=="33")

I think I’m supposed to sking the FIPS code step. Now to see if nhgeo looks correct

qtm(nhgeo)

Not really understanding this, but testing:

str(nhgeo$NAME)
##  Factor w/ 1921 levels "Abbeville","Acadia",..: 684 791 416 138 1470 334 1653 1131 282 1657
str(nhdata$County)
##  chr [1:10] "Belknap" "Carroll" "Cheshire" "Coos" "Grafton" ...

Change counties from R factors to character strings

nhgeo$NAME <- as.character(nhgeo$NAME)

Next, it can be helpful to sort both data sets by county name and then compare

nhgeo <- nhgeo[order(nhgeo$NAME),]

and

nhdata <- nhdata[order(nhdata$County),]

check to see if two county columns are identical

identical(nhgeo$NAME,nhdata$County)
## [1] TRUE

That worked. Now join the two files by using tmaptool’s append_data() because of its intuitive syntax and allowing names of the two joined columns to be different

nhmap <- append_data(nhgeo, nhdata, key.shp = "NAME", key.data="County")
## Warning: This function is deprecated and has been migrated to github.com/
## mtennekes/oldtmaptools
## Keys match perfectly.

maybe that worked. Now try to see new data structure with:

str(nhmap)
## Classes 'sf' and 'data.frame':   10 obs. of  16 variables:
##  $ STATEFP                : Factor w/ 56 levels "01","02","04",..: 30 30 30 30 30 30 30 30 30 30
##  $ COUNTYFP               : Factor w/ 328 levels "001","003","005",..: 1 2 3 5 6 8 10 12 14 15
##  $ COUNTYNS               : Factor w/ 3233 levels "00023901","00025441",..: 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505
##  $ AFFGEOID               : Factor w/ 3233 levels "0500000US01001",..: 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
##  $ GEOID                  : Factor w/ 3233 levels "01001","01003",..: 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774
##  $ NAME                   : chr  "Belknap" "Carroll" "Cheshire" "Coos" ...
##  $ LSAD                   : Factor w/ 11 levels "00","03","04",..: 5 5 5 5 5 5 5 5 5 5
##  $ ALAND                  : Factor w/ 3233 levels "1000508842","1001064387",..: 67 1991 1424 2526 2494 1831 1997 1382 3172 730
##  $ AWATER                 : Factor w/ 3233 levels "0","10017640",..: 778 638 2460 3097 55 1942 2454 1327 1852 1817
##  $ Clinton                : num  3495 3230 5132 2013 6918 ...
##  $ Sanders                : num  6005 5638 12441 3639 14245 ...
##  $ SandersMarginVotes     : num  2510 2408 7309 1626 7327 ...
##  $ SandersPct             : num  0.264 0.272 0.416 0.288 0.346 ...
##  $ ClintonPct             : num  -0.264 -0.272 -0.416 -0.288 -0.346 ...
##  $ SandersMarginPctgPoints: num  0.528 0.543 0.832 0.575 0.692 ...
##  $ geometry               :sfc_POLYGON of length 10; first list element: List of 1
##   ..$ : num [1:33, 1:2] -71.7 -71.7 -71.7 -71.7 -71.7 ...
##   ..- attr(*, "class")= chr  "XY" "POLYGON" "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  "STATEFP" "COUNTYFP" "COUNTYNS" "AFFGEOID" ...

I’m assuming that worked. That was the hard part: finding data, getting it into the right format and merging it with geospatial data. Now, create a simple static map of Sanders’s 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.

and mapping margins by percentage

qtm(nhmap, "SandersMarginPctgPoints")

not sure how to get the maps to print side by side, but moving on. For more control over the map’s colors, borders, etc., use the tmshape() function, which uses a ggplot2-like syntax to set fill, border and other attributes.

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.

Not sure about the too wide legend labels message. Moving on. Using built-in tmap themes (first try didn’t work, trying again):

tm_shape(nhmap) +
  tm_fill("SandersMarginVotes", title="Sanders Margin, Total Votes", palette = "PRGn") +
  tm_borders(alpha=.5) +
  tm_text("NAME", size=0.8) +
  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
## 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.

You can save static maps created by tmap by using the save_tmap() function (which makes me wonder what happens to the ones I don’t save). I tried this a couple of times. It appears it wants tmap_save, not save_tmap. However I’m not sure what to do about the width=7, height=7 message below. I hope it doesn’t mess up steps 6 throught 10, which I’m going to attempt.

Please note: I couldn’t get the following chunk to work, so I got rid of the chunk.

#nhstaticmap <- tm_shape(nhmap) + tm_fill(“SandersMarginVotes”, title=“Sanders Margin, Total Votes”, palette = “PRGn”) + tm_borders(alpha=.5) + tm_text(“NAME”, size = 0.8) + tmap_save(nhstaticmap, filename=“nhdemprimary.jpg”)

The above step wasn’t working, and I’m not sure why, so I got rid of the chunk and put a hashtag in front of the code so hopefully I can post what I have to Rpubs.

Please note: I started the extra credit portion, but didn’t have luck. Maybe I was supposed to re-enter a lot of the Sanders code for Clinton. Or maybe it’s because I couldn’t get the above section to work.