EVOLUTION-MANAGER
Edit File: text.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Add Text to a Plot</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for text {graphics}"><tr><td>text {graphics}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Add Text to a Plot</h2> <h3>Description</h3> <p><code>text</code> draws the strings given in the vector <code>labels</code> at the coordinates given by <code>x</code> and <code>y</code>. <code>y</code> may be missing since <code><a href="../../grDevices/html/xy.coords.html">xy.coords</a>(x, y)</code> is used for construction of the coordinates. </p> <h3>Usage</h3> <pre> text(x, ...) ## Default S3 method: text(x, y = NULL, labels = seq_along(x$x), adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, cex = 1, col = NULL, font = NULL, ...) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x, y</code></td> <td> <p>numeric vectors of coordinates where the text <code>labels</code> should be written. If the length of <code>x</code> and <code>y</code> differs, the shorter one is recycled.</p> </td></tr> <tr valign="top"><td><code>labels</code></td> <td> <p>a character vector or <a href="../../base/html/expression.html">expression</a> specifying the <em>text</em> 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 <code><a href="../../base/html/character.html">as.character</a></code>. If <code>labels</code> is longer than <code>x</code> and <code>y</code>, the coordinates are recycled to the length of <code>labels</code>.</p> </td></tr> <tr valign="top"><td><code>adj</code></td> <td> <p>one or two values in <i>[0, 1]</i> 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 <i>[0, 1]</i> will also work. See below.</p> </td></tr> <tr valign="top"><td><code>pos</code></td> <td> <p>a position specifier for the text. If specified this overrides any <code>adj</code> value given. Values of <code>1</code>, <code>2</code>, <code>3</code> and <code>4</code>, respectively indicate positions below, to the left of, above and to the right of the specified <code>(x,y)</code> coordinates.</p> </td></tr> <tr valign="top"><td><code>offset</code></td> <td> <p>when <code>pos</code> is specified, this value controls the distance (‘offset’) of the text label from the specified coordinate in fractions of a character width.</p> </td></tr> <tr valign="top"><td><code>vfont</code></td> <td> <p><code>NULL</code> 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 <code>labels</code> is an expression.</p> </td></tr> <tr valign="top"><td><code>cex</code></td> <td> <p>numeric <b>c</b>haracter <b>ex</b>pansion factor; multiplied by <code><a href="par.html">par</a>("cex")</code> yields the final character size. <code>NULL</code> and <code>NA</code> are equivalent to <code>1.0</code>.</p> </td></tr> <tr valign="top"><td><code>col, font</code></td> <td> <p>the color and (if <code>vfont = NULL</code>) font to be used, possibly vectors. These default to the values of the global <a href="par.html">graphical parameters</a> in <code><a href="par.html">par</a>()</code>.</p> </td></tr> <tr valign="top"><td><code>...</code></td> <td> <p>further <a href="par.html">graphical parameters</a> (from <code><a href="par.html">par</a></code>), such as <code>srt</code>, <code>family</code> and <code>xpd</code>.</p> </td></tr> </table> <h3>Details</h3> <p><code>labels</code> must be of type <code><a href="../../base/html/character.html">character</a></code> or <code><a href="../../base/html/expression.html">expression</a></code> (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. </p> <p><code>adj</code> allows <em>adj</em>ustment of the text position with respect to <code>(x, y)</code>. Values of 0, 0.5, and 1 specify that <code>(x, y)</code> should align with the left/bottom, middle and right/top of the text, respectively. The default is for centered text, i.e., <code>adj = c(0.5, NA)</code>. 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: <code>adj = c(0,0)</code> 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 <code>NA</code> for strings centers them, including descenders. </p> <p>The <code>pos</code> and <code>offset</code> arguments can be used in conjunction with values returned by <code>identify</code> to recreate an interactively labelled plot. </p> <p>Text can be rotated by using <a href="par.html">graphical parameters</a> <code>srt</code> (see <code><a href="par.html">par</a></code>). When <code>adj</code> is specified, a non-zero <code>srt</code> rotates the label about <code>(x, y)</code>. If <code>pos</code> is specified, <code>srt</code> rotates the text about the point on its bounding box which is closest to <code>(x, y)</code>: top center for <code>pos = 1</code>, right center for <code>pos = 2</code>, bottom center for <code>pos = 3</code>, and left center for <code>pos = 4</code>. The <code>pos</code> interface is not as useful for rotated text because the result is no longer centered vertically or horizontally with respect to <code>(x, y)</code>. At present there is no interface in the <span class="pkg">graphics</span> package for directly rotating text about its center which is achievable however by fiddling with <code>adj</code> and <code>srt</code> simultaneously. </p> <p>Graphical parameters <code>col</code>, <code>cex</code> and <code>font</code> can be vectors and will then be applied cyclically to the <code>labels</code> (and extra values will be ignored). <code>NA</code> values of <code>font</code> are replaced by <code>par("font")</code>, and similarly for <code>col</code>. </p> <p>Labels whose <code>x</code>, <code>y</code> or <code>labels</code> value is <code>NA</code> are omitted from the plot. </p> <p>What happens when <code>font = 5</code> (the symbol font) is selected can be both device- and locale-dependent. Most often <code>labels</code> will be interpreted in the Adobe symbol encoding, so e.g. <code>"d"</code> is delta, and <code>"\300"</code> is aleph. </p> <h3>Euro symbol</h3> <p>The Euro symbol may not be available in older fonts. In current versions of Adobe symbol fonts it is character 160, so <code>text(x, y, "\xA0", font = 5)</code> 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 <code><a href="../../grDevices/html/postscript.html">postscript</a></code> and <code><a href="../../grDevices/html/pdf.html">pdf</a></code>. It is <span class="samp">\u20ac</span> in Unicode, which can be used in UTF-8 locales. </p> <p>The Euro should be rendered correctly by <code><a href="../../grDevices/html/x11.html">X11</a></code> in UTF-8 locales, but the corresponding single-byte encoding in <code><a href="../../grDevices/html/postscript.html">postscript</a></code> and <code><a href="../../grDevices/html/pdf.html">pdf</a></code> will need to be selected as <code>ISOLatin9.enc</code> (and the font will need to contain the Euro glyph, which for example older printers may not). </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <p>Murrell, P. (2005) <em>R Graphics</em>. Chapman & Hall/CRC Press. </p> <h3>See Also</h3> <p><code><a href="plot.formula.html">text.formula</a></code> for the formula method; <code><a href="mtext.html">mtext</a></code>, <code><a href="title.html">title</a></code>, <code><a href="../../grDevices/html/Hershey.html">Hershey</a></code> for details on Hershey vector fonts, <code><a href="../../grDevices/html/plotmath.html">plotmath</a></code> for details and more examples on mathematical annotation. </p> <h3>Examples</h3> <pre> 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 ©, but not ® ...") mtext("«Latin-1 accented chars»: éè øØ å<Å æ<Æ", 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çais, c'est façile: Règles, Liberté, Egalité, Fraternité...") text(5, 9.8, "Jetz no chli züritüütsch: (noch ein bißchen Zürcher deutsch)") </pre> <hr /><div style="text-align: center;">[Package <em>graphics</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>