-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add changeColors()
for changing the color of a map layer on the fly
#67
base: master
Are you sure you want to change the base?
Conversation
changeColors()
for changing the color of a map layer on the fly
Hi @dfriend21,
l <- leaflet() %>%
setView(-112.56, 40.66, 5) %>%
addProviderTiles("CartoDB.DarkMatter") %>%
addWMSTiles("https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2016_Bare_Ground_Shrubland_Fractional_Component/ows?SERVICE=WMS&",
layers = "NLCD_2016_Bare_Ground_Shrubland_Fractional_Component",
options = WMSTileOptions(className = "bare_ground", transparent = TRUE, format = "image/png"))
l l %>% changeColors("bare_ground", terrain.colors(2000))
library(raster)
r <- raster(xmn = -2.8, xmx = -2.79, ymn = 54.04, ymx = 54.05, nrows = 30, ncols = 30)
values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
crs(r) <- CRS("+init=epsg:4326")
pal <- colorNumeric("Spectral", domain = c(0, 1000))
leaflet() %>% addTiles() %>%
addRasterImage(r, colors = pal, opacity = 0.8, options = tileOptions(className = "base")) %>%
addLegend(pal = pal, values = c(0, 1000)) leaflet() %>% addTiles() %>%
addRasterImage(r, colors = pal, opacity = 0.8, options = tileOptions(className = "base")) %>%
addLegend(pal = pal, values = c(0, 1000)) |>
changeColors("base", colorRampPalette(RColorBrewer::brewer.pal(11, "Blues"))(50)) Is there any way to infer/clarify the number of colors for 1.? |
Hi @tim-salabim, I made some changes to allow for r <- raster::raster(xmn = -2.8, xmx = -2.79, ymn = 54.04, ymx = 54.05,
nrows = 30, ncols = 30, crs = "EPSG:4326", vals = 1:900)
old_pal <- colorNumeric(topo.colors(50), c(0, 1000))
new_pal <- heat.colors(50)
leaflet() |>
addTiles() |>
addRasterImage(r, colors = old_pal, opacity = 0.8,
options = tileOptions(className = "base")) |>
addLegend(pal = old_pal, values = c(0, 1000),
className = "info legend base-legend") |>
changeColors("base", new_pal) |>
changeColors("base-legend", new_pal, legend = TRUE) It's a bit clunky, but I couldn't find a more streamlined way to do it. Also, I've made some changes and bug fixes to this function in the time since I made the pull request, so I added those changes in there (for example, the original version didn't work with |
This PR adds a function to re-map the colors of a leaflet layer on the fly. I had two closely related reasons for creating it:
It uses the gradientmaps JavaScript library to change the colors. For an example, here's a WMS layer as-is:
And here I've changed the colors using the
changeColors()
function:Here's the code to recreate the previous example:
I thought it might be a good fit for the
leafem
package, and it seems like something that could be useful for others as well. And from a more selfish point of view, it'd be nice for me if it was part of an already-existing package rather than its own tiny package, which is how I have it right now.No worries if you'd rather not include it in the package. Also, I'm happy to make any changes to the code and/or documentation.