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

Adding argument to change the 'between' in legends #73

Closed CIOData closed 11 months ago

CIOData commented 1 year ago

In leaflet there is a function labelFormat() that allows users to change the separator in legends using the between argument. I cannot seem to get this same functionality to work with leaflegend. Would it be possible to add that feature? Thanks.

tomroh commented 1 year ago

Hey @CIOData,

I'm happy to take a look. Can you provide a reproducible example with code?

CIOData commented 1 year ago

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"))
tomroh commented 1 year ago

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"))
CIOData commented 1 year ago

Thanks. I had to hack this a little more because I'm using it in a dynamic environment, but it works.

tomroh commented 11 months ago

8aea4af23dc7b97d24ac9440148fea55e61e48e5