kalakaru
kalakaru

Reputation: 131

Customize how R tmap legend values are printed

Is there a way to customize how R tmap legend values are printed? Instead of:

enter image description here

...I want to achieve the following classification in order to avoid printing two times the same value:

248-500
501-1000 (501 instead of 500)
1001-10000 (1001 instead of 1000)
10001-20000 (10001 instead of 10000)
20001-21588 (20001 instead of 20000)

Also I would like to print all "-" exactly beneath each other so that all tens, hundreds and thousend values are easily comparable. This image illustrates the result I want to achieve:

enter image description here

Here is my example code which does not yet work:

library(sf)
library(tmap)

mybreaks1 = c(248, 500, 1000, 10000, 20000, 21588) 
mybreaks2 = c()

for (i in 1:length(mybreaks1)-1){
  print(i)
  if (i == 1){
  mybreaks2[i] <- paste0(mybreaks1[i], " - ", mybreaks1[i+1])
  }
  if (i >1){
  mybreaks2[i] <- paste0(mybreaks1[i]+1, " - ", mybreaks1[i+1])
}
}

mybreaks2

nc <- st_read(system.file("shape/nc.shp", package="sf"))

myplot <- tm_shape(nc) + 
          tm_fill("BIR74",
          palette="Set1", #  tmaptools::palette_explorer()
          style="fixed",
          breaks= mybreaks) + 
          tm_borders(
          col = "grey40", 
          lwd = 1, 
          lty = "solid", 
          alpha = NA) +
          tm_layout(
          legend.show = TRUE,
          frame = FALSE,
          legend.outside = TRUE,
          legend.position = c("right","top")
          #legend.just = "middle", 
          #legend.format=list(fun=function(x) formatC(x, digits=0, format="d"), text.separator= "-", text.align = "left") 
          ) +
          tm_add_legend(labels= mybreaks2,  col = c("red", "orange", "green", "blue", "purple"))
myplot

Upvotes: 1

Views: 1014

Answers (2)

Jindra Lacko
Jindra Lacko

Reputation: 8719

To elaborate a little on @orlando-sabogal answer - since the question included legend centering and separation of thousands - consider this code:

It uses includes centering of the legend text (the little dashes are not exactly aligned though; you would need a fixed width font for that) and prettyNum() is applied to the legend to clearly separate thousands.

Feel free to replace the space if you follow American (and not European) numbering conventions :)

library(tmap)
library(sf)

mybreaks1 = c(248, 500, 1000, 10000, 20000, 21588) 
mybreaks2 = c()

for (i in 1:length(mybreaks1)-1){
  print(i)
  if (i == 1){
    mybreaks2[i] <- paste0(mybreaks1[i], " - ", mybreaks1[i+1])
  }
  if (i >1){
    mybreaks2[i] <- paste0(prettyNum(mybreaks1[i]+1, big.mark = " "), " - ", prettyNum(mybreaks1[i+1], big.mark = " "))
  }
}

mybreaks2

nc <- st_read(system.file("shape/nc.shp", package="sf"))

myplot <- tm_shape(nc) + 
  tm_fill("BIR74",
          palette="Set1", #  tmaptools::palette_explorer()
          style="fixed",
          breaks= mybreaks1,
          labels = mybreaks2) + 
  tm_borders(
    col = "grey40", 
    lwd = 1, 
    lty = "solid", 
    alpha = NA) +
  tm_layout(
    legend.show = TRUE,
    frame = FALSE,
    legend.outside = TRUE,
    legend.format = list(text.align = "center"),
    legend.position = c("right","top"))

myplot

enter image description here

Upvotes: 2

Orlando Sabogal
Orlando Sabogal

Reputation: 1630

I think your issue is that you are including breaks2 labels in the tm_add_legend() function and not in the tm_fill() function.

I think the following is what you need:

mybreaks1 = c(248, 500, 1000, 10000, 20000, 21588) 
mybreaks2 = c()

for (i in 1:length(mybreaks1)-1){
  print(i)
  if (i == 1){
    mybreaks2[i] <- paste0(mybreaks1[i], " - ", mybreaks1[i+1])
  }
  if (i >1){
    mybreaks2[i] <- paste0(mybreaks1[i]+1, " - ", mybreaks1[i+1])
  }
}

mybreaks2

nc <- st_read(system.file("shape/nc.shp", package="sf"))

myplot <- tm_shape(nc) + 
  tm_fill("BIR74",
          palette="Set1", #  tmaptools::palette_explorer()
          style="fixed",
          breaks= mybreaks1,
          labels = mybreaks2) + 
  tm_borders(
    col = "grey40", 
    lwd = 1, 
    lty = "solid", 
    alpha = NA) +
  tm_layout(
    legend.show = TRUE,
    frame = FALSE,
    legend.outside = TRUE,
    legend.position = c("right","top"))

myplot

enter image description here

Upvotes: 2

Related Questions