2

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)
myfatson
  • 354
  • 2
  • 14
  • Can you please share your data by providing the output of `dput(zipcodes)` ? The link you provided has information for `risk.factor` but not for latitude and longitude, so we are not able to reproduce the map. – canovasjm Mar 17 '21 at 04:19
  • @canovasjm data is updated in original post – myfatson Mar 17 '21 at 15:18

5 Answers5

2

Just do na.color = NA

colorFactor(palette =  c("#F5DCA4","#E8A46A","#D16769","#B74146"), 
            domain = zipcodes@data$risk.factor,
            na.color = NA)
myfatson
  • 354
  • 2
  • 14
  • FYI, this solution seems to completely remove any polygons with value NA from the map. So, yes, no more NA block in the legend, but also no hover label or color at all for the NA polygons. – lauren.marietta Jun 04 '21 at 17:43
1

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.

canovasjm
  • 501
  • 1
  • 3
  • 11
  • 1
    Ah unfortunately not, I experimented with that first. It removes the text from the label but then there's just a phantom gray box floating at bottom with no text next to it. – myfatson Mar 17 '21 at 15:20
1

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) 
lauren.marietta
  • 2,125
  • 1
  • 11
  • 19
0

You could maybe include a CSS with the following rule:

.legend i {
  display: none;
}

This will make the grey box go away.

eastclintw00d
  • 2,250
  • 1
  • 9
  • 18
-1

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

Oliver Oliver
  • 2,057
  • 4
  • 16
  • 14