user319487
user319487

Reputation:

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?

Upvotes: 0

Views: 1217

Answers (1)

user319487
user319487

Reputation:

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

enter image description here

Upvotes: 1

Related Questions