简体   繁体   English

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

[英]In R tmap, how do I control layer visibility in interactive mode?

Starting with a toy example I can quickly get an interactive map in tmap with the following code:从一个玩具示例开始,我可以使用以下代码在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")

I'd like the map to initially display only the World layer and have the metro layer hidden.我希望地图最初仅显示World图层并隐藏 Metro 图​​层。 It'd only appear when user ticks the box in the layers selection.它只会在用户勾选图层选择中的框时出现。

I went through tm_shape and tm_dots docs and haven't found anything that seems to control such behaviour.我浏览了tm_shapetm_dots文档,但没有发现任何似乎可以控制这种行为的东西。 Is that possible?那可能吗?

Seems that this was addressed on GitHub as an issue here as well.似乎这在 GitHub 上也作为问题解决

One solution would be to use tmap::tmap_leaflet() to create a leaflet widget, and then use leaflet::hideGroup to show/hide layers .一种解决方案是使用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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM