簡體   English   中英

R tmap,圖例格式 - 在第一個圖例鍵中顯示單個值

[英]R tmap, legend formatting - display single value in the first legend key

我不確定如何或是否有可能以下列方式調整關鍵傳說。 考慮這個例子:

library(tmap)
data(Europe)

my_map <- 
  tm_shape(Europe) +
  tm_polygons("well_being", textNA="Non-European countries", title="Well-Being Index") +
  tm_text("iso_a3", size="AREA", root=5) +
  tm_layout(legend.position = c("RIGHT","TOP"),
            legend.frame = TRUE)

在此輸入圖像描述

我嘗試了legend.format = list(scientific = TRUE) ,例如:

my_map2 <- my_map +
  tm_layout(legend.format = list(scientific = TRUE))

這給了一個傳奇:

在此輸入圖像描述

但是,我希望的是:

在此輸入圖像描述

在我的數據中,4是零,我被要求讓它脫穎而出,僅為零。

據我所知,單獨使用tmap圖例格式是不可能的 - 總會有小分隔符。 使用來自legend.format調用的text.separator “to”或覆蓋它的任何內容。

您需要的是數據處理中的一個額外步驟 - 您必須使用標簽創建一個特殊變量,並根據此繪圖。 您可以完全控制級別,因此您可以將第一個作為獨立的零。

為簡單起見,我使用ifelse分為兩個級別(加上非歐洲國家的NA),但你可以得到更多的幻想......

Europe <- Europe %>%
  st_as_sf() %>%  # from sf package - makes Europe behave as a data.frame
  mutate(well_being_adj = ifelse(well_being < 5, "0", "more than five"))

my_map <- 
  tm_shape(Europe) +
  tm_polygons("well_being_adj", textNA="Non-European countries", title="Well-Being Index") +
  tm_text("iso_a3", size="AREA", root=5) +
  tm_layout(legend.position = c("RIGHT","TOP"),
            legend.frame = TRUE)
print(my_map)

在此輸入圖像描述

盡管已經解決了,但另一個更實用的替代方法是修改tm_polygons labels的級別

tm_polygons(labels = c ("0", "more than five")

這種方式也適用於柵格文件

暫無
暫無

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

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