Skip to content

Instantly share code, notes, and snippets.

@alandipert alandipert/leafletbm.R
Last active May 14, 2019

Embed
What would you like to do?
# 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
You can’t perform that action at this time.