Skip to contents

Creates an interactive leaflet map from raster or sf vector data, with options to customize basemaps, styling, and popups.

Usage

leafmap(
  m = NULL,
  data = NULL,
  group_layers = NULL,
  label = NULL,
  opacity = 0.5,
  color = "black",
  fill_color = color,
  radius = 3,
  weight = 2,
  palette = "viridis",
  basemaps = c("Esri.WorldImagery", "Esri.WorldTopoMap"),
  add_legend = TRUE,
  add_image_query = TRUE
)

Arguments

m

Optional existing leaflet map object. If NULL, a new map is initialized.

data

A Raster*, terra::SpatRaster, or sf object to be plotted. If not provided, m will be interpreted as the data.

group_layers

Character vector of existing overlay groups. Used to maintain group-layer visibility toggles.

label

A character string for the layer label and legend title. If NULL, will be auto-generated from the object name.

opacity

Numeric (0–1) for layer transparency. Defaults to 0.5.

color

Color used for vector geometries (ignored for rasters). Defaults to "black".

fill_color

Color used for fill of vector geometries (ignored for rasters). Defaults to color parameter.

radius

Numeric size of circle markers for point geometries. Defaults to 3.

weight

Line or border thickness for vector geometries. Defaults to 2.

palette

Color palette name used with leaflet::colorNumeric() for raster coloring. Defaults to "viridis". If categorical values, supply a list - e.g. list(classes = 1, colors = "#99d2ff", labels = "Water"))

basemaps

Character vector of tile provider names (from leaflet::providers) to include as base layers. Defaults to c("Esri.WorldImagery", "Esri.WorldTopoMap").

add_legend

Logical to produce a legend or not. Defaults to TRUE. Also controls if raster value query appears.

add_image_query

Logical to add mouse hover query of raster values. Defaults to TRUE.

Value

A leaflet map object with the data layer(s) and controls.

Details

This function is designed to handle both standalone and piped usage. If the first argument is a raster or sf object and data is NULL, it automatically reassigns the input appropriately. It can visualize raster values with color palettes and legends, or render vector data (points, lines, polygons) with informative popups.

  • Raster data is projected to WGS84 (EPSG:4326) and colorized using a continuous palette.

  • Vector data supports POINT, LINESTRING, and POLYGON geometries.

  • Attributes are displayed in scrollable popups if there are many fields.

  • The function adds scale bars, measurement tools, and layer controls.

  • add_image_query can make the file size of the leaflet html widget very large e.g. up to 20x's larger. It is recommended to have it = FALSE when exporting as a Web Page or html widget.

Examples

if (FALSE) { # \dontrun{
library(leaflet)
library(sf)
library(raster)
# From scratch
leafmap(data = st_read(system.file("shape/nc.shp", package = "sf")))

# Add to existing map
m <- leaflet()
leafmap(m, st_read(system.file("shape/nc.shp", package = "sf")))
} # }