简体   繁体   中英

Customize how R tmap legend values are printed

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

在此处输入图片说明

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

248-500
50 1 -1000 (501 instead of 500)
100 1 -10000 (1001 instead of 1000)
1000 1 -20000 (10001 instead of 10000)
2000 1 -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:

在此处输入图片说明

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

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

在此处输入图片说明

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM