tomroh / leaflegend

Provides extensions to the leaflet R package to customize legends with images, text styling, orientation, sizing, and symbology.
https://leaflegend.delveds.com
Other
34 stars 3 forks source link

support for dashed lines in leaflet legend #74

Closed essair closed 11 months ago

essair commented 1 year ago

This is an awesome package and makes leaflet maps just so much better. I'm having trouble showing solid and dashed lines in my legend. Is there any chance of some support regarding how to get that into the legend?

tomroh commented 1 year ago

I can add this in. Can you provide a reproducible example for me to work with?

essair commented 12 months ago

Thats great, here is a very simplified version of my map:

Mini_map_example.zip

tomroh commented 11 months ago

959462757a93839e12884085b89a5c6735991e8c

library(leaflet)
lineData <- sf::st_as_sf(
  data.frame(
    type = c(1,2),
    wkt = c(
      "LINESTRING (7.964056 47.98372, 7.968604 47.98427, 7.971705 47.98565, 7.972738 47.98704, 7.972738 47.9887, 7.972945 47.99174, 7.975426 47.99271, 7.975839 47.99714, 7.974392 47.99852, 7.975426 48.00032, 7.97894 48.00184, 7.982868 48.00364, 7.987002 48.00391, 7.99155 48.00391, 7.994651 48.0035)",
      "LINESTRING (7.973152 47.98026, 7.982247 47.98233, 7.987002 47.98303, 7.99031 47.98469, 7.995478 47.98538, 7.998578 47.98538, 8.000646 47.98731)"
    )), crs = 4326, wkt = 'wkt'
)
colors <- c("#F5B400", "#188FA7")
colour_palette <- colorFactor(palette = colors, domain = c("1", "2"), ordered=TRUE)
linetype_differentiation <- function(i){
  unname(c("1" = "1", "2" = "12 12")[i])
}
leaflet(lineData) |> 
  leaflet::addTiles() |> 
  addPolylines( 
    col = ~ colour_palette(type),
    dashArray = ~linetype_differentiation(type)) |> 
  addLegendSymbol(color = colors, shape = rep('line', 2), values = ~type,
    strokeWidth = 5,
    dashArray = list("none", "12 12"), width = 100
    )