walkerke / mapgl

R interface to Mapbox GL JS v3 and Maplibre GL JS
https://walker-data.com/mapgl
Other
83 stars 3 forks source link

graceful handling for missing in add_circle_layer #30

Open ews-grmunjal opened 1 month ago

ews-grmunjal commented 1 month ago

if one of the values in the column is NA... no circles are rendered

good behaviour could be to have a na.rm type argument, if TRUE, dont render NA circles, if FALSE, NAs can be rendered as a separate category

ews-grmunjal commented 1 month ago

reproducible example:

library(mapgl) library(sf) library(dplyr)

nc <- st_read(system.file('shape/nc.shp', package = 'sf'))

WORKS

numcolors <- 6 nc <- nc %>% mutate(zz = sample(letters[1:numcolors], nrow(.), replace = T)) magma_pal <- viridisLite::magma(numcolors) magma_substr_pal <- substr(magma_pal, 1, 7)

mapboxgl() %>% fit_bounds(nc, animate = F) %>% add_circle_layer( id = 'GC Zones', source = nc, circle_color = match_expr( column = 'zz', values = unique(nc$zz), stops = magma_substr_pal ) ) %>% add_legend( 'GC Zones', values = unique(nc$zz), colors = magma_substr_pal, type = 'categorical' )

DOES NOT WORK

nc$zz[1] <- NA numcolors <- 7 magma_pal <- viridisLite::magma(numcolors) magma_substr_pal <- substr(magma_pal, 1, 7)

mapboxgl() %>% fit_bounds(nc, animate = F) %>% add_circle_layer( id = 'GC Zones', source = nc, circle_color = match_expr( column = 'zz', values = unique(nc$zz), stops = magma_substr_pal ) ) %>% add_legend( 'GC Zones', values = unique(nc$zz), colors = magma_substr_pal, type = 'categorical' )