Code
library(sf)
library(mapview)
# sample data
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"), quiet=TRUE) |>
st_transform(4326)
mapview::mapView(nc, zcol="SID74")
The following code chunks demonstrate:
mapView()
function to quickly map spatial data.leaflet()
function has all the flexibility, including with changing the basemap to the best possible Esri Ocean Basemap with labels, but it requires more code to replicate the original mapView()
map.Note:
code-link: true
in the YAML header of this Quarto document.mapview::mapView()
vs simply mapView()
, is not needed as long as the library is loaded with, e.g. library(mapview)
, but included below to explicitly show which R libraries contribute to the functionality.mapView()
Use North Carolina (NC) Sudden Infant Death Syndrome (SIDS) sample data. (For details: ?nc
.)
library(sf)
library(mapview)
# sample data
nc <- sf::st_read(system.file("shape/nc.shp", package="sf"), quiet=TRUE) |>
st_transform(4326)
mapview::mapView(nc, zcol="SID74")
leaflet()
library(leaflet)
leaflet::leaflet() |>
# add base: blue bathymetry and light brown/green topography
addProviderTiles(
"Esri.OceanBasemap",
options = providerTileOptions(
variant = "Ocean/World_Ocean_Base")) |>
# add reference: placename labels and borders
addProviderTiles(
"Esri.OceanBasemap",
options = providerTileOptions(
variant = "Ocean/World_Ocean_Reference"))
ms_basemap()
This functionmsens::
ms_basemap()
is really just a wrapper function for the leaflet()
example above.
library(glue)
library(msens) # remotes::install_github("MarineSensitivity/msens")
msens::ms_basemap() |>
addPolygons(
data = nc,
label = ~NAME,
popup = ~glue("NAME: {NAME}<br>FIPS: {FIPS}"))
ms_basemap()
+ popupTable()
library(leafpop)
msens::ms_basemap() |>
leaflet::addPolygons(
data = nc,
popup = leafpop::popupTable(nc))
ms_basemap()
+ popupTable()
+ addLegend()
leaflet::
mapview::
leaflet::addPolygons()
# color palette
pal <- colorNumeric(
palette = "viridis",
domain = nc$SID74)
msens::ms_basemap() |>
leaflet::addPolygons(
data = nc,
popup = leafpop::popupTable(nc),
color = "black",
weight = 1,
fillColor = ~pal(SID74),
fillOpacity = 0.6) |>
leaflet::addLegend(
pal = pal,
values = nc$SID74,
title = "nc - SID74")