Palette and also a specific color

Hi, it is possible create use a grey when the value in nycounties$dimension is 0 and in the rest use the palette? How can I do it?

library(leaflet)
nycounties <- rgdal::readOGR("https://raw.githubusercontent.com/openpolis/geojson-italy/master/geojson/limits_IT_provinces.geojson")

city <- c("Novara", "Milano","Torino","Bari")
dimension <- as.numeric(c("1500", "5000","3000","4600"))
df <- data.frame(city, dimension)

nycounties@data = data.frame(nycounties@data,
                             df[match(nycounties@data[, "prov_name"],
                                      df[, "city"]),])
pal <- colorNumeric("viridis", NULL)

nycounties$dimension[is.na(nycounties$dimension)] = 0

leaflet(nycounties) %>%
  addTiles() %>%
  addPolygons(stroke = TRUE, smoothFactor = 0.3, fillOpacity = 1,
              fillColor = ~pal(nycounties$dimension), weight = 1, color = "black", label = nycounties$prov_name) %>%
  addLegend(pal = pal, values = ~(nycounties$dimension), opacity = 1.0)

I think this can be done by using the colorNumeric, domain :

pal <- colorNumeric("viridis", domain=c(1,max(nycounties$dimension)))

image

Have you change only this code? My result is complety different

I have change pal and nycounties$dimension[is.na(nycounties$dimension)] = 0 order but the graphic is different (unit legend)

yes, I changed specifically only the line I altered and got the result I shared from doing that single edit.

This topic was automatically closed 7 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.