This simple project creates a leaflet map that shows the number of murders in each state by gun crime. The data is from a story first published by The Guardian [https://www.theguardian.com/news/datablog/2011/jan/10/gun-crime-us-state].
# reading in the state boundary polygon definitions
states <- geojsonio::geojson_read("https://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_040_00_500k.json", what = "sp")
states$MURDERS <- as.numeric(left_join(data.frame(states$NAME, stringsAsFactors = FALSE),
gun_crime, by = c("states.NAME" = "State"))$Total.murders)
## Warning: NAs introduced by coercion
# setting up the bins for color
bins <- c(0, 10, 20, 50, 100, 200, 500, 1000, Inf)
pal <- colorBin("YlOrRd", domain = states$MURDERS, bins = bins)
m <- leaflet(states) %>%
setView(-96, 37.8, 4) %>%
addTiles()
#m %>% addPolygons()
labels <- sprintf("%s (%d murders)", states$NAME, states$MURDERS)
stamp <- tags$div(timestamp())
## ##------ Sat Dec 28 13:45:07 2019 ------##
m %>% addPolygons(
fillColor = ~pal(MURDERS),
weight = 2,
opacity = 1,
color = "white",
dashArray = 3,
fillOpacity = 0.7,
highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE),
label = labels,
labelOptions = labelOptions(
style = list("font-weight" = "normal", padding = "3px 8px"),
textsize = "15px",
direction = "auto")) %>%
addLegend(
pal = pal, values = ~MURDERS, opacity = 0.7, title = NULL,
position = "bottomright") %>%
addControl(stamp, position = "bottomleft")