EVOLUTION-MANAGER
Edit File: plotmath.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: Mathematical Annotation in R</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 plotmath {grDevices}"><tr><td>plotmath {grDevices}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Mathematical Annotation in R</h2> <h3>Description</h3> <p>If the <code>text</code> argument to one of the text-drawing functions (<code><a href="../../graphics/html/text.html">text</a></code>, <code><a href="../../graphics/html/mtext.html">mtext</a></code>, <code><a href="../../graphics/html/axis.html">axis</a></code>, <code><a href="../../graphics/html/legend.html">legend</a></code>) in <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> is an expression, the argument is interpreted as a mathematical expression and the output will be formatted according to TeX-like rules. Expressions can also be used for titles, subtitles and x- and y-axis labels (but not for axis labels on <code>persp</code> plots). </p> <p>In most cases other language objects (names and calls, including formulas) are coerced to expressions and so can also be used. </p> <h3>Details</h3> <p>A mathematical expression must obey the normal rules of syntax for any <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> expression, but it is interpreted according to very different rules than for normal <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> expressions. </p> <p>It is possible to produce many different mathematical symbols, generate sub- or superscripts, produce fractions, etc. </p> <p>The output from <code>demo(plotmath)</code> includes several tables which show the available features. In these tables, the columns of grey text show sample <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> expressions, and the columns of black text show the resulting output. </p> <p>The available features are also described in the tables below: </p> <table summary="Rd table"> <tr> <td style="text-align: left;"> <b>Syntax</b> </td><td style="text-align: left;"> <b>Meaning</b> </td> </tr> <tr> <td style="text-align: left;"> <code>x + y</code> </td><td style="text-align: left;"> x plus y </td> </tr> <tr> <td style="text-align: left;"> <code>x - y</code> </td><td style="text-align: left;"> x minus y </td> </tr> <tr> <td style="text-align: left;"> <code>x*y</code> </td><td style="text-align: left;"> juxtapose x and y </td> </tr> <tr> <td style="text-align: left;"> <code>x/y</code> </td><td style="text-align: left;"> x forwardslash y </td> </tr> <tr> <td style="text-align: left;"> <code>x %+-% y</code> </td><td style="text-align: left;"> x plus or minus y </td> </tr> <tr> <td style="text-align: left;"> <code>x %/% y</code> </td><td style="text-align: left;"> x divided by y </td> </tr> <tr> <td style="text-align: left;"> <code>x %*% y</code> </td><td style="text-align: left;"> x times y </td> </tr> <tr> <td style="text-align: left;"> <code>x %.% y</code> </td><td style="text-align: left;"> x cdot y </td> </tr> <tr> <td style="text-align: left;"> <code>x[i]</code> </td><td style="text-align: left;"> x subscript i </td> </tr> <tr> <td style="text-align: left;"> <code>x^2</code> </td><td style="text-align: left;"> x superscript 2 </td> </tr> <tr> <td style="text-align: left;"> <code>paste(x, y, z)</code> </td><td style="text-align: left;"> juxtapose x, y, and z </td> </tr> <tr> <td style="text-align: left;"> <code>sqrt(x)</code> </td><td style="text-align: left;"> square root of x </td> </tr> <tr> <td style="text-align: left;"> <code>sqrt(x, y)</code> </td><td style="text-align: left;"> yth root of x </td> </tr> <tr> <td style="text-align: left;"> <code>x == y</code> </td><td style="text-align: left;"> x equals y </td> </tr> <tr> <td style="text-align: left;"> <code>x != y</code> </td><td style="text-align: left;"> x is not equal to y </td> </tr> <tr> <td style="text-align: left;"> <code>x < y</code> </td><td style="text-align: left;"> x is less than y </td> </tr> <tr> <td style="text-align: left;"> <code>x <= y</code> </td><td style="text-align: left;"> x is less than or equal to y </td> </tr> <tr> <td style="text-align: left;"> <code>x > y</code> </td><td style="text-align: left;"> x is greater than y </td> </tr> <tr> <td style="text-align: left;"> <code>x >= y</code> </td><td style="text-align: left;"> x is greater than or equal to y </td> </tr> <tr> <td style="text-align: left;"> <code>!x</code> </td><td style="text-align: left;"> not x </td> </tr> <tr> <td style="text-align: left;"> <code>x %~~% y</code> </td><td style="text-align: left;"> x is approximately equal to y </td> </tr> <tr> <td style="text-align: left;"> <code>x %=~% y</code> </td><td style="text-align: left;"> x and y are congruent </td> </tr> <tr> <td style="text-align: left;"> <code>x %==% y</code> </td><td style="text-align: left;"> x is defined as y </td> </tr> <tr> <td style="text-align: left;"> <code>x %prop% y</code> </td><td style="text-align: left;"> x is proportional to y </td> </tr> <tr> <td style="text-align: left;"> <code>x %~% y</code> </td><td style="text-align: left;"> x is distributed as y </td> </tr> <tr> <td style="text-align: left;"> <code>plain(x)</code> </td><td style="text-align: left;"> draw x in normal font </td> </tr> <tr> <td style="text-align: left;"> <code>bold(x)</code> </td><td style="text-align: left;"> draw x in bold font </td> </tr> <tr> <td style="text-align: left;"> <code>italic(x)</code> </td><td style="text-align: left;"> draw x in italic font </td> </tr> <tr> <td style="text-align: left;"> <code>bolditalic(x)</code> </td><td style="text-align: left;"> draw x in bolditalic font </td> </tr> <tr> <td style="text-align: left;"> <code>symbol(x)</code> </td><td style="text-align: left;"> draw x in symbol font </td> </tr> <tr> <td style="text-align: left;"> <code>list(x, y, z)</code> </td><td style="text-align: left;"> comma-separated list </td> </tr> <tr> <td style="text-align: left;"> <code>...</code> </td><td style="text-align: left;"> ellipsis (height varies) </td> </tr> <tr> <td style="text-align: left;"> <code>cdots</code> </td><td style="text-align: left;"> ellipsis (vertically centred) </td> </tr> <tr> <td style="text-align: left;"> <code>ldots</code> </td><td style="text-align: left;"> ellipsis (at baseline) </td> </tr> <tr> <td style="text-align: left;"> <code>x %subset% y</code> </td><td style="text-align: left;"> x is a proper subset of y </td> </tr> <tr> <td style="text-align: left;"> <code>x %subseteq% y</code> </td><td style="text-align: left;"> x is a subset of y </td> </tr> <tr> <td style="text-align: left;"> <code>x %notsubset% y</code> </td><td style="text-align: left;"> x is not a subset of y </td> </tr> <tr> <td style="text-align: left;"> <code>x %supset% y</code> </td><td style="text-align: left;"> x is a proper superset of y </td> </tr> <tr> <td style="text-align: left;"> <code>x %supseteq% y</code> </td><td style="text-align: left;"> x is a superset of y </td> </tr> <tr> <td style="text-align: left;"> <code>x %in% y</code> </td><td style="text-align: left;"> x is an element of y </td> </tr> <tr> <td style="text-align: left;"> <code>x %notin% y</code> </td><td style="text-align: left;"> x is not an element of y </td> </tr> <tr> <td style="text-align: left;"> <code>hat(x)</code> </td><td style="text-align: left;"> x with a circumflex </td> </tr> <tr> <td style="text-align: left;"> <code>tilde(x)</code> </td><td style="text-align: left;"> x with a tilde </td> </tr> <tr> <td style="text-align: left;"> <code>dot(x)</code> </td><td style="text-align: left;"> x with a dot </td> </tr> <tr> <td style="text-align: left;"> <code>ring(x)</code> </td><td style="text-align: left;"> x with a ring </td> </tr> <tr> <td style="text-align: left;"> <code>bar(xy)</code> </td><td style="text-align: left;"> xy with bar </td> </tr> <tr> <td style="text-align: left;"> <code>widehat(xy)</code> </td><td style="text-align: left;"> xy with a wide circumflex </td> </tr> <tr> <td style="text-align: left;"> <code>widetilde(xy)</code> </td><td style="text-align: left;"> xy with a wide tilde </td> </tr> <tr> <td style="text-align: left;"> <code>x %<->% y</code> </td><td style="text-align: left;"> x double-arrow y </td> </tr> <tr> <td style="text-align: left;"> <code>x %->% y</code> </td><td style="text-align: left;"> x right-arrow y </td> </tr> <tr> <td style="text-align: left;"> <code>x %<-% y</code> </td><td style="text-align: left;"> x left-arrow y </td> </tr> <tr> <td style="text-align: left;"> <code>x %up% y</code> </td><td style="text-align: left;"> x up-arrow y </td> </tr> <tr> <td style="text-align: left;"> <code>x %down% y</code> </td><td style="text-align: left;"> x down-arrow y </td> </tr> <tr> <td style="text-align: left;"> <code>x %<=>% y</code> </td><td style="text-align: left;"> x is equivalent to y </td> </tr> <tr> <td style="text-align: left;"> <code>x %=>% y</code> </td><td style="text-align: left;"> x implies y </td> </tr> <tr> <td style="text-align: left;"> <code>x %<=% y</code> </td><td style="text-align: left;"> y implies x </td> </tr> <tr> <td style="text-align: left;"> <code>x %dblup% y</code> </td><td style="text-align: left;"> x double-up-arrow y </td> </tr> <tr> <td style="text-align: left;"> <code>x %dbldown% y</code> </td><td style="text-align: left;"> x double-down-arrow y </td> </tr> <tr> <td style="text-align: left;"> <code>alpha</code> -- <code>omega</code> </td><td style="text-align: left;"> Greek symbols </td> </tr> <tr> <td style="text-align: left;"> <code>Alpha</code> -- <code>Omega</code> </td><td style="text-align: left;"> uppercase Greek symbols </td> </tr> <tr> <td style="text-align: left;"> <code>theta1, phi1, sigma1, omega1</code> </td><td style="text-align: left;"> cursive Greek symbols</td> </tr> <tr> <td style="text-align: left;"> <code>Upsilon1</code> </td><td style="text-align: left;"> capital upsilon with hook</td> </tr> <tr> <td style="text-align: left;"> <code>aleph</code> </td><td style="text-align: left;"> first letter of Hebrew alphabet</td> </tr> <tr> <td style="text-align: left;"> <code>infinity</code> </td><td style="text-align: left;"> infinity symbol </td> </tr> <tr> <td style="text-align: left;"> <code>partialdiff</code> </td><td style="text-align: left;"> partial differential symbol </td> </tr> <tr> <td style="text-align: left;"> <code>nabla</code> </td><td style="text-align: left;"> nabla, gradient symbol</td> </tr> <tr> <td style="text-align: left;"> <code>32*degree</code> </td><td style="text-align: left;"> 32 degrees </td> </tr> <tr> <td style="text-align: left;"> <code>60*minute</code> </td><td style="text-align: left;"> 60 minutes of angle </td> </tr> <tr> <td style="text-align: left;"> <code>30*second</code> </td><td style="text-align: left;"> 30 seconds of angle </td> </tr> <tr> <td style="text-align: left;"> <code>displaystyle(x)</code> </td><td style="text-align: left;"> draw x in normal size (extra spacing) </td> </tr> <tr> <td style="text-align: left;"> <code>textstyle(x)</code> </td><td style="text-align: left;"> draw x in normal size </td> </tr> <tr> <td style="text-align: left;"> <code>scriptstyle(x)</code> </td><td style="text-align: left;"> draw x in small size </td> </tr> <tr> <td style="text-align: left;"> <code>scriptscriptstyle(x)</code> </td><td style="text-align: left;"> draw x in very small size </td> </tr> <tr> <td style="text-align: left;"> <code>underline(x)</code> </td><td style="text-align: left;"> draw x underlined</td> </tr> <tr> <td style="text-align: left;"> <code>x ~~ y</code> </td><td style="text-align: left;"> put extra space between x and y </td> </tr> <tr> <td style="text-align: left;"> <code>x + phantom(0) + y</code> </td><td style="text-align: left;"> leave gap for "0", but don't draw it </td> </tr> <tr> <td style="text-align: left;"> <code>x + over(1, phantom(0))</code> </td><td style="text-align: left;"> leave vertical gap for "0" (don't draw) </td> </tr> <tr> <td style="text-align: left;"> <code>frac(x, y)</code> </td><td style="text-align: left;"> x over y </td> </tr> <tr> <td style="text-align: left;"> <code>over(x, y)</code> </td><td style="text-align: left;"> x over y </td> </tr> <tr> <td style="text-align: left;"> <code>atop(x, y)</code> </td><td style="text-align: left;"> x over y (no horizontal bar) </td> </tr> <tr> <td style="text-align: left;"> <code>sum(x[i], i==1, n)</code> </td><td style="text-align: left;"> sum x[i] for i equals 1 to n </td> </tr> <tr> <td style="text-align: left;"> <code>prod(plain(P)(X==x), x)</code> </td><td style="text-align: left;"> product of P(X=x) for all values of x </td> </tr> <tr> <td style="text-align: left;"> <code>integral(f(x)*dx, a, b)</code> </td><td style="text-align: left;"> definite integral of f(x) wrt x </td> </tr> <tr> <td style="text-align: left;"> <code>union(A[i], i==1, n)</code> </td><td style="text-align: left;"> union of A[i] for i equals 1 to n </td> </tr> <tr> <td style="text-align: left;"> <code>intersect(A[i], i==1, n)</code> </td><td style="text-align: left;"> intersection of A[i] </td> </tr> <tr> <td style="text-align: left;"> <code>lim(f(x), x %->% 0)</code> </td><td style="text-align: left;"> limit of f(x) as x tends to 0 </td> </tr> <tr> <td style="text-align: left;"> <code>min(g(x), x > 0)</code> </td><td style="text-align: left;"> minimum of g(x) for x greater than 0 </td> </tr> <tr> <td style="text-align: left;"> <code>inf(S)</code> </td><td style="text-align: left;"> infimum of S </td> </tr> <tr> <td style="text-align: left;"> <code>sup(S)</code> </td><td style="text-align: left;"> supremum of S </td> </tr> <tr> <td style="text-align: left;"> <code>x^y + z</code> </td><td style="text-align: left;"> normal operator precedence </td> </tr> <tr> <td style="text-align: left;"> <code>x^(y + z)</code> </td><td style="text-align: left;"> visible grouping of operands </td> </tr> <tr> <td style="text-align: left;"> <code>x^{y + z}</code> </td><td style="text-align: left;"> invisible grouping of operands </td> </tr> <tr> <td style="text-align: left;"> <code>group("(",list(a, b),"]")</code> </td><td style="text-align: left;"> specify left and right delimiters </td> </tr> <tr> <td style="text-align: left;"> <code>bgroup("(",atop(x,y),")")</code> </td><td style="text-align: left;"> use scalable delimiters </td> </tr> <tr> <td style="text-align: left;"> <code>group(lceil, x, rceil)</code> </td><td style="text-align: left;"> special delimiters </td> </tr> <tr> <td style="text-align: left;"> <code>group(lfloor, x, rfloor)</code> </td><td style="text-align: left;"> special delimiters </td> </tr> <tr> <td style="text-align: left;"> </td> </tr> </table> <p>The supported ‘scalable delimiters’ are <code>| ( [ {</code>, <code>lceil</code>, <code>lfloor</code> and their right-hand versions. <code>"."</code> is equivalent to <code>""</code>: the corresponding delimiter will be omitted. Delimiter <code>||</code> is supported but has the same effect as <code>|</code>. </p> <p>The symbol font uses Adobe Symbol encoding so, for example, a lower case mu can be obtained either by the special symbol <code>mu</code> or by <code>symbol("m")</code>. This provides access to symbols that have no special symbol name, for example, the universal, or forall, symbol is <code>symbol("\042")</code>. To see what symbols are available in this way use <code>TestChars(font=5)</code> as given in the examples for <code><a href="../../graphics/html/points.html">points</a></code>: some are only available on some devices. </p> <p>Note to TeX users: TeX's <span class="samp">\Upsilon</span> is <code>Upsilon1</code>, TeX's <span class="samp">\varepsilon</span> is close to <code>epsilon</code>, and there is no equivalent of TeX's <span class="samp">\epsilon</span>. TeX's <span class="samp">\varpi</span> is close to <code>omega1</code>. <code>vartheta</code>, <code>varphi</code> and <code>varsigma</code> are allowed as synonyms for <code>theta1</code>, <code>phi1</code> and <code>sigma1</code>. </p> <p><code>sigma1</code> is also known as <code>stigma</code>, its Unicode name. </p> <p>Control characters (e.g., <span class="samp">\n</span>) are not interpreted in character strings in plotmath, unlike normal plotting. </p> <p>The fonts used are taken from the current font family, and so can be set by <code><a href="../../graphics/html/par.html">par</a>(family=)</code> in base graphics, and <code><a href="../../grid/html/gpar.html">gpar</a>(fontfamily=)</code> in package <span class="pkg">grid</span>. </p> <p>Note that <code>bold</code>, <code>italic</code> and <code>bolditalic</code> do not apply to symbols, and hence not to the Greek <em>symbols</em> such as <code>mu</code> which are displayed in the symbol font. They also do not apply to numeric constants. </p> <h3>Other symbols</h3> <p>On many OSes and some graphics devices many other symbols are available as part of the standard text font, and all of the symbols in the Adobe Symbol encoding are in principle available <em>via</em> changing the font face or (see ‘Details’) plotmath: see the examples section of <code><a href="../../graphics/html/points.html">points</a></code> for a function to display them. (‘In principle’ because some of the glyphs are missing from some implementations of the symbol font.) Unfortunately, <code><a href="postscript.html">postscript</a></code> and <code><a href="pdf.html">pdf</a></code> have support for little more than European (not Greek) and CJK characters and the Adobe Symbol encoding (and in a few fonts, also Cyrillic characters). </p> <dl> <dt>On Unix-alikes:</dt><dd> <p>In a UTF-8 locale any Unicode character can be entered, perhaps as a <span class="samp">\uxxxx</span> or <span class="samp">\Uxxxxxxxx</span> escape sequence, but the issue is whether the graphics device is able to display the character. The widest range of characters is likely to be available in the <code><a href="x11.html">X11</a></code> device using cairo: see its help page for how installing additional fonts can help. This can often be used to display Greek <em>letters</em> in bold or italic. </p> <p>In non-UTF-8 locales there is normally no support for symbols not in the languages for which the current encoding was intended. </p> </dd> <dt>On Windows:</dt><dd> <p>Any Unicode character can be entered into a text string <em>via</em> a <span class="samp">\uxxxx</span> escape, or used by number in a call to <code><a href="../../graphics/html/points.html">points</a></code>. The <code><a href="windows.html">windows</a></code> family of devices can display such characters if they are available in the font in use. This can often be used to display Greek <em>letters</em> in bold or italic. </p> <p>A good way to both find out which characters are available in a font and to determine the Unicode number is to use the ‘Character Map’ accessory (usually on the ‘Start’ menu under ‘Accessories->System Tools’). You can also copy-and-paste characters from the ‘Character Map’ window to the <code>Rgui</code> console (but not to <code>Rterm</code>). </p> </dd> </dl> <h3>References</h3> <p>Murrell, P. and Ihaka, R. (2000). An approach to providing mathematical annotation in plots. <em>Journal of Computational and Graphical Statistics</em>, <b>9</b>, 582–599. doi: <a href="https://doi.org/10.2307/1390947">10.2307/1390947</a>. </p> <p>The symbol codes can be found in octal in the Adobe reference manuals, e.g. for Postscript <a href="https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf">https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf</a> or PDF <a href="https://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf">https://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf</a> and in decimal, octal and hex at <a href="http://www.stat.auckland.ac.nz/~paul/R/CM/AdobeSym.html">http://www.stat.auckland.ac.nz/~paul/R/CM/AdobeSym.html</a>. </p> <h3>See Also</h3> <p><code>demo(plotmath)</code>, <code><a href="../../graphics/html/axis.html">axis</a></code>, <code><a href="../../graphics/html/mtext.html">mtext</a></code>, <code><a href="../../graphics/html/text.html">text</a></code>, <code><a href="../../graphics/html/title.html">title</a></code>, <code><a href="../../base/html/substitute.html">substitute</a></code> <code><a href="../../base/html/substitute.html">quote</a></code>, <code><a href="../../base/html/bquote.html">bquote</a></code> </p> <h3>Examples</h3> <pre> require(graphics) x <- seq(-4, 4, len = 101) y <- cbind(sin(x), cos(x)) matplot(x, y, type = "l", xaxt = "n", main = expression(paste(plain(sin) * phi, " and ", plain(cos) * phi)), ylab = expression("sin" * phi, "cos" * phi), # only 1st is taken xlab = expression(paste("Phase Angle ", phi)), col.main = "blue") axis(1, at = c(-pi, -pi/2, 0, pi/2, pi), labels = expression(-pi, -pi/2, 0, pi/2, pi)) ## How to combine "math" and numeric variables : plot(1:10, type="n", xlab="", ylab="", main = "plot math & numbers") theta <- 1.23 ; mtext(bquote(hat(theta) == .(theta)), line= .25) for(i in 2:9) text(i, i+1, substitute(list(xi, eta) == group("(",list(x,y),")"), list(x = i, y = i+1))) ## note that both of these use calls rather than expressions. ## text(1, 10, "Derivatives:", adj = 0) text(1, 9.6, expression( " first: {f * minute}(x) " == {f * minute}(x)), adj = 0) text(1, 9.0, expression( " second: {f * second}(x) " == {f * second}(x)), adj = 0) plot(1:10, 1:10) 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 = .8) text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n))) text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))", cex = .8) text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ", plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})), cex = 1.2) ## some other useful symbols plot.new(); plot.window(c(0,4), c(15,1)) text(1, 1, "universal", adj = 0); text(2.5, 1, "\\042") text(3, 1, expression(symbol("\042"))) text(1, 2, "existential", adj = 0); text(2.5, 2, "\\044") text(3, 2, expression(symbol("\044"))) text(1, 3, "suchthat", adj = 0); text(2.5, 3, "\\047") text(3, 3, expression(symbol("\047"))) text(1, 4, "therefore", adj = 0); text(2.5, 4, "\\134") text(3, 4, expression(symbol("\134"))) text(1, 5, "perpendicular", adj = 0); text(2.5, 5, "\\136") text(3, 5, expression(symbol("\136"))) text(1, 6, "circlemultiply", adj = 0); text(2.5, 6, "\\304") text(3, 6, expression(symbol("\304"))) text(1, 7, "circleplus", adj = 0); text(2.5, 7, "\\305") text(3, 7, expression(symbol("\305"))) text(1, 8, "emptyset", adj = 0); text(2.5, 8, "\\306") text(3, 8, expression(symbol("\306"))) text(1, 9, "angle", adj = 0); text(2.5, 9, "\\320") text(3, 9, expression(symbol("\320"))) text(1, 10, "leftangle", adj = 0); text(2.5, 10, "\\341") text(3, 10, expression(symbol("\341"))) text(1, 11, "rightangle", adj = 0); text(2.5, 11, "\\361") text(3, 11, expression(symbol("\361"))) </pre> <hr /><div style="text-align: center;">[Package <em>grDevices</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>