手动指定tmap中图例类别的数量,包括未表示的类别

发布于 2025-01-11 17:27:33 字数 1549 浏览 0 评论 0原文

我试图为多边形地图和整数变量指定一个简单的图例,其中并非存在范围内的所有值。我需要这个,以便在可比较的地图上具有相同的图例和相同的色阶,这些地图的范围和特定值都不同。例如,任何地图上变量的最大值都是 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

此图例缺少值 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

Map_w_style_fixed

这里的图例有一个值“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

Map legend missing value 6

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

Map_w_style_fixed

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

残花月 2025-01-18 17:27:33

请使用 tmap 找到以下一种可能的解决方案:

Reprex

  • 您的数据
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)))))
  • 建议的代码
mymap <- tm_shape(shape) +
  tm_fill(col = "myvar",
          style = "fixed",
          breaks = c(0,1,2,3,4,5,6,7,8,+Inf), 
          labels = c("0","1","2","3","4","5","6","7","8")) +
  tm_layout(inner.margins = c(0.02, 0.08, 0.02, 0.02))

mymap

在此处输入图像描述

reprex 包 (v2.0.1)

Please find below one possible solution using tmap:

Reprex

  • Your data
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)))))
  • Suggested code
mymap <- tm_shape(shape) +
  tm_fill(col = "myvar",
          style = "fixed",
          breaks = c(0,1,2,3,4,5,6,7,8,+Inf), 
          labels = c("0","1","2","3","4","5","6","7","8")) +
  tm_layout(inner.margins = c(0.02, 0.08, 0.02, 0.02))

mymap

enter image description here

Created on 2022-03-04 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文