Skip to content

Instantly share code, notes, and snippets.

@alandipert
Last active May 14, 2019 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alandipert/931e19069987d93c0a4c7ac83762fcb2 to your computer and use it in GitHub Desktop.
Save alandipert/931e19069987d93c0a4c7ac83762fcb2 to your computer and use it in GitHub Desktop.
# https://gist.github.com/keberwein/05f633a2be293b01b52bf05553d24b93
# http://eric.clst.org/tech/usgeojson/
# https://rstudio.github.io/leaflet/choropleths.html
library(dplyr)
library(leaflet)
library(maptools)
library(rgdal)
library(rgeos)
library(scales)
library(sf)
library(tidycensus)
#library(sp) # Trying to use SF instead
counties_zip <- "https://www2.census.gov/geo/tiger/TIGER2018/COUNTY/tl_2018_us_county.zip"
if (!file.exists("counties")) {
download.file(counties_zip, "counties.zip")
unzip("counties.zip", exdir = "counties")
}
counties <- st_read("counties/tl_2018_us_county.shp")
# Alaska(2), Hawaii(15), Puerto Rico (72), Guam (66), Virgin Islands (78), American Samoa (60)
# Mariana Islands (69), Micronesia (64), Marshall Islands (68), Palau (70), Minor Islands (74)
extra_continental_statefp <- c("02", "15", "72", "66", "78", "60", "69", "64", "68", "70", "74")
# Outlying islands
outlying_islands <- c("81", "84", "86", "87", "89", "71", "76", "95", "79")
counties <- counties %>% filter(!STATEFP %in% extra_continental_statefp)
counties <- counties %>% filter(!STATEFP %in% outlying_islands)
tidycensus::census_api_key("replace with your key")
crs <- leafletCRS(proj4def = '+proj=longlat +datum=WGS84')
profvis::profvis({
leaflet(head(counties, 500), options = leafletOptions(crs = crs)) %>%
addPolygons() %>%
addTiles()
})
#us.map <- us.map[!us.map$STATEFP %in% ,]
#states <- geojsonio::geojson_read("json/us-states.geojson", what = "sf")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment