Now that we have the (very) basics of leaflet down, let’s try to load some geoJSON and plot it. geoJSON is the easiest geo-spatial format to work with, as it acts essentially as both a data frame and a geo-spatial one, to oversimplify a bit. So we can use dplyr’s data-cleaning tools on it - filter, summarize, mutate, and so on. We can merge it with other data sets, be they spatial or non-spatial.
Let’s get geoJSON that defines the county boundaries of California:
https://gis.data.ca.gov/datasets/CALFIRE-Forestry::california-county-boundaries/explore
ca <- read_sf("~/Downloads/California_County_Boundaries.geojson")
Define a color palette
pal <- colorNumeric("viridis", ca$OBJECTID, 58)
(have to describe much more about picking a palette here. also link to documention)
Let’s plot it!
leaflet(ca) %>%
addTiles() %>%
addPolygons(stroke = FALSE,
smoothFactor = 0.9,
fillOpacity = 0.7,
fillColor = ~pal(ca$OBJECTID),
label = ~paste0(ca$COUNTY_NAME)
)