Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
CIOData opened this issue Aug 28, 2023 · 5 comments
Closed

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

CIOData opened this issue Aug 28, 2023 · 5 comments

Comments

@CIOData
Copy link

CIOData commented Aug 28, 2023

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
Copy link
Owner

tomroh commented Aug 28, 2023

Hey @CIOData,

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

@CIOData
Copy link
Author

CIOData commented Aug 28, 2023

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
Copy link
Owner

tomroh commented Aug 28, 2023

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
Copy link
Author

CIOData commented Sep 1, 2023

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

@tomroh tomroh closed this as completed Sep 15, 2023
@tomroh tomroh reopened this Sep 15, 2023
@tomroh
Copy link
Owner

tomroh commented Sep 15, 2023

8aea4af

@tomroh tomroh closed this as completed Sep 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants