Leaflet (addLegend) don't show color scale with comma as decimal mark

42 views Asked by At

I need to print a PDF report with Rmarkdown, which presents decimals with comma separator (e.g. 2,5; 0,0123; etc,). Using options(OutDec = ","), I get all my decimal numeric output to be presented with comma: inline text, graphs, tables and the values presented in maps using leaflet.

However, the leaflet legend is not configured and the color scale does not appear.

I have not been able to configure leaflet to show the legend with the color scale and the numbers with decimal separator comma.

This is an code example:

library(tidyverse)
library(tibble)
library(leaflet)
library(leaflegend)

id <- as.tibble(c("a", "b", "c", "d", "e", "f", "g", "h"))
lat <- as.tibble(c(45.72643, 45.72149, 45.74190, 45.74855, 45.79040, 45.77804, 45.78961, 45.80105))
long <- as.tibble(c(-71.41670, -71.49019 ,-71.48091 ,-71.47479, -71.50638, -71.53254, -71.53285, -71.53381))
val <- as.tibble(c(9.73, 3.44, 4.71, 7.78 ,8.30, 8.01, 8.58, 6.81))

valores <- bind_cols(id,lat,long,val) %>% rename(id = 1, lat = 2, long = 3, val = 4)

options(OutDec = ",")

pal <- colorNumeric(palette = "YlGnBu",
                    domain = valores$val)

leaflet(valores) %>%
  addTiles() %>%
  addCircleMarkers(~long, # Longitud
                   ~lat, # Latitud
                   radius = 10,
                   color = "black",
                   fillColor = ~ pal(val),
                   label = ~ val,
                   labelOptions = labelOptions(noHide = T, 
                                               textOnly = TRUE,
                                               style = list("color" = "black",
                                                            "font-family" = "arial",
                                                            "font-style" = "bold",
                                                            "font-size" = "15px",
                                                            "text-align" = "center"),
                                               offset = c(10,-8),
                                               direction = "right"),
                   stroke = TRUE,
                   fillOpacity = 0.9) %>% 
  addProviderTiles(providers$CartoDB.Positron,
                   options = providerTileOptions(opacity = 0.50)) %>%
  leaflet::addLegend("bottomright",
                     pal = pal, 
                     values = ~val,
                     title = "Mapa prueba",
                     labFormat = labelFormat(suffix = " [\u00b5g/m\u00b3]"),
                     opacity = 1)

Thank you.

I need to print a leaflet map with all my data and legends using a comma as decimal mark.

0

There are 0 answers