简体   繁体   中英

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

Starting with a toy example I can quickly get an interactive map in tmap with the following code:

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

I correctly get the legend for polygons. But failed to get one for tm_dots despite setting legend.show = TRUE . Is there any way around it?

In the absence of other options, this solution might be a way around. Simply create dummy variable and map it to colour or palette of choice:

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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