在 R tmap 中,如何在交互模式下向图例添加图层?

In R tmap, how do I add layers to legend in interactive mode?

从玩具示例开始,我可以使用以下代码在 tmap 中快速获得交互式地图:

library(tmap)
tmap_mode("view")

data("World", "metro")

tm_shape(World) +
tm_polygons('life_exp',
          legend.show = TRUE) +      
tm_shape(metro) +
  tm_dots("pop2010", 
          col = "red",
          legend.show = TRUE) + 
tm_format("World")

我正确地得到了多边形的图例。尽管设置了 legend.show = TRUE,但未能获得 tm_dots 的一个。有什么解决办法吗?

在没有其他选择的情况下,此解决方案可能是一种解决方法。只需创建虚拟变量并将其映射到选择的颜色或调色板:

library(tmap)
tmap_mode("view")

data("World", "metro")

metro$MyLegend <- "My item"

tm_shape(World) +
  tm_polygons('life_exp',
              legend.show = TRUE) +      
  tm_shape(metro) +
  tm_dots(col = "MyLegend",
          palette = c("red"),
          legend.show = TRUE) + 
  tm_format("World")