addLeafLegends {leaflegend} | R Documentation |
Add Customizable Color Legends to a 'leaflet' map widget
Description
Functions for more control over the styling of 'leaflet' legends. The 'leaflet' map is passed through and the output is a 'leaflet' control so that the legends are integrated with other functionality of the API. Style the text of the labels, the symbols used, orientation of the legend items, and sizing of all elements.
Usage
addLegendNumeric(
map,
pal,
values,
title = NULL,
shape = c("rect", "stadium"),
orientation = c("vertical", "horizontal"),
width = 20,
height = 100,
bins = 7,
numberFormat = function(x) {
prettyNum(x, format = "f", big.mark = ",", digits =
3, scientific = FALSE)
},
tickLength = 4,
tickWidth = 1,
decreasing = FALSE,
fillOpacity = 1,
group = NULL,
labels = NULL,
naLabel = "NA",
labelStyle = "",
className = "info legend leaflet-control",
data = leaflet::getMapData(map),
...
)
addLegendQuantile(
map,
pal,
values,
title = NULL,
labelStyle = "",
shape = "rect",
orientation = c("vertical", "horizontal"),
width = 24,
height = 24,
numberFormat = function(x) {
prettyNum(x, big.mark = ",", scientific = FALSE,
digits = 1)
},
opacity = 1,
fillOpacity = opacity,
group = NULL,
className = "info legend leaflet-control",
naLabel = "NA",
between = " - ",
data = leaflet::getMapData(map),
...
)
addLegendBin(
map,
pal,
values,
title = NULL,
labelStyle = "",
shape = "rect",
orientation = c("vertical", "horizontal"),
width = 24,
height = 24,
numberFormat = function(x) {
format(round(x, 3), big.mark = ",", trim = TRUE,
scientific = FALSE)
},
opacity = 1,
fillOpacity = opacity,
group = NULL,
className = "info legend leaflet-control",
naLabel = "NA",
between = " - ",
data = leaflet::getMapData(map),
...
)
addLegendFactor(
map,
pal,
values,
title = NULL,
labelStyle = "",
shape = "rect",
orientation = c("vertical", "horizontal"),
width = 24,
height = 24,
opacity = 1,
fillOpacity = opacity,
group = NULL,
className = "info legend leaflet-control",
naLabel = "NA",
data = leaflet::getMapData(map),
...
)
Arguments
map |
a map widget object created from 'leaflet' |
pal |
the color palette function, generated from colorNumeric |
values |
the values used to generate colors from the palette function |
title |
the legend title, pass in HTML to style |
shape |
the desired shape of the symbol, See availableShapes |
orientation |
stack the legend items vertically or horizontally |
width |
in pixels |
height |
in pixels |
bins |
an approximate number of tick-marks on the color gradient for the colorNumeric palette if it is of length one; you can also provide a numeric vector as the pre-defined breaks |
numberFormat |
formatting functions for numbers that are displayed e.g. format, prettyNum |
tickLength |
in pixels |
tickWidth |
in pixels |
decreasing |
order of numbers in the legend |
fillOpacity |
fill opacity of the legend items |
group |
group name of a leaflet layer group |
labels |
labels |
naLabel |
the legend label for NAs in values |
labelStyle |
character string of style argument for HTML text |
className |
extra CSS class to append to the control, space separated |
data |
a data object. Currently supported objects are matrices, data
frames, spatial objects from the sp package
( |
... |
arguments to pass to addControl |
opacity |
opacity of the legend items |
between |
a separator between legend range labels |
Value
an object from addControl
Examples
library(leaflet)
data(quakes)
# Numeric Legend
numPal <- colorNumeric('viridis', quakes$depth)
leaflet() %>%
addTiles() %>%
addLegendNumeric(
pal = numPal,
values = quakes$depth,
position = 'topright',
title = 'addLegendNumeric (Horizontal)',
orientation = 'horizontal',
shape = 'rect',
decreasing = FALSE,
height = 20,
width = 100
) %>%
addLegendNumeric(
pal = numPal,
values = quakes$depth,
position = 'topright',
title = htmltools::tags$div('addLegendNumeric (Decreasing)',
style = 'font-size: 24px; text-align: center; margin-bottom: 5px;'),
orientation = 'vertical',
shape = 'stadium',
decreasing = TRUE,
height = 100,
width = 20
) %>%
addLegend(pal = numPal, values = quakes$depth, title = 'addLegend')
# Quantile Legend
# defaults to adding quantile numeric break points
quantPal <- colorQuantile('viridis', quakes$mag, n = 5)
leaflet() %>%
addTiles() %>%
addCircleMarkers(data = quakes,
lat = ~lat,
lng = ~long,
color = ~quantPal(mag),
opacity = 1,
fillOpacity = 1
) %>%
addLegendQuantile(pal = quantPal,
values = quakes$mag,
position = 'topright',
title = 'addLegendQuantile',
numberFormat = function(x) {prettyNum(x, big.mark = ',',
scientific = FALSE, digits = 2)},
shape = 'circle') %>%
addLegendQuantile(pal = quantPal,
values = quakes$mag,
position = 'topright',
title = htmltools::tags$div('addLegendQuantile',
htmltools::tags$br(),
'(Omit Numbers)'),
numberFormat = NULL,
shape = 'circle') %>%
addLegend(pal = quantPal, values = quakes$mag, title = 'addLegend')
# Factor Legend
# Style the title with html tags, several shapes are supported drawn with svg
quakes[['group']] <- sample(c('A', 'B', 'C'), nrow(quakes), replace = TRUE)
factorPal <- colorFactor('Dark2', quakes$group)
leaflet() %>%
addTiles() %>%
addCircleMarkers(
data = quakes,
lat = ~ lat,
lng = ~ long,
color = ~ factorPal(group),
opacity = 1,
fillOpacity = 1
) %>%
addLegendFactor(
pal = factorPal,
title = htmltools::tags$div('addLegendFactor', style = 'font-size: 24px;
color: red;'),
values = quakes$group,
position = 'topright',
shape = 'triangle',
width = 50,
height = 50
) %>%
addLegend(pal = factorPal,
values = quakes$group,
title = 'addLegend')
# Bin Legend
# Restyle the text of the labels, change the legend item orientation
binPal <- colorBin('Set1', quakes$mag)
leaflet(quakes) %>%
addTiles() %>%
addCircleMarkers(
lat = ~ lat,
lng = ~ long,
color = ~ binPal(mag),
opacity = 1,
fillOpacity = 1
) %>%
addLegendBin(
pal = binPal,
position = 'topright',
values = ~mag,
title = 'addLegendBin',
labelStyle = 'font-size: 18px; font-weight: bold;',
orientation = 'horizontal'
) %>%
addLegend(pal = binPal,
values = quakes$mag,
title = 'addLegend')
# Group Layer Control
# Works with baseGroups and overlayGroups
leaflet() %>%
addTiles() %>%
addLegendNumeric(
pal = numPal,
values = quakes$depth,
position = 'topright',
title = 'addLegendNumeric',
group = 'Numeric Data'
) %>%
addLegendQuantile(
pal = quantPal,
values = quakes$mag,
position = 'topright',
title = 'addLegendQuantile',
group = 'Quantile'
) %>%
addLegendBin(
data = quakes,
pal = binPal,
position = 'bottomleft',
title = 'addLegendBin',
group = 'Bin',
values = ~mag
) %>%
addLayersControl(
baseGroups = c('Numeric Data', 'Quantile'), overlayGroups = c('Bin'),
position = 'bottomright'
)