R :: tmap 在图例中绘制和显示 NA 值
R ::tmap plot and display NA values in legend
我想用鸟类计数结果映射点数据。点的大小应根据被计数的鸟类数量进行缩放。如果一个区域没有被计算过,一个 x 应该被显示为 NA 值。我如何绘制这些数据并使用 tmap 包得出漂亮的图例?
这是一个类似的例子:
rm(list=ls(all=TRUE))
library(tmap)
data(World, metro)
# put population size pop2020 to NA for some cities
metro$pop2020[10:300] <- NA
# add column with code for the shape of the symbol (21 for data available, 4 for NA)
metro$shape_symbol <- 21
metro[is.na(metro$pop2020), ]$shape_symbol <- 4
tm_shape(World) + tm_fill()+
tm_shape(metro) +
tm_symbols(
size = "pop2020",
col = "black",
shape = "shape_symbol", # use column shape_symbol in metro for the symbol
# shapeNA = "4", # should plot NA as cross by default - didn´t work for me
title.size = "subtitle",
legend.size.is.portrait=TRUE) +
tm_layout(legend.bg.color = "gray",
legend.frame = "black")
给出了这个输出。为什么不显示 NA 值?我如何获得漂亮的图例输出?
我的目标是达到:
鸟类的名称应以粗体图例标题的形式给出,并为下面的图例符号附加标签 "Anzahl"。理想情况下,NA 的符号 x 应该像这样放置。我可以使用 title.size 从循环中粘贴 (i) 作为图例标题,但是如何在图例中获得第二个标题。
附加问题:我可以将点的大小设置在一定范围内吗?所以非常小的数字在地图上有最小尺寸?
我自己使用变通方法解决了这个问题:
我添加了一个额外的图层并仅选择了具有缺失值的数据。然后我使用 tm_add_legend 添加了一个额外的图例元素。
data(World, metro)
# put population size pop2020 to NA for some cities
metro$pop2020[10:300] <- NA
tm_shape(World) + tm_fill()+
tm_shape(metro) +
tm_symbols(
size = "pop2020",
col = "black",
title.size = "subtitle",
legend.size.is.portrait=TRUE) +
tm_shape(metro[is.na(metro$pop2020),]) +
tm_dots(shape=4, size = 0.5, border.lwd = 0.5) +
tm_layout(legend.bg.color = "gray",
legend.frame = "black") +
tm_add_legend(type="symbol", shape =4, labels = "not available", size = 0.5, border.lwd = 0.5, col = "black")
我想用鸟类计数结果映射点数据。点的大小应根据被计数的鸟类数量进行缩放。如果一个区域没有被计算过,一个 x 应该被显示为 NA 值。我如何绘制这些数据并使用 tmap 包得出漂亮的图例?
这是一个类似的例子:
rm(list=ls(all=TRUE))
library(tmap)
data(World, metro)
# put population size pop2020 to NA for some cities
metro$pop2020[10:300] <- NA
# add column with code for the shape of the symbol (21 for data available, 4 for NA)
metro$shape_symbol <- 21
metro[is.na(metro$pop2020), ]$shape_symbol <- 4
tm_shape(World) + tm_fill()+
tm_shape(metro) +
tm_symbols(
size = "pop2020",
col = "black",
shape = "shape_symbol", # use column shape_symbol in metro for the symbol
# shapeNA = "4", # should plot NA as cross by default - didn´t work for me
title.size = "subtitle",
legend.size.is.portrait=TRUE) +
tm_layout(legend.bg.color = "gray",
legend.frame = "black")
给出了这个输出。为什么不显示 NA 值?我如何获得漂亮的图例输出?
我的目标是达到:
鸟类的名称应以粗体图例标题的形式给出,并为下面的图例符号附加标签 "Anzahl"。理想情况下,NA 的符号 x 应该像这样放置。我可以使用 title.size 从循环中粘贴 (i) 作为图例标题,但是如何在图例中获得第二个标题。 附加问题:我可以将点的大小设置在一定范围内吗?所以非常小的数字在地图上有最小尺寸?
我自己使用变通方法解决了这个问题:
我添加了一个额外的图层并仅选择了具有缺失值的数据。然后我使用 tm_add_legend 添加了一个额外的图例元素。
data(World, metro)
# put population size pop2020 to NA for some cities
metro$pop2020[10:300] <- NA
tm_shape(World) + tm_fill()+
tm_shape(metro) +
tm_symbols(
size = "pop2020",
col = "black",
title.size = "subtitle",
legend.size.is.portrait=TRUE) +
tm_shape(metro[is.na(metro$pop2020),]) +
tm_dots(shape=4, size = 0.5, border.lwd = 0.5) +
tm_layout(legend.bg.color = "gray",
legend.frame = "black") +
tm_add_legend(type="symbol", shape =4, labels = "not available", size = 0.5, border.lwd = 0.5, col = "black")