“零长度箭头”有多小?

Mic*_*ico 4 plot r

如果您arrows经常使用该功能,您可能会遇到以下警告:

set.seed(438520)
N = 1000
x = rnorm(N, sd = .1)
y = rnorm(N)

png('~/Desktop/arrows.png', height = 240, width = 240)
plot(NA, xlim = c(-1, 1), ylim = c(-3, 3))
arrows(x[-N], y[-N], x[-1L], y[-1L])
dev.off()
Run Code Online (Sandbox Code Playgroud)

警告消息:在箭头(x[-N], y[-N], x[-1L], y[-1L]) 中:零长度箭头的角度不确定,因此跳过

显示箭头的杂乱图; x 轴标记为“索引”,y 轴标记为“NA”,点位于垂直方向的椭圆中。

我们如何确定哪些箭头有问题,以便按照我们认为合适的方式处理它们?

Mic*_*ico 6

答案在?arrows

零长度箭头的方向是不确定的,因此箭头的方向也是不确定的。考虑到舍入误差,任何长度小于 1/1000 英寸的箭头都省略了箭头(带有警告)。

这当然引出了一个问题——什么是英寸?

问答集中在一个相关的问题上,但学习也可以在这里应用:

png('~/Desktop/arrows.png', height = 240, width = 240)
plot(NA, xlim = c(-1, 1), ylim = c(-3, 3))

# get each arrow's length by converting x and y coords to inches
units = par(c('usr', 'pin'))
x_to_inches = with(units, pin[1L]/diff(usr[1:2]))
y_to_inches = with(units, pin[2L]/diff(usr[3:4]))

dists = sqrt((x_to_inches * diff(x))**2 + (y_to_inches * diff(y))**2)

# which arrows are the culprits?
idx = which(dists < .001)

# option: remove the arrow base & head from the culprit pair(s)
arrows(x[-c(N, idx)], y[-c(N, idx)], 
       x[-c(1L, idx + 1L)], y[-c(1L, idx + 1L)])
dev.off()
Run Code Online (Sandbox Code Playgroud)

你可以看到这里在R源,这种做法是几乎相同的使用(在C级)生成摆在首位此警告。

有些东西总是困扰着我,但永远不足以坐下来解决它。