Learn R Programming

leaflegend (version 0.3.0)

addLeafLegends: 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,
  ...
)

addLegendQuantile( map, pal, values, title = NULL, labelStyle = "", shape = c("rect", "circle", "triangle", "plus", "cross", "diamond", "star", "stadium"), orientation = c("vertical", "horizontal"), width = 24, height = 24, numberFormat = function(x) { prettyNum(x, big.mark = ",", scientific = FALSE, digits = 1) }, opacity = 1, fillOpacity = opacity, ... )

addLegendBin( map, pal, values, title = NULL, labelStyle = "", shape = c("rect", "circle", "triangle", "plus", "cross", "diamond", "star", "stadium"), orientation = c("vertical", "horizontal"), width = 24, height = 24, opacity = 1, fillOpacity = opacity, ... )

addLegendFactor( map, pal, values, title = NULL, labelStyle = "", shape = c("rect", "circle", "triangle", "plus", "cross", "diamond", "star", "stadium"), orientation = c("vertical", "horizontal"), width = 24, height = 24, opacity = 1, fillOpacity = opacity, ... )

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

shape of the color symbols

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

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

...

arguments to pass to addControl

labelStyle

character string of style argument for HTML text

opacity

opacity of the legend items

Value

an object from addControl

Examples

Run this code
# NOT RUN {
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() %>%
  addTiles() %>%
  addCircleMarkers(
    data = quakes,
    lat = ~ lat,
    lng = ~ long,
    color = ~ binPal(mag),
    opacity = 1,
    fillOpacity = 1
  ) %>%
  addLegendBin(
    pal = binPal,
    values = quakes$mag,
    position = 'topright',
    title = 'addLegendBin',
    labelStyle = 'font-size: 18px; font-weight: bold;',
    orientation = 'horizontal'
  ) %>%
  addLegend(pal = binPal,
            values = quakes$mag,
            title = 'addLegend')
# }

Run the code above in your browser using DataLab