0

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? I have try an if in fill color but i have an error Error in getMapData(map) : argument "map" is missing, with no default

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)
Inuraghe
  • 564
  • 3
  • 13
  • How about you use a palette like `yourPalette <- sequential_hcl(3, 'Mako')` then replace middle value by grey `yourPalette[2] <- "#838383"` then use the palette in your function – Yacine Hajji May 04 '22 at 09:08
  • How can I use? I found pal <- colorNumeric("viridis", NULL) and it's work well – Inuraghe May 04 '22 at 09:26
  • Yes it can work with any palette because it returns a string of HTML color code. Your can use sequential_hcl palettes with `colorspace` library. `library("colorspace") ; hcl_palettes(plot = TRUE)` – Yacine Hajji May 04 '22 at 10:05

1 Answers1

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

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

nycounties@data = data.frame(nycounties@data,
                             df[match(nycounties@data[, "prov_name"],
                                      df[, "city"]),])

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

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

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)
Inuraghe
  • 564
  • 3
  • 13