diff --git a/.Rbuildignore b/.Rbuildignore index 8e1c9d0..e231712 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -10,3 +10,4 @@ inst/examples/examples.R ^_pkgdown\.yml$ ^docs$ ^pkgdown$ +^FUNDING.yml$ diff --git a/DESCRIPTION b/DESCRIPTION index 624ceb9..750848b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: leaflegend Type: Package Title: Add Custom Legends to 'leaflet' Maps -Version: 0.4.0 +Version: 0.5.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 b4f9b2e..ea3de91 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(addLegendAwesomeIcon) export(addLegendBin) export(addLegendFactor) export(addLegendImage) diff --git a/NEWS.md b/NEWS.md index 41e29bd..0488fed 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,46 +1,53 @@ -# leaflegend 0.4.0 - -* fixes error on makeSizeLegend where fillColor is not evaluated if no argument is provided - -* adding colorValues as an argument to makeSizeLegend so that symbols can be sized and colored on different vectors of data - -* fixes warning on addLegendNumeric where the shape default was a vector not a single value - -* adding number formatting to addSizeLegend - -* adding group layer support for legends. Use addLayersControl to turn on/off -legends - -* added example for using raster images with size encodings based on data - - -# leaflegend 0.3.0 - -* stroke outlines of shapes are now padded so that the stroke is not cut off - -* numeric legends now have appropriate sizing for text - -* star symbol outline has been fixed - -* new function `makeSizeIcons` as a convenience wrapper size scaled symbols - -# leaflegend 0.2.0 - -* 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` 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. - -* added more shapes to `makeSymbol`, `addLegendNumeric`, -`addLegendQuantile`, `addLegendFactor`, `addLegendBin` - -* Control the opacity of the legend shapes for `addLegendNumeric`, -`addLegendQuantile`, `addLegendFactor`, `addLegendBin` - -* `makeSymbol` now returns embeddable svg - -# leaflegend 0.1.0 - -* Added a `NEWS.md` file to track changes to the package. +# leaflegend 0.5.0 + +* adding addLegendAwesomeIcon function to produce legends with markers from +awesome icon libraries + +* adding in line and polygon symbol and adding symbols to the README + +# leaflegend 0.4.0 + +* fixes error on makeSizeLegend where fillColor is not evaluated if no argument is provided + +* adding colorValues as an argument to makeSizeLegend so that symbols can be sized and colored on different vectors of data + +* fixes warning on addLegendNumeric where the shape default was a vector not a single value + +* adding number formatting to addSizeLegend + +* adding group layer support for legends. Use addLayersControl to turn on/off +legends + +* added example for using raster images with size encodings based on data + + +# leaflegend 0.3.0 + +* stroke outlines of shapes are now padded so that the stroke is not cut off + +* numeric legends now have appropriate sizing for text + +* star symbol outline has been fixed + +* new function `makeSizeIcons` as a convenience wrapper size scaled symbols + +# leaflegend 0.2.0 + +* 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` 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. + +* added more shapes to `makeSymbol`, `addLegendNumeric`, +`addLegendQuantile`, `addLegendFactor`, `addLegendBin` + +* Control the opacity of the legend shapes for `addLegendNumeric`, +`addLegendQuantile`, `addLegendFactor`, `addLegendBin` + +* `makeSymbol` now returns embeddable svg + +# leaflegend 0.1.0 + +* Added a `NEWS.md` file to track changes to the package. diff --git a/R/legend.R b/R/legend.R index c0d6ae9..0e2e3c1 100644 --- a/R/legend.R +++ b/R/legend.R @@ -311,6 +311,26 @@ makeSymbol <- function(shape, width, height = width, color, fillColor = color, 'fill-opacity' = fillOpacity, ... ), + 'line' = htmltools::tags$line( + id = 'line', + x1 = 0 + strokewidth, + x2 = width + strokewidth, + y1 = height / 2 + strokewidth, + y2 = height / 2 + strokewidth, + stroke = color, + 'stroke-opacity' = opacity, + 'fill-opacity' = fillOpacity, + ... + ), + 'polygon' = htmltools::tags$polygon( + id = 'polygon', + points = draw_polygon(n = 5, width = width, height = height, offset = strokewidth), + stroke = color, + fill = fillColor, + 'stroke-opacity' = opacity, + 'fill-opacity' = fillOpacity, + ... + ), stop('Invalid shape argument.') ) svgURI <- @@ -358,6 +378,19 @@ draw_star <- function(width, height, offset = 0) { y <- height * c(0,0.2585786,0,0.1414214,0.4,0.4,0.6,0.6,0.8585786,1,0.7414214,1,1,0.7414214,1,0.8585786,0.6,0.6,0.4,0.4,0.1414214,0,0.2585786,0,0) + offset paste(x, y, sep = ',', collapse = ' ') } +draw_polygon <- function(n, width = 1, height = 1, offset = 0) { + stopifnot(n > 0 || !is.integer(n)) + radians <- seq(-pi, pi, by = 2*pi/n) + if ( n %% 2 == 0 ) { + x <- (cos(radians) + 1) * 1/2 * width + offset + y <- (sin(radians) + 1) * 1/2 * height + offset + } else { + radians <- seq(-pi, pi, by = 2*pi/n) + x <- (sin(radians) + 1) * 1/2 * width + offset + y <- (cos(radians) + 1) * 1/2 * height + offset + } + paste(x, y, sep = ',', collapse = ' ') +} #' @export #' @@ -369,7 +402,9 @@ makeSymbolIcons <- function(shape = c('rect', 'cross', 'diamond', 'star', - 'stadium'), + 'stadium', + 'line', + 'polygon'), color, fillColor = color, opacity, @@ -799,7 +834,7 @@ addLegendQuantile <- function(map, values, title = NULL, labelStyle = '', - shape = c('rect', 'circle', 'triangle', 'plus', 'cross', 'diamond', 'star', 'stadium'), + shape = c('rect', 'circle', 'triangle', 'plus', 'cross', 'diamond', 'star', 'stadium', 'line', 'polygon'), orientation = c('vertical', 'horizontal'), width = 24, height = 24, @@ -859,7 +894,7 @@ addLegendBin <- function(map, values, title = NULL, labelStyle = '', - shape = c('rect', 'circle', 'triangle', 'plus', 'cross', 'diamond', 'star', 'stadium'), + shape = c('rect', 'circle', 'triangle', 'plus', 'cross', 'diamond', 'star', 'stadium', 'line', 'polygon'), orientation = c('vertical', 'horizontal'), width = 24, height = 24, @@ -903,7 +938,7 @@ addLegendFactor <- function(map, values, title = NULL, labelStyle = '', - shape = c('rect', 'circle', 'triangle', 'plus', 'cross', 'diamond', 'star', 'stadium'), + shape = c('rect', 'circle', 'triangle', 'plus', 'cross', 'diamond', 'star', 'stadium', 'line', 'polygon'), orientation = c('vertical', 'horizontal'), width = 24, height = 24, @@ -1230,6 +1265,132 @@ makeSizeIcons <- function(values, ) } +#' Add a legend with Awesome Icons +#' +#' @param map +#' +#' a map widget object created from 'leaflet' +#' +#' @param iconSet +#' +#' a named list from \link[leaflet]{awesomeIconList}, the names will be the labels in the legend +#' +#' @param title +#' +#' the legend title, pass in HTML to style +#' +#' @param labelStyle +#' +#' character string of style argument for HTML text +#' +#' @param orientation +#' +#' stack the legend items vertically or horizontally +#' +#' @param group +#' +#' group name of a leaflet layer group +#' +#' @param className +#' +#' extra CSS class to append to the control, space separated +#' +#' @param ... +#' +#' arguments to pass to \link[leaflet]{addControl} +#' +#' @return +#' +#' an object from \link[leaflet]{addControl} +#' +#' @export +#' +#' @examples +#' library(leaflet) +#' data(quakes) +#' iconSet <- awesomeIconList( +#' `Font Awesome` = makeAwesomeIcon(icon = "font-awesome", library = "fa", +#' iconColor = 'gold', markerColor = 'red', +#' spin = FALSE, +#' squareMarker = TRUE, +#' iconRotate = 30, +#' ), +#' Ionic = makeAwesomeIcon(icon = "ionic", library = "ion", +#' iconColor = '#fffff', markerColor = 'blue', +#' spin = TRUE, +#' squareMarker = FALSE), +#' Glyphicon = makeAwesomeIcon(icon = "plus-sign", library = "glyphicon", +#' iconColor = 'rgb(192, 255, 0)', markerColor = 'darkpurple', +#' spin = TRUE, +#' squareMarker = FALSE) +#' ) +#' leaflet(quakes[1:3,]) |> +#' addTiles() |> +#' addAwesomeMarkers(lat = ~lat, +#' lng = ~long, +#' icon = iconSet) |> +#' addLegendAwesomeIcon(iconSet = iconSet, +#' orientation = 'horizontal', +#' title = htmltools::tags$div( +#' style = 'font-size: 20px;', +#' 'Awesome Icons'), +#' labelStyle = 'font-size: 16px;') |> +#' addLegendAwesomeIcon(iconSet = iconSet, +#' orientation = 'vertical', +#' title = htmltools::tags$div( +#' style = 'font-size: 20px;', +#' 'Awesome Icons'), +#' labelStyle = 'font-size: 16px;') +addLegendAwesomeIcon <- function(map, + iconSet, + title = NULL, + labelStyle = '', + orientation = c('vertical', 'horizontal'), + group = NULL, + className = 'info legend leaflet-control', + ...) { + stopifnot(inherits(iconSet, 'leaflet_awesome_icon_set')) + orientation <- match.arg(orientation) + if ( orientation == 'vertical' ) { + wrapElements <- htmltools::tags$div + } else { + wrapElements <- htmltools::tags$span + } + + htmlElements <- + Map(icon = iconSet, + label = names(iconSet), + f = function(icon, label) { + htmltools::tagList( + wrapElements( + htmltools::tags$div(style = 'vertical-align: middle; display: inline-block; position: relative;', + class = + sprintf('awesome-marker-icon-%s awesome-marker %s', + icon[['markerColor']], + ifelse(icon[['squareMarker']], 'awesome-marker-square', '')), + htmltools::tags$i(class = sprintf('%1$s %1$s-%2$s %3$s', + icon[['library']], + icon[['icon']], + ifelse(icon[['spin']], 'fa-spin', '')), + style = sprintf('color: %s; %s', icon[['iconColor']], + ifelse(icon[['iconRotate']] == 0, '', + sprintf('-webkit-transform: rotate(%1$sdeg);-moz-transform: rotate(%1$sdeg);-o-transform: rotate(%1$sdeg);-ms-transform: rotate(%1$sdeg);transform: rotate(%1$sdeg);', + icon[['iconRotate']])) + ) + )#, + #htmltools::tags$span(label, style = sprintf("vertical-align: middle; padding: 1px; %s", 'labelStyle')) + ), + htmltools::tags$span(label, style = sprintf('%s', labelStyle)) + ) + ) + }) + if (!is.null(title)) { + htmlElements <- + append(htmlElements, list(htmltools::div(htmltools::tags$strong(title))), after = 0) + } + leaflegendAddControl(map, html = htmltools::tagList(htmlElements), className = className, group = group, ...) +} + leaflegendAddControl <- function(map, html, className, diff --git a/README.md b/README.md index 4fb684c..71d5f57 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,45 @@ leaflet() %>% ) ``` - - - +## Map Symbols + +
+ rect +

rect

+
+
+ circle +

circle

+
+
+ triangle +

triangle

+
+
+ plus +

plus

+
+
+ cross +

cross

+
+
+ diamond +

diamond

+
+
+ star +

star

+
+
+ stadium +

stadium

+
+
+ line +

line

+
+
+ polygon +

polygon

+
diff --git a/man/addLeafLegends.Rd b/man/addLeafLegends.Rd index b8db509..4a2e305 100644 --- a/man/addLeafLegends.Rd +++ b/man/addLeafLegends.Rd @@ -36,7 +36,7 @@ addLegendQuantile( title = NULL, labelStyle = "", shape = c("rect", "circle", "triangle", "plus", "cross", "diamond", "star", - "stadium"), + "stadium", "line", "polygon"), orientation = c("vertical", "horizontal"), width = 24, height = 24, @@ -56,7 +56,7 @@ addLegendBin( title = NULL, labelStyle = "", shape = c("rect", "circle", "triangle", "plus", "cross", "diamond", "star", - "stadium"), + "stadium", "line", "polygon"), orientation = c("vertical", "horizontal"), width = 24, height = 24, @@ -74,7 +74,7 @@ addLegendFactor( title = NULL, labelStyle = "", shape = c("rect", "circle", "triangle", "plus", "cross", "diamond", "star", - "stadium"), + "stadium", "line", "polygon"), orientation = c("vertical", "horizontal"), width = 24, height = 24, diff --git a/man/addLegendAwesomeIcon.Rd b/man/addLegendAwesomeIcon.Rd new file mode 100644 index 0000000..f5fb344 --- /dev/null +++ b/man/addLegendAwesomeIcon.Rd @@ -0,0 +1,77 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/legend.R +\name{addLegendAwesomeIcon} +\alias{addLegendAwesomeIcon} +\title{Add a legend with Awesome Icons} +\usage{ +addLegendAwesomeIcon( + map, + iconSet, + title = NULL, + labelStyle = "", + orientation = c("vertical", "horizontal"), + group = NULL, + className = "info legend leaflet-control", + ... +) +} +\arguments{ +\item{map}{a map widget object created from 'leaflet'} + +\item{iconSet}{a named list from \link[leaflet]{awesomeIconList}, the names will be the labels in the legend} + +\item{title}{the legend title, pass in HTML to style} + +\item{labelStyle}{character string of style argument for HTML text} + +\item{orientation}{stack the legend items vertically or horizontally} + +\item{group}{group name of a leaflet layer group} + +\item{className}{extra CSS class to append to the control, space separated} + +\item{...}{arguments to pass to \link[leaflet]{addControl}} +} +\value{ +an object from \link[leaflet]{addControl} +} +\description{ +Add a legend with Awesome Icons +} +\examples{ +library(leaflet) +data(quakes) +iconSet <- awesomeIconList( + `Font Awesome` = makeAwesomeIcon(icon = "font-awesome", library = "fa", + iconColor = 'gold', markerColor = 'red', + spin = FALSE, + squareMarker = TRUE, + iconRotate = 30, + ), + Ionic = makeAwesomeIcon(icon = "ionic", library = "ion", + iconColor = '#fffff', markerColor = 'blue', + spin = TRUE, + squareMarker = FALSE), + Glyphicon = makeAwesomeIcon(icon = "plus-sign", library = "glyphicon", + iconColor = 'rgb(192, 255, 0)', markerColor = 'darkpurple', + spin = TRUE, + squareMarker = FALSE) +) +leaflet(quakes[1:3,]) |> + addTiles() |> + addAwesomeMarkers(lat = ~lat, + lng = ~long, + icon = iconSet) |> + addLegendAwesomeIcon(iconSet = iconSet, + orientation = 'horizontal', + title = htmltools::tags$div( + style = 'font-size: 20px;', + 'Awesome Icons'), + labelStyle = 'font-size: 16px;') |> + addLegendAwesomeIcon(iconSet = iconSet, + orientation = 'vertical', + title = htmltools::tags$div( + style = 'font-size: 20px;', + 'Awesome Icons'), + labelStyle = 'font-size: 16px;') +} diff --git a/man/makeSymbol.Rd b/man/makeSymbol.Rd index a840e61..f0601da 100644 --- a/man/makeSymbol.Rd +++ b/man/makeSymbol.Rd @@ -18,7 +18,7 @@ makeSymbol( makeSymbolIcons( shape = c("rect", "circle", "triangle", "plus", "cross", "diamond", "star", - "stadium"), + "stadium", "line", "polygon"), color, fillColor = color, opacity,