-
Notifications
You must be signed in to change notification settings - Fork 5
/
leaflet.R
53 lines (45 loc) · 1.32 KB
/
leaflet.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
library(tigris)
library(tidyverse)
cty <- counties(cb = TRUE, resolution = "20m") %>% rename(fips = GEOID)
library(tidycensus)
us_county_income <-
get_acs(geography = "county", variables = "B19013_001") %>%
select(fips = GEOID, name = NAME, income = "estimate") %>%
drop_na()
cty <- cty %>%
geo_join(us_county_income, by_df = "fips", by_sp = "fips")
library(leaflet)
library(RColorBrewer)
library(htmltools)
pal <- colorNumeric("YlOrRd", domain = cty$income)
labels <-
sprintf(
"<strong>%s</strong><br/> Income: %s $",
cty$name, cty$income) %>%
lapply(htmltools::HTML)
leaflet(cty) %>%
setView(-96, 37.8, 4) %>%
addProviderTiles("CartoDB.PositronNoLabels") %>%
addPolygons(
fillColor = ~pal(income),
weight = 1,
opacity = 1,
color = "white",
fillOpacity = 0.7,
highlight = highlightOptions(
weight = 2,
color = "#666",
fillOpacity = 0.7,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal"),
textsize = "15px",
direction = "auto")) %>%
addLegend(pal = pal,
values = cty$income,
position = "bottomright",
title = "Income (5-year ACS)",
labFormat = labelFormat(suffix = "$"),
opacity = 0.8,
na.label = "No data")