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

rad*_*dek 4 r tmap

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")
Run Code Online (Sandbox Code Playgroud)

我希望地图最初仅显示图层World并隐藏地铁图层。仅当用户勾选图层选择中的框时它才会出现。

我浏览了tm_shape文档tm_dots,但没有发现任何似乎可以控制这种行为的东西。那可能吗?

Val*_*tin 6

似乎这个问题也在 GitHub上得到了解决。

一种解决方案是使用tmap::tmap_leaflet()创建传单小部件,然后使用leaflet::hideGroup显示/隐藏图层

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")
Run Code Online (Sandbox Code Playgroud)