5 minutes
leaflegend Recipes
Requires leaflegend >= 1.0.0
Base Data and Map
library(leaflet)
library(leaflegend)
mapData <- data.frame(x = c(2, 1, 5, 4,3) , y = c('A', 'B', 'C', 'B', 'A'),
lng = -(118:122), lat = rep(42, 5))
map <- leaflet::leaflet(data = mapData,
options = leafletOptions(zoomControl = FALSE))
Color Scales
Create Legend for a Continuous Scale
numPal <- colorNumeric('inferno', mapData$x)
lgd <-
map |> addLegendNumeric(pal = numPal, values = ~x, decreasing = TRUE) |>
addLegendNumeric(pal = numPal, values = ~x, orientation = 'horizontal',
width = 120, height = 20, position = 'topright')
lgd
Create Legend for Quantiles
quantPal <- colorQuantile('Reds', mapData$x, n = 3)
lgd <-
map |> addLegendQuantile(pal = quantPal, values = ~x) |>
addLegendQuantile(pal = quantPal, values = ~x, numberFormat = NULL,
position = 'topright')
lgd
Create Legend for Bins and Change Legend Symbol
binPal <- colorBin(c('grey', 'blue'), mapData$x, bins = 3, pretty = FALSE)
lgd <-
map |> addLegendBin(pal = binPal, shape = 'triangle')
lgd
Create Legend for Factors and Re-order
factorPal <- colorFactor('Dark2', domain = mapData$y)
reOrder <- c('C', 'A', 'B')
factorPalRev <- colorFactor('Dark2', domain = reOrder, ordered = TRUE)
lgd <-
map |> addLegendFactor(pal = factorPal, values = ~y) |>
addLegendFactor(pal = factorPalRev,
values = ~factor(y, levels = reOrder),
position = 'topright')
lgd
Symbols and Size Scales
Legend and Points with Size Encoding
lgd <-
map |> addLegendSize(values = ~x, color = 'black', shape = 'plus',
breaks = 3) |>
addSymbolsSize(lng = ~lng, lat = ~lat, values = ~x, shape = 'plus',
color = 'black')
lgd
Legend and Points with Double Encoding
lgd <-
map |> addLegendSize(values = ~x, shape = 'plus', breaks = 3, pal = numPal,
color = 'black') |>
addSymbolsSize(lng = ~lng, lat = ~lat, values = ~x, shape = 'plus',
color = 'black', fillColor = ~numPal(x))
lgd
Legend and Points with Size and Color Encoding
lgd <-
map |> addLegendSize(values = ~x, shape = 'plus', breaks = 3, pal = numPal,
color = 'black', fillColor = 'transparent') |>
addSymbolsSize(lng = ~lng, lat = ~lat, values = ~x, shape = 'plus',
color = 'black', fillColor = ~factorPal(y)) |>
addLegendFactor(pal = factorPal, values = ~y, position = 'topright')
lgd
Legend and Points with Symbol Encoding
lgd <-
map |> addSymbols(lng = ~lng, lat = ~lat, values = ~y, color = 'black') |>
addLegendSymbol(values = ~y, color = 'black')
lgd
Legend and Points with Symbol Encoding (Non-Defaults)
lgd <-
map |> addSymbols(lng = ~lng, lat = ~lat, values = ~y,
fillColor = ~factorPal(y), color = 'black',
shape = c('cross', 'diamond', 'star')) |>
addLegendSymbol(values = ~y, pal = factorPal,
shape = c('cross', 'diamond', 'star'), color = 'black')
lgd
Legend for Line Size Encodings
lgd <-
map |> addLegendLine(values = ~x, color = 'black',
orientation = 'horizontal', width = 50)
lgd
Images
Add Custom Images to a Legend
# use raster image
leafImg <- system.file('img/leaf-blue.png', package = 'leaflegend')
# use svg
circle <- makeSymbol('rect', fillColor = 'red', color = 'black', width = 20)
lgd <-
map |>
addLegendImage(images = list(leafImg, circle), labels = c('png', 'svg'),
height = 30, width = 20)
lgd
Generate Map Symbols
shapes <- c('rect', 'circle', 'triangle', 'plus', 'cross', 'star')
colors <- c('blue', 'red', 'yellow', 'green', 'orange', 'purple')
symbols <- Map(f = makeSymbol, shape = shapes, fillColor = colors,
color = 'black', opacity = 1, fillOpacity = .5, width = 20,
`stroke-width` = 2)
lgd <-
map |> addLegendImage(images = symbols, labels = shapes,
orientation = 'horizontal')
lgd
Add Awesome Marker Legend and Specify Icon as Text
iconSet <- awesomeIconList(
`Font Awesome` = makeAwesomeIcon(icon = "font-awesome", library = "fa",
markerColor= 'red', iconColor = 'black'),
Ionic = makeAwesomeIcon(icon='R', library = 'fa',
text = fontawesome::fa("r-project"))
)
lgd <-
map |> addLegendAwesomeIcon(iconSet = iconSet)
lgd
Customization
Turn off Legend Background
lgd <-
map |> addLegendFactor(values = ~y, pal = factorPal,
class = 'leaflet-control',
labelStyle = 'color: black;')
lgd
Add Titles with Styling
lgd <-
map |> addLegendFactor(values = ~y, pal = factorPal,
title = htmltools::tags$div('Red Title',
style = 'color:red;'))
lgd
Change Label Styling
lgd <-
map |> addLegendFactor(values = ~y, pal = factorPal,
labelStyle = 'color: blue; font-size: 10px;')
lgd
Symbol Styling
lgd <-
map |> addLegendFactor(values = ~y, pal = factorPal, shape = 'polygon',
fillOpacity = .5, width = 20, height = 20)
lgd
Make Symbol Icons
symbolSet <- makeSymbolIcons(shape = c('rect', 'circle', 'triangle'),
width = 10, color = 'grey', opacity = 1)
lgd <-
map |> addMarkers(lng = ~lng, lat = ~lat, icon = ~symbolSet[as.factor(y)])
lgd
Make Symbols with Size Encodings
lgd <-
map |> addMarkers(lng = ~lng, lat = ~lat,
icon = ~makeSymbolsSize(x, color = 'black',
fillColor = 'black',
baseSize = 20,
fillOpacity = .5))
lgd
Legends with Overlay Controls
lgd <-
map |> addLegendFactor(values = ~y, pal = factorPal, group = 'Legend') |>
addLayersControl(overlayGroups = 'Legend',
options = layersControlOptions(collapsed = FALSE))
lgd