首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R-Leaflet Map -帮助我在R leaflet中组合2个图例

R-Leaflet Map -帮助我在R leaflet中组合2个图例
EN

Stack Overflow用户
提问于 2020-03-22 21:31:17
回答 1查看 331关注 0票数 0

我制作了一个R叶地图,我有2个图例。如何将它们结合起来?谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-06-19 22:08:38

了解R中地图(str(mapObject))对象的结构可能是一个很有帮助的起点。这对于对图例进行“售后”编辑非常有用。

我试着用它来解决你的问题:

代码语言:javascript
复制
    # Concatenate the vectors that define each set of colors and their corresponding values:

require(spData)
require(leaflet)
require(sf)

# loading shapes of countries from the package spData

data(world)
world <- st_read(system.file("shapes/world.gpkg", package="spData"))
africa <- world[world$continent == "Africa",]
asia <- world[world$continent == "Asia", ]

asiaPal <- colorNumeric("Reds", domain = asia$pop)
africaPal <- colorNumeric("Blues", domain = africa$pop)


map <- leaflet() %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addPolygons(data = asia,
              color = ~asiaPal(asia$pop)) %>%
  addPolygons(data = africa,
              color = ~africaPal(africa$pop)) %>%
  addLegend("bottomright", pal = asiaPal, values = asia$pop, title = "Asian Population") %>%
  addLegend("bottomright", pal = africaPal, values = africa$pop, title = "African Population")

    # Colors
    map$x$calls[[5]]$args[[1]]$colors <- 
         c(map$x$calls[[5]]$args[[1]]$colors, map$x$calls[[4]]$args[[1]]$colors)

    # Labels        
    map$x$calls[[5]]$args[[1]]$labels <-
         c(map$x$calls[[5]]$args[[1]]$labels, map$x$calls[[4]]$args[[1]]$labels)

    # Get rid of Old Legend:
    map$x$calls[[4]] <- NULL

您的图例是由map$x$调用的元素4和5产生的。

这并不能很好地工作。我怀疑这是因为这些列表元素不是最终结果,而map对象的元素是在呈现map时提供给javascript/html的。也就是说,我不知道在不查看实际HTML结果的情况下,是否可以轻松地实现您想要实现的目标。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60799923

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档