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'
)
Run the code above in your browser using DataLab