2

I am trying to make an interactive map with different layers in which the user can pick one of 8 different layers. The shape for all the layers is the exact same, the only thing changes is the column.

Ideally I would want something like this, but this doesn't work as the selectable interactive layers come from the tm_shape, not tm_fill.

library(tmap)

tmap_mode("view") +
 tm_shape(MAP_DATA) +
 tm_fill(col = "COLUMN1") +
 tm_fill(col = "COLUMN2") +
 tm_borders()

I can get this to work, but it feels very inefficient:

MAP_DATA2 <- MAP_DATA

tmap_mode("view") +
 tm_shape(MAP_DATA) +
 tm_fill(col = "COLUMN1") +
 tm_borders() +
 tm_shape(MAP_DATA2) +
 tm_fill(col = "COLUMN2") +
 tm_borders()
jazzurro
  • 23,179
  • 35
  • 66
  • 76
Aaron Walton
  • 150
  • 1
  • 10

1 Answers1

2

It seems to me that, if you want to write less code, you would have to sacrifice having multiple layers on one map. If you do not mind this sacrifice, you can do the following. You can specify which columns you want to use in tm_polygons(). You end up having four maps at the same time. However, you do not have to code a lot. In the end, I think it is your choice whether you want to code more or less.

library(tmap)
tm_shape(NLD_muni) +
tm_polygons(col = c("population", "pop_0_14", "pop_15_24",
                    "pop_25_44")) + 
tm_facets(nrow = 2, sync = TRUE)

enter image description here

jazzurro
  • 23,179
  • 35
  • 66
  • 76