Skip to content

Instantly share code, notes, and snippets.

@Nowosad
Last active October 4, 2022 08:56
Show Gist options
  • Save Nowosad/9a49aceca3ffb4dc1a4c9c615f00540a to your computer and use it in GitHub Desktop.
Save Nowosad/9a49aceca3ffb4dc1a4c9c615f00540a to your computer and use it in GitHub Desktop.
library(tmap)
#> Warning: multiple methods tables found for 'area'
data(metro)
metro$group = as.factor(sample(1:5, size = nrow(metro), replace = TRUE))

# two legends
tm_shape(metro) +
  tm_symbols(col = "group", shape = "group")

# one legend
tm_shape(metro) +
  tm_symbols(col = "group", shape = "group", 
             legend.col.show = FALSE, legend.shape.show = FALSE) +
  tm_add_legend(type = "symbol", col = RColorBrewer::brewer.pal(5, "Set3"),
                shape = 21:25, labels = sort(unique(metro$group)))

Created on 2022-07-05 by the reprex package (v2.0.1)

@mtennekes
Copy link

tmap4:

data(metro)
metro$group = as.factor(sample(1:5, size = nrow(metro), replace = TRUE))

tm_shape(metro) +
tm_symbols(fill = "group", 
   fill.scale = tm_scale_categorical(values = "brewer.set3"),
   fill.legend = tm_legend(position = tm_pos_in("right", "top")),
   shape = "group", 
   shape.legend = tm_legend_combine("fill"))

Created on 2022-10-04 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment