myfatson
myfatson

Reputation: 547

How do you exclude NA values from Leaflet legend?

Here's my data: https://paste.kodi.tv/omohuzawec

Updated data output from dput: redacted

I'm trying to replicate the following plot:

enter image description here

Here's the code I've used to replicate so far, the only thing I can't figure out is how to remove the NA from the legend.

pal <- colorFactor(palette =  c("#F5DCA4","#E8A46A","#D16769","#B74146"), 
                domain = zipcodes@data$risk.factor)

leaflet(zipcodes) %>% 
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(
    fillColor = ~pal(risk.factor),
    weight = 1, #weight of lines between zip codes
    color = "gray", #color of line between zip codes
    fillOpacity = .9, #fill opacity of zip codes
    popup = state_popup) %>%
  addLegend("bottomright", 
            pal = pal, #pal = palette declared earlier
            values = ~risk.factor, 
            title= "Risk: Lowest to Highest",
            opacity = 1) 

enter image description here

Correct code, it's the na.color that we needed :

colorFactor(palette =  c("#F5DCA4","#E8A46A","#D16769","#B74146"), 
                domain = zipcodes@data$risk.factor,
                na.color = NA)

Upvotes: 5

Views: 2796

Answers (7)

Marcos
Marcos

Reputation: 474

To build on the solution by @lauren.marietta you can also add a manual legend below the main legend to show what gray areas mean without messing up the formatting of the original legend. Here is a reproducible example.

library(leaflet)
library(leaflet.providers)
library(tidyverse)
library(sf)
library(tigris)

# download ZIP codes since original link for data not working.
# create variable with factors and random NAs, which are just zipcodes without water bodies
zipcodes <- zctas(state = "PA", year = 2010, starts_with = "191") %>% 
  mutate(risk.factor = case_when(
    AWATER10 > 0 & AWATER10 < 6252 ~ "0 - 24",
    AWATER10 >= 6252 & AWATER10 < 109209 ~ "25 - 49",
    AWATER10 >= 109209 & AWATER10 < 494740 ~ "50 - 74",
    AWATER10 >= 494740 ~ "75 - 98",
    AWATER10 == 0 ~ NA)
    ) 

pal <- colorFactor(
  palette =  c("#F5DCA4","#E8A46A","#D16769","#B74146"), 
  domain = zipcodes$risk.factor,
  na.color = "grey" # desired color for NA polygons on map
)

pal_noNA <- colorFactor(
  palette =  c("#F5DCA4","#E8A46A","#D16769","#B74146"), 
  domain = zipcodes$risk.factor,
  na.color = NA
)

leaflet(zipcodes) %>% 
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(
    fillColor = ~pal(risk.factor),
    weight = 1, #weight of lines between zip codes
    color = "gray", #color of line between zip codes
    fillOpacity = .9, #fill opacity of zip codes
    ) %>%
  addLegend(position = "bottomright",  # add manual legend with gray poly for "No data"
            colors = "#808080",
            labels = "No data",
            opacity = 1) %>% 
  addLegend("bottomright", 
            pal = pal_noNA,
            values = ~risk.factor, 
            title= "Risk: Lowest to Highest",
            opacity = 1) 

enter image description here

Upvotes: 0

carverd
carverd

Reputation: 420

You can also just exclude NA values from the values parameter within the add legend function. This allows you to utilize a single palette for the map and legend.

NA is visualized on the map, but not labeled within the legend.

I had the issue with colorNumeric where the NA label was displaying off to the side, which is super ugly. :( enter image description here

Here is an reproducible example and using colorFactor

library(sf)
library(leaflet)

demo(nc, ask = FALSE, echo = FALSE)

# Add arbitrary factor column
nc <- nc |>
  mutate(
    factor_col = rep(c("A", "B", "C", "D", NA),20)
  )


factpal <- colorFactor(topo.colors(4), nc$factor_col, na.color = "#000000")

leaflet(nc) |>
  addPolygons(color = ~factpal(factor_col))|>
  addLegend(pal = factpal,
            values = nc$factor_col[!is.na(nc$factor_col)])

enter image description here

Upvotes: 0

eastclintw00d
eastclintw00d

Reputation: 2364

You could maybe include a CSS with the following rule:

.legend i {
  display: none;
}

This will make the grey box go away.

Upvotes: 0

lauren.marietta
lauren.marietta

Reputation: 2564

For anyone who doesn't want NA to appear in their legend, but still wants their polygons with NA values to have hover labels and some sort of color, I also found the GitHub solution that @canovasjm mentioned, which defines two different palettes, helpful:

pal <- colorFactor(
    palette =  c("#F5DCA4","#E8A46A","#D16769","#B74146"), 
    domain = zipcodes@data$risk.factor,
    na.color = "grey" # desired color for NA polygons on map
)

pal_noNA <- colorFactor(
    palette =  c("#F5DCA4","#E8A46A","#D16769","#B74146"), 
    domain = zipcodes@data$risk.factor
    na.color = NA
)

leaflet(zipcodes) %>% 
  addProviderTiles("CartoDB.Positron") %>%
  addPolygons(
    fillColor = ~pal(risk.factor),
    weight = 1, #weight of lines between zip codes
    color = "gray", #color of line between zip codes
    fillOpacity = .9, #fill opacity of zip codes
    popup = state_popup) %>%
  addLegend("bottomright", 
            pal = pal_noNA,
            values = ~risk.factor, 
            title= "Risk: Lowest to Highest",
            opacity = 1) 

Upvotes: 3

myfatson
myfatson

Reputation: 547

Just do na.color = NA

colorFactor(palette =  c("#F5DCA4","#E8A46A","#D16769","#B74146"), 
            domain = zipcodes@data$risk.factor,
            na.color = NA)

Upvotes: 2

Oliver Oliver
Oliver Oliver

Reputation: 2327

Add na.color=rgb(0,0,0,0) to the colorFactor function along with na.label = "" to the addLegend function

Upvotes: -1

canovasjm
canovasjm

Reputation: 513

The function addLegend() has an argument na.label which by default is set to "NA". From here it seems one can set na.label = "" to remove the NA label from the legend.

Upvotes: 1

Related Questions