Skip to contents

Supply the legend entries exactly one of three ways: a built-in preset (builtin), a named vector or list of label-to-color pairs (legend), or parallel labels and colors vectors. Each call adds another legend, so a map can carry several at once.

Usage

add_legend(
  map,
  title = NULL,
  legend = NULL,
  labels = NULL,
  colors = NULL,
  builtin = NULL,
  position = c("bottom-left", "bottom-right", "top-left", "top-right"),
  shape = c("square", "circle", "line")
)

Arguments

map

A GeoLibre widget.

title

Legend title. Defaults to "Legend", or the preset's own title when builtin is supplied without one.

legend

A named vector or list mapping label to CSS color. Order is preserved.

labels

Item labels, paired position-wise with colors.

colors

Item CSS colors, paired position-wise with labels.

builtin

A built-in preset name from builtin_legend_names().

position

Corner for the legend: "top-left", "top-right", "bottom-left", or "bottom-right".

shape

Swatch shape for every item: "square", "circle", or "line".

Value

The modified widget.

See also

add_colorbar() for continuous rasters, builtin_legend_names()

Examples

map <- geolibre() |>
  add_legend(
    "Land cover",
    legend = c(Water = "#466b9f", Forest = "#1c5f2c"),
    position = "bottom-left"
  )

# A built-in preset carries its own title and colors.
geolibre() |> add_legend(builtin = "nlcd")