代码之家  ›  专栏  ›  技术社区  ›  radek

在R tmap中,如何在交互模式下控制层可见性?

  •  2
  • radek  · 技术社区  · 6 年前

    从一个玩具的例子开始,我可以快速得到一个交互式地图 tmap

    library(tmap)
    tmap_mode("view")
    
    data("World", "metro")
    
    tm_shape(World) +
      tm_polygons() +
      tm_shape(metro) +
      tm_dots("pop2010", 
              col = "red") + 
      tm_format("World")
    

    我希望地图最初只显示 World

    我通过了 tm_shape tm_dots 医生,还没有发现任何能控制这种行为的东西。有可能吗?

    1 回复  |  直到 6 年前
        1
  •  4
  •   Valentin_Ștefan    6 年前

    似乎这在GitHub上是一个问题 here 也。

    一种解决办法是 tmap::tmap_leaflet() 创建传单小部件,然后使用 leaflet::hideGroup show/hide layers

    library(tmap)
    library(leaflet)
    
    tmap_mode("view")
    
    data("World", "metro")
    
    tm <-
      tm_shape(World) +
      tm_polygons() +
      tm_shape(metro) +
      tm_dots("pop2010", 
              col = "red") + 
      tm_format("World")
    
    # Pipe the tmap object into tmap_leaflet() to create a leaflet widget,
    # so that we can use leaflet::hideGroup().
    tm %>% 
      tmap_leaflet() %>%
      leaflet::hideGroup("metro")