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

If I'm using the automatically generated fillcolor how do I add this pallet to the legend? #80

Open
vkentner opened this issue Dec 13, 2022 · 3 comments
Assignees

Comments

@vkentner
Copy link

I've looked everywhere for this answer and can't figure out a way to call the automatically generated color pallet in addGlPolygons fillcolor. I know I can define my own color pallet but what if I want to use the automatically generated color pallet with a legend?

  library(mapview)
  library(leaflet)
  library(leafgl)
  library(sf)
     
  fran = st_cast(franconia, "POLYGON")
  #> Warning in st_cast.sf(franconia, "POLYGON"): repeating attributes for all
  #> sub-geometries for which they may not be constant
  
  leaflet() %>%
    addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
    addGlPolygons(data = fran, fillColor ="SHAPE_AREA", popup = "NAME_ASCI") %>%
    setView(lng = 10.5, lat = 49.5, zoom = 8)%>%
    addLegend(colors="SHAPE_AREA", labels="SHAPE_AREA") #this is the issue
@tim-salabim
Copy link
Member

@trafficonese any ideas? You created all the color utils IIRC

@trafficonese
Copy link
Collaborator

trafficonese commented Dec 14, 2022

Hm one option would be to get the RGB colors with makeColorMatrix and convert them to Hex-values.

library(mapview)
library(leaflet)
library(leafgl)
library(sf)

fran = st_cast(franconia, "POLYGON")
fran$color <- sample(1:5, nrow(fran), replace = T)
colname <- "color"
# colname <- "district"
# colname <- "SHAPE_AREA"

fran <- fran[order(fran[[colname]], decreasing = T),]
colrgb <- leafgl::makeColorMatrix(x = colname, data = fran) * 255
cols <- apply(colrgb, 1, function(x)
  rgb(x[1], x[2], x[3], maxColorValue=255))

leaflet() %>%
  addProviderTiles(provider = providers$CartoDB.DarkMatter) %>%
  addGlPolygons(data = fran, fillColor = colname, popup = colname) %>%
  setView(lng = 10.5, lat = 49.5, zoom = 8) %>%
  addLegend(labels=unique(fran[[colname]]), opacity = 1,
            colors=unique(cols))

Another and simpler way would be to include a legend option in the add*-functions that will automatically create the legend for the user. For numeric columns, I would still recommend using a custom palette as each unique value gets its own color and the legend will be quite long and unreadable.

What do you think?

@vkentner
Copy link
Author

This is a great solution, Thanks for the help. For my use case I need to use a numeric color pallet so I ended up just defining a custom pallet which I call in the legend. This is an amazingly useful package and I appreciate all your hard work on it!

@trafficonese trafficonese self-assigned this May 29, 2024
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

3 participants