Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Legend colors do not match map colors #382

Closed
Fred-Wu opened this issue May 17, 2021 · 1 comment
Closed

Legend colors do not match map colors #382

Fred-Wu opened this issue May 17, 2021 · 1 comment

Comments

@Fred-Wu
Copy link

Fred-Wu commented May 17, 2021

This is similar to an old issue #219 , but a bit long time ago. So I hope it is OK for me to open a new one.

I used brewer.pal from RColorBrewer package to prepare the col.regions with 5 groups. However, the region colors look different from the ones on the legend.

I think this has something to do with alpha.regions in the first output, but not sure why leaflet would be more similar here.

colRegions <- colorRampPalette(brewer.pal(5, 'YlOrRd'))
b <- mapview(sa3, zcol = "AREASQKM16",
        at = quantile(sa3$AREASQKM16, prob = seq(0, 1, length.out=6)),
        color = NA, map.types = "OpenStreetMap",
        layer.name = "Area in km^20",
        col.regions = colRegions)

image

The region colors are more similar to the one with addLegend if I clear the legend control and add legend with leaflet function.

colPal <- colorQuantile("YlOrRd", n = 5, domain = sa3$AREASQKM16)
b@map %>% clearControls() %>% addLegend(position = "topright", 
                pal = colPal, title = "Area in km^2",
                values = sa3$AREASQKM16,
                labFormat = function(type, cuts, p) {
                  n = length(cuts)
                  paste0(cuts[-n], " &ndash; ", cuts[-1])
                })

image

@tim-salabim
Copy link
Member

Yeah, that's a tricky one and there is no clear solution here. The legend colors have a alpha of 1 whereas the the polygons are filled with an alpha of 0.6 by default (I think). The colors in the legend can only ever match the ones on the map if both are completely opaque. Otherwise their values will depend on what's behind them, i.e. the location on the map...
That said, there was a bug I recently addressed which would lead to these fully opaque legends. When you re-install from here, you can control legend.opacity via the options.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants