首页 > 解决方案 > 使用 mtext 函数时的 'pos' 等效项

问题描述

我通常使用 mtext 为图形添加标签。我经常希望在图形的左上角绘制标签。使用 par('usr')[1] 很容易,只是文本居中而不是我想要的右向。使用“文本”时,我会使用 pos=4,但对于 mtext,似乎没有等效的。例子:

plot(1)
text(par('usr')[1], 1.5, "test", xpd=TRUE, pos=4)
mtext('test', 3, at=par('usr')[1])

我想避免简单地将值添加到 at= 或使用 adj=,因为我经常用不同的轴绘制多个面板。

标签: rplottext

解决方案


adj=在基图中使用:

plot(1)
usr <- par("usr")
text(usr[1], usr[3], adj = c(-0.1, -0.1), "lower-left")
text(usr[2], usr[3], adj = c( 1.1, -0.1), "lower-right")
text(usr[1], usr[4], adj = c(-0.1,  1.1), "upper-left")
text(usr[2], usr[4], adj = c( 1.1,  1.1), "upper-right")

带有 adj 的文字

来自?text

 adj: one or two values in [0, 1] which specify the x (and
      optionally y) adjustment ('justification') of the labels,
      with 0 for left/bottom, 1 for right/top, and 0.5 for
      centered.  On most devices values outside [0, 1] will also
      work.  See below.

我使用-0.1and1.1从边缘线本身插入一点。该.1组件可能需要根据绘图的其余部分进行调整。


推荐阅读