rdeck icon indicating copy to clipboard operation
rdeck copied to clipboard

Combining data fields in scale_color_

Open edmundseto opened this issue 5 months ago • 3 comments

Hi,

Thanks for developing this package. It's amazing! I'm new to this, so forgive me for probably a simple question. I've been looking for more examples, and haven't come across what I need.

I'm trying to add two numeric data fields from my vector tile, and use the results in the color scaling function for the map. The code below works with just one data field (DM_W), but not when I try to combine two fields (e.g., DM_W + DM_A) as shown. Any suggestions would be appreciated.

rdeck() |>
  add_mvt_layer(
    data = tile_json("https://{my server's IP}/services/simple_polygons"),
    get_fill_color = scale_color_linear(
      col = DM_W + DM_A
    ),
    pickable = TRUE,
    tooltip = c(GEOID10, DM_W, DM_A),
  ) 

I get an error...

Error in `add_mvt_layer()`:
! Failed to create layer
Caused by error in `rlang::ensym()`:
! Can't convert to a symbol.
Run `rlang::last_trace()` to see where the error occurred.

From the ensym() error, I think it's expecting just a column name rather than an expression, but my R skills aren't good enough to come up with a work around.

edmundseto avatar Jan 12 '24 19:01 edmundseto

Thanks for developing this package. It's amazing! Thanks!

What you're trying to do is not (yet) supported in {rdeck}. Scales and accessors work on a single column (or field for tiles). Are you able to modify the tileset to add an additional (redundant) field?

anthonynorth avatar Jan 19 '24 03:01 anthonynorth

Correct, single-column visualizations are working nicely!

But, for the tool I'm working on, I'd like to allow users to use a R Shiny interface to pick and choose columns to combine into their own composited variable for mapping -- like a dplry::mutate() statement after the accessor grabs the tiles within the viewing bounds and the vector attribute fields. Not sure I could pre-propulate the various combinations of the 50-some attribute columns, as I'd like to give the user's a lot of flexibility on how to do the compositing.

I'm been slowly reading how R interfaces with javascript, and some other R mapping interfaces. If I find something that works, I'll report back here.

Thanks.

edmundseto avatar Jan 19 '24 17:01 edmundseto

Closing the loop for anyone who comes across this thread...

It's not based on rdeck or Deckgl, but I was able to use the R mapboxer package to access vector tiles and create composite variables based on multiple fields in the tiles. That package is basically an R interface to Mapbox GL JS, and thus involves creating list-like Mapbox GL JS style statements. The example below computes an average of two variables, and uses the result for a choropleth map. Since it's just a list, it can be constructed in R before calling add_layer, for example in R Shiny dynamically.

add_layer(style = list(
  id = "myblocks",           # unique name for the mapboxer layer
  type = "fill",             # polygons fill and optionally stroked
  source = my_tile_source,   # defined by previous mapbox_source() call
  "source-layer" = "blocks", # the layer name defined in the tile  
  paint = list(              # this is Mapbox GL JS-like style code...
    "fill-color" = list(
      "interpolate",
      list("linear"),
      list ("/", 
            list ("+", 
                  list("get", "A_variable"),  # one of the tile fields
                  list("get", "B_variable"),  # another tile field
            2),
      0, '#edf8b1',
      1, '#3e5fd8'
    ),
    "fill-outline-color" = "#7d7d7d",
    "fill-opacity" = 0.5
  )
)))

edmundseto avatar Jan 22 '24 19:01 edmundseto