簡體   English   中英

R中tmap對象圖例中的重復顏色

[英]duplicated colors in legend of tmap object in R

有沒有人在R遇到過這樣的tmap問題? 見下圖。 無論我嘗試哪種風格( prettykmeansjenks等等),圖例中總是有重復的顏色。 我也嘗試設置midpoint = NA ,但問題仍然存在。

這是我用來創建這樣一個情節的代碼。 代碼下方是用於繪圖的向量。 抱歉, dput似乎不適用於sf對象。 請隨意使用任何空間數據來復制示例。 反饋總是appriciated!

tm_shape(sfpr) + tm_borders(col = "gray") + 
  tm_polygons(col = 'Pperc', 
              style = "kmeans",
#              midpoint = NA,
              palette = viridis(4, begin = 0.48, end = 1)) + 
  tm_layout(inner.margins = c(0.1, 0.15, 0.1, 0.1), 
            legend.title.size = 1.5, 
            legend.text.size = 1.1, 
            legend.position = c("left", "bottom"), 
            legend.format = list(digits = 0), 
            main.title = "Maps on the reduction of phosphate losses (in 1000 lbs)", 
            main.title.position = "center")
> sfpr$Pperc
 [1]   -1.49   -0.02    0.12    0.27   -0.36 -247.86  -21.74   -8.88   28.63  -14.48   -0.56

在此處輸入圖片說明

該問題是由數據集中的負值和正值引起的。 如果您設置auto.palette.mapping = T (不推薦使用)或使用不同的數值作為midpoint這將解決您的問題。 我根據您的最小值/最大值生成了一些虛擬數據。 順便說一句,您應該使用tm_borders() + tm_fill()或僅使用tm_polygons()因為后者填充多邊形並繪制輪廓。

library(tidyverse)
library(tmap)
library(sf)
library(urbnmapr)
library(viridis)

# create dummy data
sfpr <- get_urbn_map("states", sf = T) %>%  
  as.tibble() %>% 
  mutate(Pperc = runif(51, -248 ,29)) %>% 
  st_as_sf() 

tm_shape(sfpr) +
  tm_borders(col = "gray") + 
  tm_fill("Pperc",
          style = "kmeans",
          palette = viridis(4, begin = 0.48, end = 1), 
          auto.palette.mapping = T) +
  tm_layout(inner.margins = c(0.1, 0.15, 0.1, 0.1), 
            legend.title.size = 1.5, 
            legend.text.size = 1.1, 
            legend.position = c("left", "bottom"), 
            legend.format = list(digits = 0), 
            main.title = "Maps on the reduction of phosphate losses (in 1000 lbs)", 
            main.title.position = "center")

在此處輸入圖片說明

暫無
暫無

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

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