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)
