1 year ago

#355478

test-img

Dave Braze

Update to "reverse order in R leaflet continuous legend"?

I have exactly the same question as this one from 5 years ago. I want to invert the scale of a continuous color scale in a leaflet choropleth created from R. I'm hoping that there is a better answer available now, than then.

reverse order in R leaflet continuous legend

Here is my code: (dput()ed data available here: https://drive.google.com/file/d/1asaNqvtm0VXGH-o7GoWVd3Q6FSm1RNES/view )


pal <- colorNumeric("magma", NULL)

map.positivity.leaflet <-
    leaflet(data=D) %>%
    setView(lng=-72.8, lat=41.5, zoom=9) %>%
    addPolygons(color="lightgrey",      # stroke color
                stroke = TRUE,
                weight = 1.5,           # stroke
                fillColor = ~pal(town.positivity),
                fillOpacity = 1,
                label = ~text,
                labelOptions = labelOptions(textsize = "15px", ## could also set "font-size" in 'style' arg
                                            style = list( # add custom CSS
                                                "background-color" = "darkgreen",
                                                "color" = "white" # font color
                                            ))) %>%
    addLegend(
        "bottomright",
        pal = pal,
        values = ~town.positivity,
        title = "<b>Test Positivity (%)</b>",
        opacity = 1
    )


Here is what I get:

choropleth with inverted colorscale

I want the colorscale to run from 0 at the bottom to its maximum at the top. Is there a less kludgy way to do that than available in answers to the 5 year old question cited above?

r

leaflet

legend

choropleth

0 Answers

Your Answer

Accepted video resources