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)
# Text legend (categorical)
quakes$grp <- sample(c('A', 'B', 'C'), nrow(quakes), replace = TRUE)
grpPal <- colorFactor('Dark2', quakes$grp)
leaflet(quakes) %>%
addTiles() %>%
addText(lng = ~long, lat = ~lat, text = ~grp,
color = 'white', fillColor = ~grpPal(grp), width = 20) %>%
addLegendText(pal = grpPal, values = ~grp, title = 'Group',
labels = c('Group A', 'Group B', 'Group C'),
position = 'topright', fontFamily = 'sans-serif')
# Text size legend (auto-computed font size)
leaflet(quakes) %>%
addTiles() %>%
addTextSize(lng = ~long, lat = ~lat,
text = ~as.character(round(mag, 1)),
values = ~mag, color = 'black', fillColor = 'yellow',
baseSize = 30) %>%
addLegendTextSize(pal = colorNumeric('viridis', quakes$mag),
values = ~mag, text = 'M', title = 'Magnitude',
baseSize = 30, breaks = 5,
position = 'bottomright')
# dashed lines
leaflet() %>%
addTiles() %>%
addLegendSymbol(color='black', dashArray = list("none", "24"),
shape = c("line", "line"), values = c('solid', 'dashed'),
strokeWidth = 10, height = 20, width = 100)
#advanced shape customization, aesthetic arguments are propogated if
# vectors are equal length or of size 1
defaultShapes <- availableShapes()[["default"]]
leaflet() %>%
addTiles() %>%
addLegendSymbol(
shape = defaultShapes,
color = "black",
fillColor =
colorRampPalette(c("blue", "yellow"))(length(defaultShapes)),
width = seq(1L, length(defaultShapes), 1L) * length(defaultShapes),
values = factor(defaultShapes, defaultShapes),
strokeWidth = 2
)
# label centered inside each legend symbol
leaflet() %>%
addTiles() %>%
addLegendSymbol(
shape = c('circle', 'circle', 'circle'),
color = 'black',
fillColor = c('red', 'blue', 'green'),
fillOpacity = 0.8,
values = c('A', 'B', 'C'),
label = c('A', 'B', 'C'),
width = 24
)
Run the code above in your browser using DataLab