Skip to content

Commit

Permalink
Merge pull request #40 from tomroh/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
tomroh authored Oct 19, 2021
2 parents 1864173 + ae13160 commit 1fbea7c
Show file tree
Hide file tree
Showing 9 changed files with 344 additions and 58 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ inst/examples/examples.R
^_pkgdown\.yml$
^docs$
^pkgdown$
^FUNDING.yml$
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")))
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(addLegendAwesomeIcon)
export(addLegendBin)
export(addLegendFactor)
export(addLegendImage)
Expand Down
99 changes: 53 additions & 46 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
169 changes: 165 additions & 4 deletions R/legend.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <-
Expand Down Expand Up @@ -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
#'
Expand All @@ -369,7 +402,9 @@ makeSymbolIcons <- function(shape = c('rect',
'cross',
'diamond',
'star',
'stadium'),
'stadium',
'line',
'polygon'),
color,
fillColor = color,
opacity,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,45 @@ leaflet() %>%
)
```




## Map Symbols

<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Crect%20id%3D%22rect%22%20x%3D%222%22%20y%3D%222%22%20height%3D%2250%22%20width%3D%2250%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Frect%3E%0A%3C%2Fsvg%3E" alt="rect" width="50" height="50" title="rect"/>
<p style="text-align: center;">rect</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Ccircle%20id%3D%22circle%22%20cx%3D%2227%22%20cy%3D%2227%22%20r%3D%2225%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Fcircle%3E%0A%3C%2Fsvg%3E" alt="circle" width="50" height="50" title="circle"/>
<p style="text-align: center;">circle</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Cpolygon%20id%3D%22triangle%22%20points%3D%222%2C52%2052%2C52%2027%2C2%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E" alt="triangle" width="50" height="50" title="triangle"/>
<p style="text-align: center;">triangle</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Cpolygon%20id%3D%22plus%22%20points%3D%2222%2C2%2022%2C22%202%2C22%202%2C32%2022%2C32%2022%2C52%2032%2C52%2032%2C32%2052%2C32%2052%2C22%2032%2C22%2032%2C2%2022%2C2%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E" alt="plus" width="50" height="50" title="plus"/>
<p style="text-align: center;">plus</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Cpolygon%20id%3D%22cross%22%20points%3D%229.07106781186548%2C2%202%2C9.07106781186548%2019.9289321881345%2C27%202%2C44.9289321881345%209.07106781186548%2C52%2027%2C34.0710678118655%2044.9289321881345%2C52%2052%2C44.9289321881345%2034.0710678118655%2C27%2052%2C9.07106781186548%2044.9289321881345%2C2%2027%2C19.9289321881345%209.07106781186548%2C2%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E" alt="cross" width="50" height="50" title="cross"/>
<p style="text-align: center;">cross</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Cpolygon%20id%3D%22diamond%22%20points%3D%2227%2C2%202%2C27%2027%2C52%2052%2C27%2027%2C2%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E" alt="diamond" width="50" height="50" title="diamond"/>
<p style="text-align: center;">diamond</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Cpolygon%20id%3D%22star%22%20points%3D%2222%2C2%2022%2C14.92893%209.07107%2C2%202%2C9.07107%2014.92893%2C22%202%2C22%202%2C32%2014.92893%2C32%202%2C44.92893%209.07107%2C52%2022%2C39.07107%2022%2C52%2032%2C52%2032%2C39.07107%2044.92893%2C52%2052%2C44.92893%2039.07107%2C32%2052%2C32%2052%2C22%2039.07107%2C22%2052%2C9.07107%2044.92893%2C2%2032%2C14.92893%2032%2C2%2022%2C2%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E" alt="star" width="50" height="50" title="star"/>
<p style="text-align: center;">star</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Crect%20id%3D%22stadium%22%20x%3D%222%22%20y%3D%222%22%20height%3D%2250%22%20width%3D%2250%22%20rx%3D%2225%25%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Frect%3E%0A%3C%2Fsvg%3E" alt="stadium" width="50" height="50" title="stadium"/>
<p style="text-align: center;">stadium</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Cline%20id%3D%22line%22%20x1%3D%222%22%20x2%3D%2252%22%20y1%3D%2227%22%20y2%3D%2227%22%20stroke%3D%22black%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Fline%3E%0A%3C%2Fsvg%3E" alt="line" width="50" height="50" title="line"/>
<p style="text-align: center;">line</p>
</div>
<div style="width: 50px; display: inline-block;">
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20version%3D%221.1%22%20width%3D%2254%22%20height%3D%2254%22%3E%0A%20%20%3Cpolygon%20id%3D%22polygon%22%20points%3D%2227%2C2%203.22358709262116%2C19.2745751406263%2012.3053686926882%2C47.2254248593737%2041.6946313073118%2C47.2254248593737%2050.7764129073788%2C19.2745751406263%2027%2C2%22%20stroke%3D%22black%22%20fill%3D%22transparent%22%20stroke-opacity%3D%221%22%20fill-opacity%3D%221%22%20stroke-width%3D%222%22%3E%3C%2Fpolygon%3E%0A%3C%2Fsvg%3E" alt="polygon" width="50" height="50" title="polygon"/>
<p style="text-align: center;">polygon</p>
</div>
Loading

0 comments on commit 1fbea7c

Please sign in to comment.