SymbolixAU / mapdeck

R interface to Deck.gl and Mapbox
https://symbolixau.github.io/mapdeck/articles/mapdeck.html
362 stars 40 forks source link

Legend values when using manual colors #152

Closed victorhdc closed 5 years ago

victorhdc commented 5 years ago

Is there a way to specify a character column for legend values, when using manual hex string colours? Using the manual colours example: https://symbolixau.github.io/mapdeck/articles/colours.html#manual-colours

sf$my_colour <- ifelse( substr(sf$SA2_NAME, 1, 1) == "A", "#00FF00FF", "#FF0000FF")
mapdeck( ) %>%
add_polygon(
data = sf
, fill_colour = "my_colour"
)

When a legend is included, the legend values are the hex color strings. How can I use a different column for legend values?


TODO

SymbolixAU commented 5 years ago

This is currently not possible, but I've wanted to do something about this for a while. I'll have a think about possible solutions and update this issue if I come up with something.

SymbolixAU commented 5 years ago

@victorhdc Could you please try the issue152 branch and let me know what you think?

devtools::install_github("SymbolixAU/mapdeck", ref = "issue152")

I've created two new functions

  1. legend_element() - for creating specific legend elements
  2. mapdeck_legend() - for formatting the legend_elements ready for the map

Example

sf <- spatialwidget::widget_melbourne
sf$my_colour <- ifelse( substr(sf$SA2_NAME, 1, 1) == "A", "#00FF00FF", "#FF0000FF")

l1 <- legend_element(
    variables = c("begins with A", "Doesn't begin with A")
    , colours = c("#00FF00FF", "#FF0000FF")
    , colour_type = "fill"
    , variable_type = "category"
)
js <- mapdeck_legend(l1)
js
# {"fill_colour":{"colour":["#00FF00FF","#FF0000FF"],"variable":["begins with A","Doesn't begin with A"],"colourType":["fill_colour"],"type":["category"],"title":[""],"css":[""]}}
mapdeck( ) %>%
    add_polygon(
        data = sf
        , fill_colour = "my_colour"
        , legend = js
    )

Screen Shot 2019-04-04 at 8 27 11 am

victorhdc commented 5 years ago

Works exactly as needed, thank you very much. I think it was a great idea to separate the logical legend definition from the json object.

SymbolixAU commented 5 years ago

reopening so it reminds me to document, test and merge to master

I think it was a great idea to separate the logical legend definition from the json object.

yeah it was needed because there can be a different legends for different colouring options (fill, stroke), so I thought making individual elements first would be easier to handle in code.