[英]duplicated colors in legend of tmap object in R
有没有人在R
遇到过这样的tmap
问题? 见下图。 无论我尝试哪种风格( pretty
、 kmeans
、 jenks
等等),图例中总是有重复的颜色。 我也尝试设置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.