既然你还没有得到任何答案,我会发布一些可以帮助你的东西,基于a 的 简单的例子 强> 。
如果你的可重复性,那当然会更容易;我想从环顾四周你已经看到有几个相关的问题/要求(关于重新着色多边形),而它似乎并没有真正的解决方案使其成为任何发布(传单)。
通过以下解决方案,您应该能够避免多重操作 addPolygons 并且可以覆盖任意数量的变量(现在我刚刚将一个变量硬编码到 modFillCol 尽管打电话)。
addPolygons
modFillCol
library(leaflet) library(maps) library(viridis) mapStates = map("state", fill = TRUE, plot = FALSE) # regarding Question 3 - the way you set the domain it looks equivalent # to just not setting it up front, i.e. domain = NULL myPalette <- colorNumeric( palette = "viridis", domain = NULL ) mp <- leaflet(data = mapStates) %>% addTiles() %>% addPolygons(fillColor = topo.colors(10, alpha = NULL), stroke = FALSE) # utility function to change fill color modFillCol <- function(x, var_x) { cls <- lapply(x$x$calls, function(cl) { if (cl$method == "addPolygons") { cl$args[[4]]$fillColor <- myPalette(var_x) } cl }) x$x$calls <- cls x } # modify fill color depending on the variable, in this simple example # I just use the number of characters of the state-names mp %>% modFillCol(nchar(mapStates$names))