# leaflet -----------------------------------------------------------------
library(sp)
library(tigris)
library(leaflet)
miCounties <- counties(state = "MI", cb = TRUE, progress_bar = FALSE)
micovid <- micovid %>%
mutate(NAME = case_when(
COUNTY == "St Clair" ~ "St. Clair",
COUNTY == "St Joseph" ~ "St. Joseph",
TRUE ~ COUNTY
))
combined <- merge(miCounties, micovid)
# pals and labels for each map -------------------------------------------------------
case_bins <- c(0, 1000, 5000, 15000, 30000, 100000, Inf)
case_pal <- colorBin("Blues", domain = combined$total_cases, bins = case_bins)
case_labels <- sprintf(
"<strong>%s</strong><br/>Cases: %g",
combined$NAME, combined$total_cases
) %>% lapply(htmltools::HTML)
death_bins <- c(0, 50, 100, 150, 500, 1500, 3000, Inf)
death_pal <- colorBin("Reds", domain = combined$total_deaths, bins = death_bins)
death_labels <- sprintf(
"<strong>%s</strong><br/>Deaths: %g",
combined$NAME, combined$total_deaths
) %>% lapply(htmltools::HTML)
leaflet() %>%
addTiles(group = "base") %>%
addPolygons(data = combined,
group = "Cases",
fillColor = ~case_pal(total_cases),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
highlightOptions = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
label = case_labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(data = combined,
title = "Cases",
pal = case_pal, values = ~total_cases, opacity = 0.7,
position = "bottomright", group = "Cases") %>%
addPolygons(data = combined,
group = "Deaths",
fillColor = ~death_pal(total_deaths),
weight = 2,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
highlightOptions = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
label = death_labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(data = combined,
title = "Deaths",
pal = death_pal, values = ~total_deaths, opacity = 0.7,
position = "bottomright", group = "Deaths") %>%
addLayersControl(overlayGroups = c("Cases", "Deaths"),
options = layersControlOptions(collapsed = FALSE)) %>%
hideGroup("Deaths")