Skip to content

Instantly share code, notes, and snippets.

@kylebutts
Last active November 10, 2020 23:16
Show Gist options
  • Save kylebutts/d6fe595e52d6a7a1a5fa8bd850295555 to your computer and use it in GitHub Desktop.
Save kylebutts/d6fe595e52d6a7a1a5fa8bd850295555 to your computer and use it in GitHub Desktop.
library(tidyverse)
library(sf)
library(tigris)
library(rmapshaper)
source("https://gist.githubusercontent.com/kylebutts/7dc66a01ec7e499faa90b4f1fd46ef9f/raw/15196997e5aad41696b03c49be3bfaaca132fdf2/theme_kyle.R")
counties <- tigris::counties(class = "sf") %>%
# Continental
filter(!(STATEFP %in% c("02", "15", "60", "66", "69", "72", "78"))) %>%
# Simplifies details to make plotting quicker
rmapshaper::ms_simplify(keep = 0.01) %>%
# Use a good projection
st_transform(st_crs(2163)) %>%
# Random variable for plotting
mutate(var = runif(n()))
states <- counties %>% group_by(STATEFP) %>% summarize()
us <- counties %>% summarize()
ggplot() +
geom_sf(data = counties, aes(fill = var),
color = "grey", size = 0.2) +
geom_sf(data = us, color = "black", fill = NA) +
# geom_sf(data = states, color = "black", fill = NA) +
# No lat-long
coord_sf(datum = NA)+
theme_kyle() +
# Continuous Scale
scale_fill_distiller(palette = "RdBu")
# Discrete Scale
# scale_fill_brewer()
@kylebutts
Copy link
Author

Example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment