crazycapivara / deckgl

An R Interface to deck.gl
https://crazycapivara.github.io/deckgl/
Other
91 stars 11 forks source link

deck gl layers toggle #198

Open paragemini opened 2 years ago

paragemini commented 2 years ago

Hello -

I have a dataframe with racial makeup for Chicago. I am trying to add 5 different layers for 5 different races. Is there a way to toggle them on and off like one do in leaflet like the jpeg below ?

image

properties <- list(
  getPosition = ~lng + lat, #[165, 42,42]
  getRadius = 25, #JS("data => Math.sqrt(data.exits)"),
  #orange 246, 107, 14 
  # white race : blue dark : [64, 223, 239]
  #black race dark [255,198,0]  , light : 246, 107, 14
  getColor = JS("data => data.Type === 'White_alone' ? [10, 161, 221]: 
                data.Type === 'Black_or_African_American_alone' ? [246, 107, 14] : 
                data.Type === 'Some_other_race_alone' ? [6,255,0]: 
                data.Type === 'Asian_alone' ? [249, 7, 22] :
                data.Type === 'Two_or_more_races' ? [246, 107, 14] :
                data.Type === 'American_Indian_and_Alaska_Native_alone' ? [255, 23, 0] :
                data.Type === 'Native_Hawaiian_and_Other_Pacific_Islander_alone' ? [147, 255, 216] : [24, 10, 10]"),
  tooltip = "{{Type}}"
)

deck <- deckgl(zoom = 10.5, latitude = 41.889673, longitude = -87.634711) %>%
  add_scatterplot_layer(data = split_race_dot[[7]], properties = properties) %>%
  add_scatterplot_layer(data = split_race_dot[[3]], properties = properties, 
                        visible = FALSE) %>%
  add_scatterplot_layer(data = split_race_dot[[1]], properties = properties, 
                        visible = FALSE) %>%
  add_scatterplot_layer(data = split_race_dot[[2]], properties = properties, 
                        visible = FALSE) %>%
  add_scatterplot_layer(data = split_race_dot[[4]], properties = properties, 
                        visible = FALSE) %>%

  add_basemap(style = use_carto_style("positron"))  

if (interactive()) deck

Final layer box that could probably come at the top-right corner of the final map Thank you for the lovely package!!

image

crazycapivara commented 2 years ago

Duplicate #153

crazycapivara commented 2 years ago

@paragemini

It is not implemented yet but you can use the json editor:

remotes::install_github("crazycapivara/deckgl")

library(deckgl)

deckgl(zoom = 10, pitch = 35) %>%
  add_scatterplot_layer(
    data = bart_stations,
    getPosition = ~lng + lat,
    radiusScale = 6,
    getRadius = 50,
    getColor = c(240, 140, 20),
    visible = TRUE
  ) %>%
  add_text_layer(
    data = bart_stations,
    getPosition = ~lng + lat,
    getText = ~name,
    getAlignmentBaseline = "bottom",
    visible = TRUE
  ) %>%
  add_basemap() %>%
  add_json_editor(maxLines = 25)

Then change visible = false/true in the editor for layers you want to hide/show