Last chance! 50% off unlimited learning
Sale ends in
text
draws the strings given in the vector labels
at the
coordinates given by x
and y
.
y
may be missing since xy.coords(x, y)
is used for
construction of the coordinates.
text(x, …)# S3 method for default
text (x, y = NULL, labels = seq_along(x$x), adj = NULL,
pos = NULL, offset = 0.5, vfont = NULL,
cex = 1, col = NULL, font = NULL, …)
numeric vectors of coordinates where the text
labels
should be written. If the length of x
and
y
differs, the shorter one is recycled.
a character vector or expression specifying
the text to be written. An attempt is made to coerce other
language objects (names and calls) to expressions, and vectors and
other classed objects to character vectors by as.character
.
If labels
is longer than x
and
y
, the coordinates are recycled to the length of labels
.
one or two values in
a position specifier for the text. If specified this
overrides any adj
value given. Values of 1
,
2
, 3
and 4
, respectively indicate
positions below, to the left of, above and to the right of
the specified (x,y)
coordinates.
when pos
is specified, this value controls the
distance (‘offset’) of the text label from the specified
coordinate in fractions of a character width.
NULL
for the current font family, or a character
vector of length 2 for Hershey vector fonts. The first element of
the vector selects a typeface and the second element selects a
style. Ignored if labels
is an expression.
numeric character expansion factor; multiplied
by par("cex")
yields the final character size.
NULL
and NA
are equivalent to 1.0
.
the color and (if vfont = NULL
) font to be
used, possibly vectors. These default to the values of the global
graphical parameters in par()
.
further graphical parameters (from par
),
such as srt
, family
and xpd
.
The Euro symbol may not be available in older fonts. In current
versions of Adobe symbol fonts it is character 160, so text(x,
y, "\xA0", font = 5)
may work. People using Western European locales
on Unix-alikes can probably select ISO-8895-15 (Latin-9) which has the
Euro as character 165: this can also be used for
postscript
and pdf
. It is \u20ac in
Unicode, which can be used in UTF-8 locales.
The Euro should be rendered correctly by X11
in UTF-8
locales, but the corresponding single-byte encoding in
postscript
and pdf
will need to be selected
as ISOLatin9.enc
(and the font will need to contain the Euro
glyph, which for example older printers may not).
In all the European Windows encodings the Euro is symbol 128 and
\u20ac will work in all locales: however not all fonts will
include it. It is not in the symbol font used for
windows
and related devices, including the Windows printer.
labels
must be of type character
or
expression
(or be coercible to such a type).
In the latter case, quite a bit of
mathematical notation is available such as sub- and superscripts,
greek letters, fractions, etc.
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.
The pos
and offset
arguments can be used in conjunction
with values returned by identify
to recreate an interactively
labelled plot.
Text can be rotated by using graphical parameters srt
(see par
). When adj
is specified, a non-zero
srt
rotates the label about (x, y)
. If pos
is
specified, srt
rotates the text about the point on its bounding
box which is closest to (x, y)
: top center for pos = 1
,
right center for pos = 2
, bottom center for pos = 3
, and
left center for pos = 4
. The pos
interface is not as
useful for rotated text because the result is no longer centered
vertically or horizontally with respect to (x, y)
. At present
there is no interface in the graphics package for directly
rotating text about its center which is achievable however by fiddling
with adj
and srt
simultaneously.
Graphical parameters col
, cex
and font
can be
vectors and will then be applied cyclically to the labels
(and
extra values will be ignored). NA
values of font
are
replaced by par("font")
, and similarly for col
.
Labels whose x
, y
or labels
value is NA
are omitted from the plot.
What happens when font = 5
(the symbol font) is selected can be
both device- and locale-dependent. Most often labels
will be
interpreted in the Adobe symbol encoding, so e.g."d"
is delta, and "\300"
is aleph.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.
Murrell, P. (2005) R Graphics. Chapman & Hall/CRC Press.
text.formula
for the formula method;
mtext
, title
,
Hershey
for details on Hershey vector fonts,
plotmath
for details and more examples on
mathematical annotation.
# NOT RUN {
plot(-1:1, -1:1, type = "n", xlab = "Re", ylab = "Im")
K <- 16; text(exp(1i * 2 * pi * (1:K) / K), col = 2)
## The following two examples use latin1 characters: these may not
## appear correctly (or be omitted entirely).
plot(1:10, 1:10, main = "text(...) examples\n~~~~~~~~~~~~~~",
sub = "R is GNU <U+00A9>, but not <U+00AE> ...")
mtext("<U+00AB>Latin-1 accented chars<U+00BB>: <U+00E9><U+00E8> <U+00F8><U+00D8> <U+00E5><<U+00C5> <U+00E6><<U+00C6>", side = 3)
points(c(6,2), c(2,1), pch = 3, cex = 4, col = "red")
text(6, 2, "the text is CENTERED around (x,y) = (6,2) by default",
cex = .8)
text(2, 1, "or Left/Bottom - JUSTIFIED at (2,1) by 'adj = c(0,0)'",
adj = c(0,0))
text(4, 9, expression(hat(beta) == (X^t * X)^{-1} * X^t * y))
text(4, 8.4, "expression(hat(beta) == (X^t * X)^{-1} * X^t * y)",
cex = .75)
text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n)))
## Two more latin1 examples
text(5, 10.2,
"Le fran<U+00E7>ais, c'est fa<U+00E7>ile: R<U+00E8>gles, Libert<U+00E9>, Egalit<U+00E9>, Fraternit<U+00E9>...")
text(5, 9.8,
"Jetz no chli z<U+00FC>rit<U+00FC><U+00FC>tsch: (noch ein bi<U+00DF>chen Z<U+00FC>rcher deutsch)")
# }
Run the code above in your browser using DataLab