R/utils.R

Defines functions add_custom_legend

Documented in add_custom_legend

#' @title Add a custom legend to a leaflet map
#'
#' @description Adding (manually) a legend in leaflet
#'
#' @param map A leaflet object
#' @param colors A vector of colors (to fill the circles)
#' @param labels A vector of labels
#' @param sizes A vector of sizes
#' @param opacity The opacity of the circles
#'
#' @export

add_custom_legend <- function(map, colors, labels, sizes, opacity = 0.65) {
  # https://stackoverflow.com/questions/58505589/circles-in-legend-for-leaflet-map-with-addcirclemarkers-in-r-without-shiny
  color_additions <- paste0(colors, "; border-radius: 50%; width:", sizes, "px; height:", sizes, "px")
  label_additions <- paste0(
    "<div style='display: inline-block;height: ", sizes, "px;margin-top: 4px;line-height: ", sizes, "px;'>", labels, "</div>"
  )

  out <- leaflet::addLegend(map, colors = color_additions, labels = label_additions, opacity = opacity)

  return(out)
}
DouglasMesquita/marineApp documentation built on Dec. 17, 2021, 5:29 p.m.