Last active
July 3, 2019 13:11
-
-
Save PaulC91/1672bea3bb6680a3e3c5056b55d9170c to your computer and use it in GitHub Desktop.
function for adding a scaled circle legend to a leaflet map in R
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
addBubbleLegend <- function( | |
map, title = "", range, scaling_fun, ..., | |
strokeColor, weight, fillColor, fillOpacity, | |
position = c("topright", "bottomright", "bottomleft", "topleft"), | |
data = getMapData(map), layerId = NULL | |
) { | |
range <- base::pretty(sort(range), 20) | |
range <- range[range != 0] | |
min_n <- min(range, na.rm = TRUE) | |
med_n <- median(range, na.rm = TRUE) | |
max_n <- max(range, na.rm = TRUE) | |
n_range <- c(min_n, med_n, max_n) | |
radii <- scaling_fun(n_range, ...) | |
circle_style <- glue::glue( | |
"border-radius:50%; | |
border: {weight}px solid {strokeColor}; | |
background: {paste0(fillColor, round(fillOpacity*100, 0))}; | |
position: absolute; | |
bottom:1px; | |
right:25%; | |
left:50%;" | |
) | |
text_style <- | |
"text-align: right; | |
font-size: 11px; | |
position: absolute; | |
bottom: 0px; | |
right:1px;" | |
circle_legend <- HTML(glue( | |
'<div class="bubble-legend"> | |
<div id="legendTitle" style="text-align: center; font-weight: bold;">{title}</div> | |
<div class="symbolsContainer" style="min-width: {radii[3]*2 + 25}px; min-height: {radii[3]*2}px;"> | |
<div class="legendCircle" style="width: {radii[1] * 2}px; height: {radii[1] * 2}px; margin-left: {-radii[1]}px; {circle_style}"></div> | |
<div class="legendCircle" style="width: {radii[2] * 2}px; height: {radii[2] * 2}px; margin-left: {-radii[2]}px; {circle_style}"></div> | |
<div class="legendCircle" style="width: {radii[3] * 2}px; height: {radii[3] * 2}px; margin-left: {-radii[3]}px; {circle_style}"></div> | |
<div><p class="legendValue" style="margin-bottom: {radii[1] * 2 - 12}px; {text_style}">{n_range[1]}</p></div> | |
<div><p class="legendValue" style="margin-bottom: {radii[2] * 2 - 12}px; {text_style}">{n_range[2]}</p></div> | |
<div><p class="legendValue" style="margin-bottom: {radii[3] * 2 - 12}px; {text_style}">{n_range[3]}</p></div> | |
</div> | |
</div>' | |
)) | |
return( | |
addControl(map, html = circle_legend, position = position, layerId = layerId) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment