1

I am trying to make a simple map with R and tmap, everything works well but I can't fully customize the legend. I have used legend.format = list(text.separator = "-") to change the text from, say "40 to 60" to "40 - 60" (i want my map legend in french so i had to drop the "to"). How can I fully customize it to display, say "De 40 à 80 k€" instead of "40 - 60" ?. Here is my code :

         tm_fill("EBE", title = "EBE (k€)", style = "fixed",
          breaks = c(40,60,80,100,110))+
          tm_borders() +
          tm_layout("EBE moyen par région",
            legend.title.size = 1,
            legend.text.size = 0.6,
            legend.position = c("left","bottom"),
            legend.format = list(text.separator = "-"),
            #legend.bg.color = "white",
            legend.bg.alpha = 1)

Here is my map :

EBE par région

Thanks in advance.

1 Answers1

2

Looks like you might be able to use the tm_add_legend https://rdrr.io/cran/tmap/man/tm_add_legend.html

I used the below code to change the labels on one of my plots where I need 'Missing' (automatic tmap label) to be changed to '0'.

tm_add_legend(type = "fill", 
    labels = c("0", "1 to 20", "21 to 40", "41 to 60", "61 to 80", "81 to 100"),
    col = c("grey", "#ffffd4", "#fed98e", "#fe9929", "#d95f0e", "#993404"),
    border.lwd = 0.5,
    title = "Count")

By the way, if you want to create more white space around the plot so that the title and legend dont overlap the actual map then you can use:

tm_layout(inner.margins = c(value,value,value,value)

(the 4 values are the for the bottom, left, top and right space)

Bananafan
  • 31
  • 3