Coder Social home page Coder Social logo

Comments (5)

tomroh avatar tomroh commented on July 22, 2024

Hey @CIOData,

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

from leaflegend.

CIOData avatar CIOData commented on July 22, 2024

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"))

from leaflegend.

tomroh avatar tomroh commented on July 22, 2024

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"))

from leaflegend.

CIOData avatar CIOData commented on July 22, 2024

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

from leaflegend.

tomroh avatar tomroh commented on July 22, 2024

8aea4af

from leaflegend.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.