tmap tm_dots 用户定义形状列的行为

tmap tm_dots behavior on user defined shape column

tm dots 似乎对标识给定观察的所需形状的列有问题

来自形状枚举:

0 = 开方

1 = 空心圆

22 = 实心方形

21 = 空心圆

当我将形状参数设置为 'shapeCol' 其中 shapeCol 是一列 0 / 1(空心形状),它 returns 一个填充但其他方面正确的形状

当我手动设置形状为0时 它 returns 正确的开放形状,但我需要这个形状随观察而变化

创建随机空间数据的数据框

library(sf)
library(tmap)
library(dplyr)

newDf <- data.frame(location=letters[1:30],
                lat=32+runif(30,0.01,0.03),
                lon=-97+runif(30,.01,.03)) %>%
                mutate(rowID=1:n(),
                reservoir=case_when(rowID<=15 ~ 'Codell',
                TRUE ~'Niobrara'))

newSf <- st_as_sf(newDf,coords=c('lon','lat'),crs=4326)

分配形状值/数字索引

shapes <- c('Niobrara'='circle',
            'Codell'='square')

shapeVals <- c('circle'=21,'square'=22,'triangleup'=24,'diamond'=23,'triangleDown'=25)
borderVals <- c('circle'=0,'square'=1,'triangleup'=2,'diamond'=5,'triangleDown'=6)


newSf.fin <- newSf %>% mutate(shapeType = shapes[reservoir],
                           shapeCol = as.factor(shapeVals[shapeType]),
                           borderCol = as.factor(borderVals[shapeType]))

newSf.fin %>% select(borderCol) # 0 and 1 / Open Shapes

#returns filled shapes despite shape column only referencing open values
tm_shape(newSf.fin) + tm_dots(shape='borderCol',size=2)

#returns open shapes by manually setting shape value
tm_shape(newSf.fin) + tm_dots(shape=0,size=2)

我知道这可以通过 map/looping 来缓解,但色阶会更成问题且更困难。有没有一种方法可以根据列值实现正确的开放形状?

我不是 tmap 专家,但这似乎是一种误解。

文档中提到了 shape 参数:

shape(s) of the symbol. Either direct shape specification(s) or a data variable name(s) that is mapped to the symbols specified by the shapes argument. See details for the shape specification.

关于 shapes 论点,它说:

palette of symbol shapes. Only applicable if shape is a (vector of) categorical variable(s). See details for the shape specification. By default, the filled symbols 21 to 25 are taken.

因此,当您执行 shape = 0 时,您正在执行直接形状规范。这就是它起作用的原因。当您使用变量名称时,您需要将其类别映射到 shapes 参数。你没有这样做,所以它需要 21 和 22,这是它的默认形状,它们是实心方形和实心圆。

这就是我的工作方式:

tm_shape(newSf.fin) + tm_dots(shape= 'borderCol',size=2, shapes = c(1, 0))