qfes / rdeck

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

scale_color_category cannot be used with integer data #64

Closed MilesMcBain closed 1 year ago

MilesMcBain commented 2 years ago
library(rdeck)library(sf)
#> Linking to GEOS 3.10.1, GDAL 3.4.0, PROJ 8.2.0; sf_use_s2() is TRUE
reprex_data <-structure(list(state_hazmat_mobilised = 0L, geometry = structure(list(     structure(c(153.408180931689, -27.9478084866912), class = c("XY", 
    "POINT", "sfg"))), class = c("sfc_POINT", "sfc"), precision = 0, bbox = structure(c(xmin = 153.408180931689, ymin = -27.9478084866912, xmax = 153.408180931689, ymax = -27.9478084866912   ), class = "bbox"), crs = structure(list(input = "EPSG:4326",     wkt = "GEOGCRS[\"WGS 84\",\n    ENSEMBLE[\"World Geodetic System 1984 ensemble\",\n        MEMBER[\"World Geodetic System 1984 (Transit)\"],\n        MEMBER[\"World Geodetic System 1984 (G730)\"],\n        MEMBER[\"World Geodetic System 1984 (G873)\"],\n        MEMBER[\"World Geodetic System 1984 (G1150)\"],\n        MEMBER[\"World Geodetic System 1984 (G1674)\"],\n        MEMBER[\"World Geodetic System 1984 (G1762)\"],\n        MEMBER[\"World Geodetic System 1984 (G2139)\"],\n        ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n            LENGTHUNIT[\"metre\",1]],\n        ENSEMBLEACCURACY[2.0]],\n    PRIMEM[\"Greenwich\",0,\n        ANGLEUNIT[\"degree\",0.0174532925199433]],\n    CS[ellipsoidal,2],\n        AXIS[\"geodetic latitude (Lat)\",north,\n            ORDER[1],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n        AXIS[\"geodetic longitude (Lon)\",east,\n            ORDER[2],\n            ANGLEUNIT[\"degree\",0.0174532925199433]],\n    USAGE[\n        SCOPE[\"Horizontal component of 3D system.\"],\n        AREA[\"World.\"],\n        BBOX[-90,-180,90,180]],\n    ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), row.names = c(NA,
-1L), sf_column = "geometry", agr = structure(c(state_hazmat_mobilised = NA_integer_), .Label = c("constant",
"aggregate", "identity"), class = "factor"), class = c("sf",
"tbl_df", "tbl", "data.frame"))

rdeck() |>
  add_scatterplot_layer(
    data = reprex_data,
    get_fill_color = scale_color_category(
      state_hazmat_mobilised
    )
  )

#> Error in `add_scatterplot_layer()`:
#> ! Failed to create layer
#> Caused by error:#> ! Assertion failed: is_discrete(x)
#> • Continuous value supplied to discrete scale

Created on 2022-04-13 by the reprex package (v2.0.1)

Slightly surprising since integer is 'discrete'.

anthonynorth commented 2 years ago

This is for consistency with {scales} (https://github.com/r-lib/scales/blob/main/R/scale-discrete.r#L28) and {ggplot2} (scale_colour_discrete()). rdeck re-implements the DiscreteRange to preserve input order, but otherwise is equivalent to scales::DiscreteRange().

While I think a factor / character would generally be a better choice for a category scale, maybe it's needlessly restrictive. One case where ints as they are would be ok, is integer codes from abs admin boundaries.