在 R tmap 中,如何在交互模式下控制图层可见性?
In R tmap, how do I control layer visibility in interactive mode?
从玩具示例开始,我可以使用以下代码在 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
文档,但没有发现任何似乎可以控制此类行为的内容。这可能吗?
这似乎也在 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")
从玩具示例开始,我可以使用以下代码在 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
文档,但没有发现任何似乎可以控制此类行为的内容。这可能吗?
这似乎也在 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")