Closed ryanscharf closed 3 months ago
Yeah, I've noticed this too. Unfortunately this is an underlying issue with the JS libraries, they can't handle 8-character hex color codes with transparency appended.
I'd be open to suggestions about this. I'm torn between making color palettes "easier" (like other R mapping packages do) and preserving the flexibility of Mapbox / MapLibre which I really like.
A possible patch in the interim is to detect if a stop is a hex code (so starting with #
) then truncating the string before sending to JS.
As a user coming from tmap/leaflet, it feels natural for me to want to use palettes in the same way in mapgl as I do elsewhere. Do other packages just do a substr if the length is >7?
Just pushed a fix in my latest commit. This code now works:
library(mapgl)
library(sf)
library(dplyr)
magma_pal <- viridisLite::magma(6)
nc <- st_read(system.file('shape/nc.shp', package = 'sf'))
nc <- nc %>% mutate(zz = sample(letters[1:6], nrow(.), replace = T))
mapboxgl() %>%
fit_bounds(nc, animate = F) %>%
add_fill_layer(
id = 'GC Zones',
source = nc,
fill_color = match_expr(
column = 'zz',
values = unique(nc$zz),
stops = magma_pal
)
) %>%
add_legend(
'GC Zones',
values = unique(nc$zz),
colors = magma_pal,
type = 'categorical'
)
I have been having an issue with my first mapgl map where virdislite palettes appear to be failing with match_expr(). If you cut off the trailing FF's, the palettes work fine.
reprex:
sessionInfo