Learn R Programming

leaflet (version 2.2.2)

addRasterLegend: Add a color legend for a SpatRaster to a map

Description

A function for adding a legend that is specifically designed for terra::SpatRaster objects, with categorical values, that carry their own color table.

Usage

addRasterLegend(map, x, layer = 1, ...)

Arguments

map

a map widget object

x

a SpatRaster object with a color table

layer

the layer of the raster to target

...

additional arguments to pass through to addLegend()

See Also

addRasterImage()

Examples

Run this code
if (FALSE) { # interactive()

library(terra)

r <- rast("/vsicurl/https://geodata.ucdavis.edu/test/pr_nlcd.tif")
leaflet() %>%
  addTiles() %>%
  addRasterImage(r, opacity = 0.75) %>%
  addRasterLegend(r, opacity = 0.75)

plot.new() # pause in interactive mode

rr <- r
levels(rr)  <- NULL
leaflet() %>%
  addTiles() %>%
  addRasterImage(rr, opacity = 0.75) %>%
  addRasterLegend(rr, opacity = 0.75)
}

Run the code above in your browser using DataLab