我的目标是用传单创建一个交互式地图,该地图将显示美国各州的population
。然后,当用户单击某个州时,地图将向下钻取到该州的县,显示每个县的population
。这是example。在县数据集中,有州和县数据可用于创建此类地图。我想在州一级,应该首先按州汇总人口。但是我不知道这种向下钻取是否可行。您的反馈非常有价值。
library(tidyverse)
library(sf)
library(leaflet)
library(noncensus)
library(tigris)
options(tigris_use_cache = TRUE)
data(counties)
counties <- counties %>%
mutate(GEOID = paste0(state_fips, county_fips))
counties_sf <- tigris::counties() %>%
as("sf") %>%
left_join(counties, by = "GEOID")
pal <- colorQuantile("YlOrRd", domain = counties_sf$population)
m <- leaflet(counties_sf) %>%
setView(-96, 37.8, 4) %>%
addTiles()
m <- m %>% addPolygons(
fillColor = ~pal(population),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7)
m