ggplot2 and Simple Features

On Monday we spent a lot of time getting the development version of ggplot2 to work so that we can use it’s ‘bleeding-edge’ functionality to represent ‘simple features’ via the sf package. We ended up producing just one visual – replicating the one found here: www.nytimes.com/elections/results/north-carolina-governor-mccrory-cooper. Consider the attached code Code-Day21.R.



SF Map Code

# library(devtools)
# install_github("tidyverse/ggplot2")

library(ggplot2)
library(ggthemes)
library(sf)

nc <- st_read(
    system.file("shape/nc.shp", package = "sf"), 
    quiet = TRUE
)

ggplot(data = nc) +
    geom_sf()

cooper <- read.csv("http://unca-pols.org/Files/Data/CooperVote.csv")

names(nc)
names(cooper)

unique(nc$NAME)
unique(cooper$County)

cooper$NAME <- cooper$County

dat <- merge(nc, cooper, by = "NAME")

ggplot(data = dat) +
    geom_sf(aes(fill = Winner))

ggplot(data = dat) +
    geom_sf(aes(fill = Cooper.Vote.Share)) +
    scale_fill_gradient2(name = "Cooper Vote Share", 
                          low = "#dc322f", 
                          mid = "white",
                         high = "#268bd2",
                     midpoint = 50) +
    theme_solarized_2()