qfes / rdeck

Deck.gl widget for R
https://qfes.github.io/rdeck
MIT License
97 stars 0 forks source link

`get_fill_color` as a string doesn't generate a legend #96

Open MikeLydeamore opened 1 year ago

MikeLydeamore commented 1 year ago

Passing a string instead of an accessor into get_scatterplot_layer doesn't seem to produce a legend, even when visible = TRUE is enabled.

I can bypass this by creating a dummy column that is the same for every row in the object, and then using scale_color_* but this feels like a bit of a clunky workaround.

An example of the map I would expect to see a fill legend for:

library(rdeck)
library(dplyr)
library(sf)
library(absmapsdata)

shp <- sa32016 %>%
  filter(gcc_name_2016 == "Greater Melbourne") |>
  group_by(sa3_code_2016) |>
  mutate(
    centroid = st_centroid(geometry)
  )

rdeck(
  map_style = mapbox_dark(),
  # set the bounds of the map to include all of the manhattan data
  initial_bounds = st_bbox(shp),
  # add a 2 pixel buffer to each point, making it easier to hover
  picking_radius = 2
) |>
  add_polygon_layer(
    data = shp,
    get_polygon = geometry,
    filled = TRUE,
    opacity = 0.1,
    stroked = TRUE,
    get_line_color = "#663399ff",
    line_width_units = "pixels",
  ) |>
  add_scatterplot_layer(
    data = shp,
    get_position = centroid,
    get_radius = 200,
    get_fill_color = "#ffffff",
    visible = TRUE,
    radius_min_pixels = 5
  )

The workaround is to change get_fill_color to:

get_fill_color = cale_color_category(
        col = state_name_2016,
        palette = c("#ffffff")
      )

I understand the normal use case for a string in get_fill_color is probably a useless single-valued legend. My use case is controlling several very similar layers with shiny that have each dataset on it's own layer (for individual toggling) and so the ability to create a "manual" legend would be very useful.

anthonynorth commented 1 year ago

Ideally, I think what you would want to use is an in-javascript filter on a single layer? That's planned, but I don't have a timeframe for it yet.

Manual legends aren't totally useless and I agree that adding a dummy constant and using a category scale is clunky (it also adds bloat).

I think manual legends are related to aggregation legends (also not available yet) --- workaround there is to create a dummy layer, which is a hack.

I think something like this for the api for both could work:

color <- function(color, legend=TRUE) {}
color_range <- function(palette, legend=TRUE) {}