【问题标题】:In R tmap, how do I add layers to legend in interactive mode?在 R tmap 中,如何在交互模式下将图层添加到图例?
【发布时间】:2018-11-07 14:48:03
【问题描述】:

从一个玩具示例开始,我可以使用以下代码在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 获得一个。有什么办法解决吗?

【问题讨论】:

    标签: r tmap


    【解决方案1】:

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

    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")
    

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 2021-05-14
      • 2018-06-21
      • 2017-10-03
      • 2019-06-15
      • 2020-12-20
      • 1970-01-01
      • 2016-01-18
      • 1970-01-01
      相关资源
      最近更新 更多