custom color palette for leaflet R consistently throws a warning

50 Views Asked by At

I create a leaflet app. For one particular value within my data, I want to use a custom color. For the remaining colors, I want to use a ramp. As I don't know any better, I create a simple color function with an ifelse-statement.

Here is sample-code:

library(leaflet)

colors <- function(x){
  return(ifelse(x==1422, #if it was founded in 1422
                "#ff7400", #return this orange, else use colorNumeric
                colorNumeric(palette="viridis",
                             domain=unique(breweries91$founded) %>% setdiff(1422), 
                             na.color=NA)(x)
                )
    )
  }


breweries91 %>% leaflet() %>% addTiles() %>% addCircleMarkers(fillColor =~colors(founded),
                                                              weight=0,
                                                              fillOpacity = 1)

the result looks as expected, with one point in orange.

enter image description here

But it throws the following warning, as if the Brewery from 1422 still passes through colorNumeric()

Warning message:
In colorNumeric(palette = "viridis", domain = unique(breweries91$founded) %>%  :
  Some values were outside the color scale and will be treated as NA
2

There are 2 best solutions below

0
Beni On

One possibility, that better handles missing values, would be:

colors <- function(x){
  return(if_else(x==1422, #if it was founded in 1422
                "#ff7400", #return this orange, else use colorNumeric
                suppressWarnings(colorNumeric(palette="viridis",
                             domain=unique(breweries91$founded) %>% setdiff(1422), 
                             na.color="black")(x)),
                missing = "#FF000000"
                )
    )
  }

as indicated here: ifelse shows warning based on the else function even when the test condition is met

2
VinceGreg On

@Beni pointed out the important element about the ifelse test. I am sharing my solution and reproductible example, where the specific value is remplaced by NA beforehand. I used meuse from sp, transormed it to sf. The copper is equivalent to founded and the value 14 is the equivalent of the specified 1422

Edit: It has been specified that the datset contain NA. I also put an argument orange_value for the 14 / 1422 value.

library(leaflet)
library(sf)
library(dplyr)

data(meuse, package = "sp") 
meuse_sf = st_as_sf(meuse, coords = c("x", "y"), crs = 28992) %>% 
  st_transform(crs=4326)

# Adding some missing values to be displayed black
meuse_sf[c(1:3) , "copper"] = NA

colors <- function(x,orange_value){
  # Temporary change of "minimum" highlighted value
  x2 = x %>%  na_if(orange_value)
  
  # General color palette
  x3=colorNumeric(palette="viridis",
                  domain = range(x2, na.rm=TRUE),
                  na.color="black")(x2)
  
  #Re-specifying the orange value 
  x3[x==orange_value] = "#ff7400"
  return(x3)
  
}

meuse_sf %>%
  leaflet() %>% addTiles() %>% addCircleMarkers(fillColor =~colors(copper,14),
                                                weight=0,
                                                fillOpacity = 1)