Reputation: 1210
When adding text to a plot, the default position is centered at the specified coordinates. For example,
plot(1:10, 1:10)
text(x = 6, y = 1, "text")
In this plot, the text starts at around x = 5, y = 1
, but I want it to be left-aligned, i.e. start at x = 6, y = 1
. How can I change the alignment?
Thank you
Upvotes: 2
Views: 1935
Reputation: 1210
Alignment (adjustment / justification) of text
is set by the adj
argument. To left-align, set adj = 0
.
From ?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. UsingNA
for strings centers them, including descenders.
plot(1:10, 1:10)
text(x = 6, y = 1, "text", adj = 0)
adj = 0
will left-align the text:
Upvotes: 2