r - 在 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")
我希望地图最初只显示World
图层并隐藏地铁图层。它只会在用户勾选图层选择中的框时出现。
我浏览了文档tm_shape
并tm_dots
没有发现任何似乎可以控制这种行为的东西。那可能吗?
解决方案
似乎这在 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")
推荐阅读
- javascript - 为什么对象中的元素在循环中总是被赋予相同的值 | VueJS
- python - Django Channels - 当组成员时无法直接向频道发送消息
- firebase - 当您的应用服务器托管在不同的云服务上时,如何安全地从 Firebase 托管上的 CDN 缓存中受益
- python - 第二个按钮覆盖第一个按钮
- r - 时间序列模型推荐
- airflow - 使用 Jinja 根据变量向收件人发送失败电子邮件
- sql - 通过将姓氏的前 3 个字母添加到生成的从 001 开始的数字序列中来创建员工编号
- javascript - 打字稿 - 方法未定义
- excel - 粘贴边框的 VBA 代码 If Range A:A <> ""
- python - 如何过滤 NaT 值