繁体   English   中英

R :: tmap绘图并在图例中显示NA值

[英]R ::tmap plot and display NA values in legend

我想将点数据与鸟类计数的结果相对应。 点数的大小应根据被计数的鸟的数量进行缩放。 如果尚未计算面积,则应为NA值显示x。 我该如何绘制这些数据并使用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应该这样放置。 我可以从循环中粘贴(i)作为带有title.size的图例标题,但是如何在图例中获得第二个标题。 附加问题:我可以将点的大小设置为一定范围吗? 这样,很小的数字在地图上的大小就会达到最小?

我自己使用一种解决方法解决了它:

我添加了一个额外的层,并仅选择了缺少值的数据。 然后,我使用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") 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM