add_legend {webmap}R Documentation

Add legend to a web map

Description

Add a legend to a Leaflet map.

Usage

add_legend(
  map,
  labels,
  colors,
  radius,
  opacity = 0.5,
  symbol = c("square", "circle"),
  title = "EXPLANATION",
  position = "topright"
)

Arguments

map

'leaflet'. Map widget object

labels

'character' vector. Labels in the legend.

colors

'character' vector. HTML colors corresponding to labels.

radius

'numeric' number. Border radius of symbols in the legend, in pixels.

opacity

'numeric' number. Opacity of symbols in the legend, from 0 to 1.

symbol

'character' string. Symbol type in the legend, either "square" or "circle".

title

'character' string. Legend title

position

'character' string. Position of the button on the web map. Possible values are "topleft", "topright", "bottomleft", and "bottomright".

Value

A new HTML web map with added element, an object of class 'leaflet'.

Author(s)

J.C. Fisher, U.S. Geological Survey, Idaho Water Science Center

See Also

make_map function for creating a map widget.

Examples

# define marker colors based on whether a city serves as a capital
colors <- c(
  "Non-capital" = "green",
  "Capital" = "red"
)
fill_colors <- colors[(us_cities$capital > 0) + 1L] |>
  as.character()

# print map with city circle markers and a map legend
make_map(maps = "Topo") |>
  leaflet::addCircleMarkers(
    lng = ~lng,
    lat = ~lat,
    radius = 6,
    color = "white",
    weight = 1,
    opacity = 1,
    fillColor = fill_colors,
    fillOpacity = 1,
    fill = TRUE,
    data = us_cities
  ) |>
  add_legend(
    labels = names(colors),
    colors = colors,
    radius = 5,
    opacity = 1,
    symbol = "circle"
  )

[Package webmap version 1.1.0 Index]