From fda701250eb7cc568f895c43778f6d9790ae2254 Mon Sep 17 00:00:00 2001 From: Thomas Roh Date: Sat, 15 Jan 2022 11:54:08 -0800 Subject: [PATCH 1/5] adding in addLineLegend and better error messages --- DESCRIPTION | 2 +- NAMESPACE | 1 + NEWS.md | 4 +++ R/legend.R | 77 +++++++++++++++++++++++++++++++++++++++++--- man/addLegendSize.Rd | 46 ++++++++++++++++++++++++-- 5 files changed, 123 insertions(+), 7 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 750848b..4b310a8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: leaflegend Type: Package Title: Add Custom Legends to 'leaflet' Maps -Version: 0.5.0 +Version: 0.6.0 Authors@R: c( person("Thomas", "Roh", email = "thomas@roh.engineering", role = c("aut", "cre")), person("Ricardo Rodrigo", "Basa", email = "radbasa@gmail.com", role = c("ctb"))) diff --git a/NAMESPACE b/NAMESPACE index ea3de91..2be9015 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ export(addLegendAwesomeIcon) export(addLegendBin) export(addLegendFactor) export(addLegendImage) +export(addLegendLine) export(addLegendNumeric) export(addLegendQuantile) export(addLegendSize) diff --git a/NEWS.md b/NEWS.md index c22e862..00ecde2 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,10 @@ * updated layers control to handle special characters in group name. All non-alphanumeric characters are removed from the class names and javascript selectors. +* added better error message for missing color and pal + +* adding `addLegendLine` to add height only encoding of size based on values + # leaflegend 0.5.0 * updated example in README diff --git a/R/legend.R b/R/legend.R index 15e36a2..7526114 100644 --- a/R/legend.R +++ b/R/legend.R @@ -313,8 +313,8 @@ makeSymbol <- function(shape, width, height = width, color, fillColor = color, ), 'line' = htmltools::tags$line( id = 'line', - x1 = 0 + strokewidth, - x2 = width + strokewidth, + x1 = 0, + x2 = width + strokewidth * 2, y1 = height / 2 + strokewidth, y2 = height / 2 + strokewidth, stroke = color, @@ -972,7 +972,7 @@ addLegendFactor <- function(map, leaflegendAddControl(map, html = htmltools::tagList(htmlElements), className = className, group = group, ...) } -#' Add a legend that for the sizing of symbols +#' Add a legend that for the sizing of symbols or the width of lines #' #' @param map #' @@ -1140,6 +1140,25 @@ addLegendFactor <- function(map, #' 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) addLegendSize <- function(map, pal, values, @@ -1161,6 +1180,7 @@ addLegendSize <- function(map, shape <- match.arg(shape) sizes <- sizeBreaks(values, breaks, baseSize) if ( missing(color) ) { + stopifnot( missing(color) & !missing(pal)) colors <- pal(as.numeric(names(sizes))) } else { stopifnot(length(color) == 1 || length(color) == length(breaks)) @@ -1187,7 +1207,8 @@ addLegendSize <- function(map, `stroke-width` = strokeWidth) addLegendImage(map, images = symbols, labels = numberFormat(as.numeric(names(sizes))), title = title, labelStyle = labelStyle, - orientation = orientation, width = sizes, height = sizes, group = group, className = className, ...) + orientation = orientation, width = sizes, height = sizes, + group = group, className = className, ...) } @@ -1228,6 +1249,7 @@ makeSizeIcons <- function(values, shape <- match.arg(shape) if ( missing(color) ) { if ( missing(colorValues) ) { + stopifnot( all(missing(color), missing(pal), missing(colorValues)) ) colors <- pal(values) } else { colors <- pal(colorValues) @@ -1264,6 +1286,53 @@ makeSizeIcons <- function(values, ... ) } +#' @param width +#' +#' width in pixels of the lines +#' +#' @export +#' +#' @rdname addLegendSize +addLegendLine <- function(map, + pal, + values, + title = NULL, + labelStyle = '', + orientation = c('vertical', 'horizontal'), + width = 20, + color, + opacity = 1, + fillOpacity = opacity, + breaks = 5, + baseSize = 10, + numberFormat = function(x) {prettyNum(x, big.mark = ',', scientific = FALSE, digits = 1)}, + group = NULL, + className = 'info legend leaflet-control', + ...) { + shape <- 'rect' + sizes <- sizeBreaks(values, breaks, baseSize) + if ( missing(color) ) { + stopifnot( missing(color) & !missing(pal)) + colors <- pal(as.numeric(names(sizes))) + } else { + stopifnot(length(color) == 1 || length(color) == length(breaks)) + colors <- color + } + symbols <- Map(makeSymbol, + shape = 'rect', + width = width, + height = sizes, + color = 'transparent', + fillColor = colors, + opacity = opacity, + fillOpacity = fillOpacity, + `stroke-width` = 0) + addLegendImage(map, images = symbols, labels = numberFormat(as.numeric(names(sizes))), + title = title, labelStyle = labelStyle, + orientation = orientation, width = width, height = sizes, + group = group, className = className, ...) + +} #' Add a legend with Awesome Icons #' diff --git a/man/addLegendSize.Rd b/man/addLegendSize.Rd index ab9f3a9..b20566a 100644 --- a/man/addLegendSize.Rd +++ b/man/addLegendSize.Rd @@ -5,7 +5,8 @@ \alias{sizeNumeric} \alias{sizeBreaks} \alias{makeSizeIcons} -\title{Add a legend that for the sizing of symbols} +\alias{addLegendLine} +\title{Add a legend that for the sizing of symbols or the width of lines} \usage{ addLegendSize( map, @@ -48,6 +49,26 @@ makeSizeIcons( baseSize, ... ) + +addLegendLine( + map, + pal, + values, + title = NULL, + labelStyle = "", + orientation = c("vertical", "horizontal"), + width = 20, + color, + opacity = 1, + fillOpacity = opacity, + breaks = 5, + baseSize = 10, + numberFormat = function(x) { prettyNum(x, big.mark = ",", scientific = FALSE, + digits = 1) }, + group = NULL, + className = "info legend leaflet-control", + ... +) } \arguments{ \item{map}{a map widget object created from 'leaflet'} @@ -96,12 +117,14 @@ be this exact size} \link[leaflegend]{makeSymbol} for makeSizeIcons} \item{colorValues}{the values used to generate color from the palette function} + +\item{width}{width in pixels of the lines} } \value{ an object from \link[leaflet]{addControl} } \description{ -Add a legend that for the sizing of symbols +Add a legend that for the sizing of symbols or the width of lines } \examples{ library(leaflet) @@ -175,4 +198,23 @@ leaflet() \%>\% 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) } From 0a358c2a4f09bd842ad4c5b18adc37b356650ade Mon Sep 17 00:00:00 2001 From: Thomas Roh Date: Mon, 17 Jan 2022 07:30:12 -0800 Subject: [PATCH 2/5] spell check --- NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 00ecde2..66580bf 100644 --- a/NEWS.md +++ b/NEWS.md @@ -46,7 +46,7 @@ legends * new functions `addLegendSize`, `sizeNumeric`, and `sizeBreaks` were added to allow encoding size on symbols. -* `addLegendImage` supports multiple height and width paramaters for images where you want different sizes +* `addLegendImage` supports multiple height and width parameters for images where you want different sizes * `addLegendImage` now supports using an svg URI from the output of `makeSymbol`. To supply a custom svg URI, add the 'svgURI' class to the character vector. From b0a9905f5023195650e2b07b0e1af4370fa3e059 Mon Sep 17 00:00:00 2001 From: Thomas Roh Date: Mon, 17 Jan 2022 07:32:11 -0800 Subject: [PATCH 3/5] replacing pipes --- R/legend.R | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/R/legend.R b/R/legend.R index 7526114..c78821a 100644 --- a/R/legend.R +++ b/R/legend.R @@ -1145,17 +1145,17 @@ addLegendFactor <- function(map, #' baseSize <- 10 #' lineColor <- '#00000080' #' pal <- colorNumeric('Reds', atlStorms2005$MinPress) -#' leaflet() |> -#' addTiles() |> +#' leaflet() %>% +#' addTiles() %>% #' addPolylines(data = atlStorms2005, #' weight = ~sizeNumeric(values = MaxWind, baseSize = baseSize), #' color = ~pal(MinPress), -#' popup = ~as.character(MaxWind)) |> +#' popup = ~as.character(MaxWind)) %>% #' addLegendLine(values = atlStorms2005$MaxWind, #' title = 'MaxWind', #' baseSize = baseSize, #' width = 50, -#' color = lineColor) |> +#' color = lineColor) %>% #' addLegendNumeric(pal = pal, #' title = 'MinPress', #' values = atlStorms2005$MinPress) @@ -1397,17 +1397,17 @@ addLegendLine <- function(map, #' spin = TRUE, #' squareMarker = FALSE) #' ) -#' leaflet(quakes[1:3,]) |> -#' addTiles() |> +#' leaflet(quakes[1:3,]) %>% +#' addTiles() %>% #' addAwesomeMarkers(lat = ~lat, #' lng = ~long, -#' icon = iconSet) |> +#' icon = iconSet) %>% #' addLegendAwesomeIcon(iconSet = iconSet, #' orientation = 'horizontal', #' title = htmltools::tags$div( #' style = 'font-size: 20px;', #' 'Awesome Icons'), -#' labelStyle = 'font-size: 16px;') |> +#' labelStyle = 'font-size: 16px;') %>% #' addLegendAwesomeIcon(iconSet = iconSet, #' orientation = 'vertical', #' marker = FALSE, From fb7e3fa55dc35c7bf82e137738eab541bafdec7d Mon Sep 17 00:00:00 2001 From: Thomas Roh Date: Mon, 17 Jan 2022 07:40:44 -0800 Subject: [PATCH 4/5] adding site rebuild --- docs/404.html | 2 +- docs/LICENSE-text.html | 2 +- docs/LICENSE.html | 2 +- docs/authors.html | 6 +-- docs/index.html | 2 +- docs/news/index.html | 8 +-- docs/pkgdown.yml | 2 +- docs/reference/addLeafLegends.html | 22 ++++----- docs/reference/addLegendAwesomeIcon.html | 6 +-- docs/reference/addLegendImage.html | 10 ++-- docs/reference/addLegendSize.html | 63 ++++++++++++++++++++---- docs/reference/index.html | 6 +-- docs/reference/makeSymbol.html | 2 +- docs/search.json | 2 +- 14 files changed, 90 insertions(+), 45 deletions(-) diff --git a/docs/404.html b/docs/404.html index ef12394..c42028a 100644 --- a/docs/404.html +++ b/docs/404.html @@ -40,7 +40,7 @@ leaflegend - 0.5.0 + 0.6.0