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.
# Create a Leaflet map
leaflet_map <- leaflet(data = GunsPerState) %>% # Create a new Leaflet map object using the GunsPerState data
addProviderTiles("CartoDB.Positron") %>% # Add a basemap from CartoDB (CartoDB.Positron is used here)
# Add polygons to the map (representing states)
addPolygons(
data = states_geojson, # Use the states_geojson data as the source for polygons
fillColor = ~colorNumeric(palette = "YlOrRd", domain = GunsPerState$GunN)(GunsPerState$GunN), # Set fill color based on GunN values
fillOpacity = 0.7, # Set the fill opacity to 0.7
color = "white", # Set the border color to white
weight = 1, # Set the border weight to 1
# Define options for highlighting when interacting with the polygons
highlight = highlightOptions(
weight = 5, # Increase border weight when highlighted
color = "black", # Set the border color to black when highlighted
bringToFront = TRUE # Bring the highlighted polygon to the front
),
label = ~paste(name, "Registered Firearms:", formatC(GunsPerState$GunN, format = "f", digits = 2)) # Define labels with state names and GunN values
) %>% # Chain the next function
# Add a legend to the map
addLegend(
position = "bottomright", # Set the legend position to the bottom-right
pal = colorNumeric(palette = "YlOrRd", domain = GunsPerState$GunN), # Set the color palette based on GunN values
values = ~GunsPerState$GunN, # Specify the values to be displayed on the legend
title = "Registered Firearms" # Set the legend title
)
# Display the Leaflet map
leaflet_map # Print the map with all the configured layers