Closed CIOData closed 11 months ago
Hey @CIOData,
I'm happy to take a look. Can you provide a reproducible example with code?
Sure. Here is a simple example that shows the base functionality of addLegend
in leaflet, followed by two examples of using labelFormat()
, followed by the base functionality of using leaflegend.
library(tidyverse)
library(leaflet)
library(leaflegend)
df<-data.frame(x=rnorm(100),y=rexp(100,2),z=runif(100))
pal<-colorBin("PuOr",df$z,bins=c(0,.1,.4,.9,1))
#base functionality
leaflet(df) %>%
addTiles() %>%
addCircleMarkers(~x,~y,color=~pal(z),group="circles") %>%
addLegend(pal=pal,values=~z,group="circles",position="bottomleft") %>%
addLayersControl(overlayGroups=c("circles"))
#change separator to " to "
leaflet(df) %>%
addTiles() %>%
addCircleMarkers(~x,~y,color=~pal(z),group="circles") %>%
addLegend(pal=pal,values=~z,
labFormat=labelFormat(between=" to "),
group="circles",position="bottomleft") %>%
addLayersControl(overlayGroups=c("circles"))
#display as an interval of percentages with percent sign
leaflet(df) %>%
addTiles() %>%
addCircleMarkers(~x,~y,color=~pal(z),group="circles") %>%
addLegend(pal=pal,values=~z,
labFormat=labelFormat(
prefix="(",suffix=")%",between=",",
transform=function(x)100*x
),
group="circles",position="bottomleft") %>%
addLayersControl(overlayGroups=c("circles"))
#use leaflegend
leaflet(df) %>%
addTiles() %>%
addCircleMarkers(~x,~y,color=~pal(z),group="circles") %>%
addLegendBin(pal=pal, values = ~z, position = 'bottomleft') %>%
addLayersControl(overlayGroups=c("circles"))
This feature might be added in the future but currently the way to do it is:
breaks <- c(0,.1,.4,.9,1)
df$bins <- cut(df$z, breaks, paste(head(breaks, 4), tail(breaks,4), sep = ' to '))
pal <- colorFactor('PuOr', df$bins)
leaflet(df) %>%
addTiles() %>%
addCircleMarkers(~x,~y,color=~pal(bins),group="circles") %>%
addLegendFactor(pal=pal, values = ~bins, position = 'bottomleft', ) %>%
addLayersControl(overlayGroups=c("circles"))
Thanks. I had to hack this a little more because I'm using it in a dynamic environment, but it works.
8aea4af23dc7b97d24ac9440148fea55e61e48e5
In leaflet there is a function
labelFormat()
that allows users to change the separator in legends using thebetween
argument. I cannot seem to get this same functionality to work with leaflegend. Would it be possible to add that feature? Thanks.