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

Viewed 101

?par says this about 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 says something similar. However, it seems that tick labels are not in general placed at line mgp[2], but rather at line mgp[2] + (mgp[3] %% 1). Thus, axis seems to behave as documented only when mgp[3] is an integer. Is this a bug in axis, or is something else going on?

Here is an example showing expected and actual output of axis for integer and noninteger 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))

enter image description here

2 Answers
Related