问题
The following code creates a legend with even breaks. How could I change it so that every value from 0-1 is shown as white, and values from 1-max value are shown with breaks of 0.5 displayed with graduated shades? (In other words, the only change to the map below would be that all values 0-1 are shown in white).
Mapview doesn't seem to recognize breaks
. The only other solution I can think of is to split the data and make two maps, one of SHAPE_LEN
values from 0-1, and the second of SHAPE_LEN
values from 1-3.98, and display both maps together. That's not a desirable solution because I then have to display 2 legends.
Thanks SO!
library(leaflet)
library(mapview)
pink2 = colorRampPalette(c('white', 'deeppink'))
mapview(franconia, alpha.regions = 1, map.types = 'CartoDB.Positron',
layer.name = 'Franconia', zcol = 'SHAPE_LEN',
col.regions = pink2, at = seq(0, 4, .05))
回答1:
If you do not mind using the leaflet package rather than the mapview package, you can take the following approach. What you need is to create custom colors with colorRampPalette. In your way, you just interpolated the color space between the two colors. You can actually specify how many colors you want to create. Here I created two color spaces. The first one is just for white and the second one is for the color space between white and deeppink2. I combined these two color vectors and created rampcols
. I created colors for leaflet with this. Finally, I drew a map for you. I am not familiar with mawview. But if you can use the same approach, that may be the best scenario for you. Otherwise, this is enough for you to carry on your project.
library(mapview)
library(leaflet)
# Make vector of colors for values between 0 and 1 (10 colors)
# You may have to change the number of colors depending on your situation.
rc1 <- colorRampPalette(colors = c("white", "white"), space = "Lab")(10)
# Make vector of colors for values between 1 and 4 (30 colors)
# You may have to change the number of colors depending on your situation.
rc2 <- colorRampPalette(colors = c("white", "deeppink2"), space = "Lab")(30)
# Combine the two color palettes
rampcols <- c(rc1, rc2)
mypal <- colorNumeric(palette = rampcols, domain = franconia$SHAPE_LEN
# Draw a map
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data = franconia, group = "Franconia",
stroke = FALSE, smoothFactor = 0.2, fillOpacity = 1,
fillColor = ~mypal(franconia$SHAPE_LEN),
popup = paste("Region: ", franconia$NAME_ASCI, "<br>",
"Value: ", franconia$SHAPE_LEN, "<br>")) %>%
addLayersControl(overlayGroups = "Franconia") %>%
addLegend(position = "bottomright", pal = mypal, values = franconia$SHAPE_LEN,
title = "Franconia",
opacity = 1)
来源:https://stackoverflow.com/questions/59913705/how-to-set-an-uneven-sequence-for-legends-in-mapview