对于参数 mgp[3] 的非整数值,函数 axis 的行为异常

Function `axis` behaves unexpectedly for noninteger values of parameter `mgp[3]`

?par 关于 mgp[1:3] 的说法:

The margin line (in mex units) for the axis title, axis labels and axis line. Note that mgp[1] affects title whereas mgp[2:3] affect axis. The default is c(3, 1, 0).

?axis 说了类似的话。但是,刻度标签似乎 而不是 通常放在 mgp[2] 行,而是放在 mgp[2] + (mgp[3] %% 1) 行。因此,axis 似乎仅在 mgp[3] 是整数时才表现得像记录的那样。这是 axis 中的错误,还是其他原因?

这是一个示例,显示了 axis 的整数和非整数 mgp[3] 的预期和实际输出:

par(mar = c(5, 1, 5, 1))
plot.new()
plot.window(xlim = c(0, 1), ylim = c(0, 1))
box(lty = 3)

## 'axis' puts line and labels in right place when 'mgp[3]' is integer
mgp_bottom <- c(0, 2.5, 1) # 'mgp[1]' is arbitrary
axis(side = 1, mgp = mgp_bottom)
mtext(c("labels", "line"), side = 1, line = mgp_bottom[2:3]) 

## 'axis' puts line in right place when 'mgp[3]' is noninteger,
## but apparently not labels
mgp_top <- mgp_bottom - c(0, 0, 1e-3)
axis(side = 3, mgp = mgp_top)
mtext(c("labels", "line"), side = 3, line = mgp_top[2:3])
mtext("LABELS", side = 3, line = mgp_top[2] + (mgp_top[3] %% 1))

这看起来确实像一个错误。我认为它发生在这里: https://github.com/wch/r-source/blob/6e61247f042985d5cb9f09034cb9e694a69082e0/src/library/graphics/src/plot.c#L944-L948 。在此上下文中,gpptr(dd)->mgp[2]par("mgp")[3] 的值,并且是双精度值。这里转为int,去掉了小数部分。

我不太明白这个计算的意图,所以我不确定简单地将 lineoff 更改为 double 不会在其他地方引起问题。

这已在 PR#18194 中报告给 R 的 bugzilla,即, https://bugs.r-project.org/show_bug.cgi?id=18194

现在已在 R 源 svn 80947 中修复。