簡體   English   中英

自定義 R tmap 圖例值的打印方式

[英]Customize how R tmap legend values are printed

有沒有辦法自定義 R tmap 圖例值的打印方式? 代替:

在此處輸入圖片說明

...我想實現以下分類,以避免打印兩次相同的值:

248-500
50 1 -1000 (501 而不是 500)
100 1 -10000 (1001 而不是 1000)
1000 1 -20000 (10001 而不是 10000)
2000 1 -21588 (20001 而不是 20000)

此外,我想將所有“-”打印在彼此的正下方,以便所有十、百和千值都可以輕松進行比較。 這張圖片說明了我想要達到的結果:

在此處輸入圖片說明

這是我的示例代碼,它尚未工作:

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

要詳細說明@orlando-sabogal 的答案 - 因為問題包括圖例居中和數千人的分離 - 考慮以下代碼:

它使用包括圖例文本的居中(盡管小破折號沒有完全對齊;你需要一個固定寬度的字體),並將prettyNum()應用於圖例以清楚地分隔數千個。

如果您遵循美國(而非歐洲)編號約定,請隨意替換空格:)

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

在此處輸入圖片說明

我認為您的問題是您在 tm_add_legend() 函數中而不是在 tm_fill() 函數中包含了break2標簽。

我認為以下是您需要的:

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

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM