更改绘图文本的对齐方式

Change alignment of plot text

向绘图添加文本时,默认位置在指定坐标处居中。例如,

plot(1:10, 1:10)
text(x = 6, y = 1, "text")

在此图中,文本从 x = 5, y = 1 左右开始,但我希望它 左对齐 ,即从 x = 6, y = 1 开始。如何更改对齐方式? 谢谢

text 的对齐(调整/对齐)由 adj 参数设置。为 left-align,设置 adj = 0.

来自?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.

adj allows adjustment of the text position with respect to (x, y). Values of 0, 0.5, and 1 specify that (x, y) should align with the left/bottom, middle and right/top of the text, respectively. The default is for centered text, i.e., adj = c(0.5, NA). Accurate vertical centering needs character metric information on individual characters which is only available on some devices. Vertical alignment is done slightly differently for character strings and for expressions: adj = c(0,0) means to left-justify and to align on the baseline for strings but on the bottom of the bounding box for expressions. This also affects vertical centering: for strings the centering excludes any descenders whereas for expressions it includes them. Using NA for strings centers them, including descenders.

plot(1:10, 1:10)
text(x = 6, y = 1, "text", adj = 0)

adj = 0 将 left-align 文本: