手动指定tmap中图例类别的数量,包括未表示的类别
我试图为多边形地图和整数变量指定一个简单的图例,其中并非存在范围内的所有值。我需要这个,以便在可比较的地图上具有相同的图例和相同的色阶,这些地图的范围和特定值都不同。例如,任何地图上变量的最大值都是 8。我希望图例显示 0 到 8 的值,即使任何多边形都不存在 6 或任何特定值,并且我希望图例上的标签为 0 到 8(包括 0 到 8),并且没有池化。
一个可复制的例子:
library(tmap)
library(dplyr)
library(sf)
set.seed(1234)
shape <- st_read(system.file("shape/nc.shp", package ="sf")) %>%
st_transform(shape, crs = 4326)
shape$myvar <- as.integer(sample(x=c(c(base::rep(c(0,1,2,3,4),16),base::rep(5,15),c(7,7,7,7,8)))))
table(shape$myvar)
0 1 2 3 4 5 7 8
16 16 16 16 16 15 4 1
我尝试了很多变体,但在这种情况下无法获得九个类别以及相应的标签。
mymap <- tm_shape(shape) +
tm_fill(col = "myvar",
style = "cat",
breaks = c(0,1,2,3,4,5,6,7,8),
n=9)
mymap
此图例缺少值 6。
相反,使用 style = 'fixed' 选项,如下所示:
mymap <- tm_shape(shape) +
tm_fill(col = "myvar",
style = "fixed",
breaks = c(0,1,2,3,4,5,6,7,8),
n=9)
mymap
这里的图例有一个值“6”的类别,但是汇集最后两个类别。
我尝试在 tm_fill 中添加一个附加参数:
style.args = classIntervals(var=shape$myvar,n=9)
这并没有改变上一个映射的输出。
尽管我尽了最大的努力,我还是无法获得全部 9 个类别。
有人可以给我一些关于如何获得我想要的简单图例而不需要使用 GIMP 的建议吗?
I am trying to specify a simple legend for a map of polygons and an integer variable, in which not all values within the range are present. I need this in order to have the same legend, with the same color scale, on comparable maps which differ in both the range and the particular values present. For example, the maximum value of a variable is 8 on any of the maps. I want the legend to show the values 0 to 8 even when 6, or any particular value, is not present for any polygon, and I want the labels on the legend to be 0 to 8, inclusive, with no pooling.
A replicable example:
library(tmap)
library(dplyr)
library(sf)
set.seed(1234)
shape <- st_read(system.file("shape/nc.shp", package ="sf")) %>%
st_transform(shape, crs = 4326)
shape$myvar <- as.integer(sample(x=c(c(base::rep(c(0,1,2,3,4),16),base::rep(5,15),c(7,7,7,7,8)))))
table(shape$myvar)
0 1 2 3 4 5 7 8
16 16 16 16 16 15 4 1
I have tried many variations, but can't in this case get nine categories, with the corresponding labels.
mymap <- tm_shape(shape) +
tm_fill(col = "myvar",
style = "cat",
breaks = c(0,1,2,3,4,5,6,7,8),
n=9)
mymap
This legend is missing value 6.
In contrast, using the style = 'fixed' option like this:
mymap <- tm_shape(shape) +
tm_fill(col = "myvar",
style = "fixed",
breaks = c(0,1,2,3,4,5,6,7,8),
n=9)
mymap
The legend here has a category for the value '6', but pools the last two categories.
I tried putting an additional argument into tm_fill:
style.args = classIntervals(var=shape$myvar,n=9)
This didn't change the output from the previous map.
Try as I might, I can't get all 9 categories.
Could someone please give me some suggestions as to how to get the simple legend I want, without having to use GIMP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请使用
tmap
找到以下一种可能的解决方案:Reprex
由 reprex 包 (v2.0.1)
Please find below one possible solution using
tmap
:Reprex
Created on 2022-03-04 by the reprex package (v2.0.1)