library(leaflet)
data("quakes")
quakes <- quakes[1:100,]
numPal <- colorNumeric('viridis', quakes$depth)
sizes <- sizeNumeric(quakes$depth, baseSize = 10)
symbols <- Map(
makeSymbol,
shape = 'triangle',
color = numPal(quakes$depth),
width = sizes,
height = sizes
)
leaflet() %>%
addTiles() %>%
addMarkers(data = quakes,
icon = icons(iconUrl = symbols),
lat = ~lat, lng = ~long) %>%
addLegendSize(
values = quakes$depth,
pal = numPal,
title = 'Depth',
labelStyle = 'margin: auto;',
shape = c('triangle'),
orientation = c('vertical', 'horizontal'),
opacity = .7,
breaks = 5)
# a wrapper for making icons is provided
sizeSymbols <-
makeSymbolsSize(
quakes$depth,
shape = 'cross',
fillColor = numPal(quakes$depth),
color = 'black',
strokeWidth = 1,
opacity = .8,
fillOpacity = .5,
baseSize = 20
)
leaflet() %>%
addTiles() %>%
addMarkers(data = quakes,
icon = sizeSymbols,
lat = ~lat, lng = ~long) %>%
addLegendSize(
values = quakes$depth,
pal = numPal,
title = 'Depth',
shape = 'cross',
orientation = 'horizontal',
strokeWidth = 1,
opacity = .8,
fillOpacity = .5,
color = 'black',
baseSize = 20,
breaks = 5)
# Group layers control
leaflet() %>%
addTiles() %>%
addLegendSize(
values = quakes$depth,
pal = numPal,
title = 'Depth',
labelStyle = 'margin: auto;',
shape = c('triangle'),
orientation = c('vertical', 'horizontal'),
opacity = .7,
breaks = 5,
group = 'Depth') %>%
addLayersControl(overlayGroups = c('Depth'))
# Polyline Legend for Size
baseSize <- 10
lineColor <- '#00000080'
pal <- colorNumeric('Reds', atlStorms2005$MinPress)
leaflet() %>%
addTiles() %>%
addPolylines(data = atlStorms2005,
weight = ~sizeNumeric(values = MaxWind, baseSize = baseSize),
color = ~pal(MinPress),
popup = ~as.character(MaxWind)) %>%
addLegendLine(values = atlStorms2005$MaxWind,
title = 'MaxWind',
baseSize = baseSize,
width = 50,
color = lineColor) %>%
addLegendNumeric(pal = pal,
title = 'MinPress',
values = atlStorms2005$MinPress)
# Stacked Legends
leaflet(quakes) %>%
addTiles() %>%
addSymbolsSize(values = ~10^(mag),
lat = ~lat,
lng = ~long,
shape = 'circle',
color = 'black',
fillColor = 'red',
opacity = 1,
baseSize = 5) |>
addLegendSize(
values = ~10^(mag),
title = 'Magnitude',
baseSize = 5,
shape = 'circle',
color = 'black',
fillColor = 'red',
labelStyle = 'font-size: 18px;',
position = 'bottomleft',
stacked = TRUE,
breaks = 5)
Run the code above in your browser using DataLab