library(geojson)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
## (status 2 uses the sf package in place of rgdal)
##
## Attaching package: 'geojson'
## The following object is masked from 'package:graphics':
##
## polygon
library(geojsonio)
## Registered S3 method overwritten by 'geojsonsf':
## method from
## print.geojson geojson
##
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
##
## pretty
library(leaflet)
library(readr)
# Define the URL to the GeoJSON file containing state boundaries
states_geojson_url <- "https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.json"
# Read the GeoJSON data and convert it to spatial objects
states_geojson <- geojson_read(states_geojson_url, what = "sp")
# Read the CSV data containing information about guns per state
GunsPerState <- read_csv("GunsPerState.csv")
## Rows: 51 Columns: 2
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): state_name
## dbl (1): GunN
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Your existing Leaflet map code
leaflet_map <- leaflet(data = GunsPerState) %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(
data = states_geojson,
fillColor = ~colorNumeric(palette = "YlOrRd", domain = GunsPerState$GunN)(GunsPerState$GunN),
fillOpacity = 0.7,
color = "black",
weight = 1,
highlight = highlightOptions(
weight = 5,
color = "black",
bringToFront = TRUE
),
label = ~paste(name, "Registered Firearms:", formatC(GunsPerState$GunN, format = "f", digits = 2))
) %>%
addLegend(
position = "bottomright",
pal = colorNumeric(palette = "YlOrRd", domain = GunsPerState$GunN),
values = ~GunsPerState$GunN,
title = "Registered Firearms"
)
leaflet_map <- addControl(
leaflet_map,
position = "topright",
html = '<div style="font-size: 18px; font-weight: bold; text-align: center;">Registered Firearms by State</div>'
)
leaflet_map