如果你不介意使用leaflet 包而不是mapview 包,你可以采取以下方法。您需要的是使用 colorRampPalette 创建自定义颜色。以您的方式,您只是在两种颜色之间插入了颜色空间。您实际上可以指定要创建多少种颜色。在这里,我创建了两个色彩空间。第一个仅用于白色,第二个用于白色和 deeppink2 之间的颜色空间。我结合了这两个颜色向量并创建了rampcols
. 我用这个为传单创建了颜色。最后,我为你画了一张地图。我对mawview不熟悉。但是,如果您可以使用相同的方法,那可能是您的最佳方案。否则,这足以让您继续您的项目。
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)