Hiding Colors in a Map

Using the following link as a guide (Possibility of using leaflet.extra::addSearchFeature to addGlPolygons · Issue #18 · r-spatial/leafgl · GitHub), I made this map that I really like in R:

library(sf)  
library(leaflet)
library(leafgl)
library(colourvalues)
library(leaflet.extras)


nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>% 
  st_transform(st_crs(4326)) %>% 
  st_cast('POLYGON')

leaflet(data = nc) %>% addPolygons( stroke = FALSE) %>% addTiles(group = "OSM") %>%  addProviderTiles(provider = providers$OpenStreetMap) %>% addPolygons(data = nc, weight=1, popup = ~NAME,
                label = ~NAME, group = "name", col = 'blue') %>% 
    addSearchFeatures(targetGroups  = 'name', options = searchFeaturesOptions(zoom=10, openPopup=TRUE))

enter image description here

The only thing that I would like to change - I would like to remove the blue color/shading from this map. That is, I would like the user to be able to search for a name on this map (e.g. Edgecombe) - and once the user searchers for this name, ONLY the corresponding region "lights up" in this blue shading.

I tried to do this by manually removing the color option from the leaflet statement:

leaflet(data = nc) %>% addPolygons( stroke = FALSE) %>% addTiles(group = "OSM") %>%  addProviderTiles(provider = providers$OpenStreetMap) %>% addPolygons(data = nc, weight=1, popup = ~NAME,
                label = ~NAME, group = "name" %>% 
    addSearchFeatures(targetGroups  = 'name', options = searchFeaturesOptions(zoom=10, openPopup=TRUE))

But nothing has changed.

  • Does anyone have any ideas on how to this? Something like this?

Thank you!

Removing the blue color is possible; set it's opacity to zero. Such as in here - setting fillOpacity of the polygons to zero.

Unfortunately I am not aware of a way to flip the color back for the single polygon found; I don't think it is possible, at least without writing custom javascript.

library(sf)  
library(leaflet)
library(leafgl)
library(leaflet.extras)


nc <- st_read(system.file("gpkg/nc.gpkg", package="sf"), quiet = TRUE) %>% 
  st_transform(st_crs(4326)) %>% 
  st_cast("POLYGON")

leaflet(data = nc) %>% 
  addProviderTiles(provider = providers$OpenStreetMap) %>% 
  addPolygons(data = nc, 
              weight=1, 
              popup = ~NAME,
              label = ~NAME, 
              group = "name", 
              col = 'blue', 
              fillOpacity = 0) %>% 
  addSearchFeatures(targetGroups  = 'name', 
                    options = searchFeaturesOptions(zoom=10, openPopup=TRUE))

Screenshot-41

Thank you so much for your answer! Can you please explain what you mean by "writing custom Javascript"? Is this possible in R itself? Do you think this would be difficult?

Thank you so much!

In R you can do a lot, but the interactivity required would need to happen outside of your R session; the leaflet map is a standalone piece of work.

The javascript providing the search feature is distributed as a part of {leaflet.extras} package; the source code is available at leaflet.extras/lfx-search-bindings.js at master · bhaskarvk/leaflet.extras · GitHub .

In theory you should be able to clone it and amend slightly so that the color of a feature found changes.

In practice this would be way over my (very limited) javascript knowledge.

1 Like

thank you for your reply I wish there was some tutorial that would show you how to do this!

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.