Mathematical Annotation in R

Description

If the text argument to one of the text-drawing functions
(text, mtext, axis,
legend) in R 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 persp plots).

In most cases other language objects (names and calls, including
formulas) are coerced to expressions and so can also be used.

Details

A mathematical expression must obey the normal rules of syntax for any
R expression, but it is interpreted according to very different rules
than for normal R expressions.

It is possible to produce many different mathematical symbols, generate
sub- or superscripts, produce fractions, etc.

The output from demo(plotmath) includes several tables which
show the available features. In these tables, the columns of grey text
show sample R expressions, and the columns of black text show the
resulting output.

The available features are also described in the tables below:

Syntax

Meaning

x + y

x plus y

x - y

x minus y

x*y

juxtapose x and y

x/y

x forwardslash y

x %+-% y

x plus or minus y

x %/% y

x divided by y

x %*% y

x times y

x %.% y

x cdot y

x[i]

x subscript i

x^2

x superscript 2

paste(x, y, z)

juxtapose x, y, and z

sqrt(x)

square root of x

sqrt(x, y)

yth root of x

x == y

x equals y

x != y

x is not equal to y

x < y

x is less than y

x <= y

x is less than or equal to y

x > y

x is greater than y

x >= y

x is greater than or equal to y

x %~~% y

x is approximately equal to y

x %=~% y

x and y are congruent

x %==% y

x is defined as y

x %prop% y

x is proportional to y

x %~% y

x is distributed as y

plain(x)

draw x in normal font

bold(x)

draw x in bold font

italic(x)

draw x in italic font

bolditalic(x)

draw x in bolditalic font

symbol(x)

draw x in symbol font

list(x, y, z)

comma-separated list

...

ellipsis (height varies)

cdots

ellipsis (vertically centred)

ldots

ellipsis (at baseline)

x %subset% y

x is a proper subset of y

x %subseteq% y

x is a subset of y

x %notsubset% y

x is not a subset of y

x %supset% y

x is a proper superset of y

x %supseteq% y

x is a superset of y

x %in% y

x is an element of y

x %notin% y

x is not an element of y

hat(x)

x with a circumflex

tilde(x)

x with a tilde

dot(x)

x with a dot

ring(x)

x with a ring

bar(xy)

xy with bar

widehat(xy)

xy with a wide circumflex

widetilde(xy)

xy with a wide tilde

x %<->% y

x double-arrow y

x %->% y

x right-arrow y

x %<-% y

x left-arrow y

x %up% y

x up-arrow y

x %down% y

x down-arrow y

x %<=>% y

x is equivalent to y

x %=>% y

x implies y

x %<=% y

y implies x

x %dblup% y

x double-up-arrow y

x %dbldown% y

x double-down-arrow y

alpha -- omega

Greek symbols

Alpha -- Omega

uppercase Greek symbols

theta1, phi1, sigma1, omega1

cursive Greek symbols

Upsilon1

capital upsilon with hook

aleph

first letter of Hebrew alphabet

infinity

infinity symbol

partialdiff

partial differential symbol

nabla

nabla, gradient symbol

32*degree

32 degrees

60*minute

60 minutes of angle

30*second

30 seconds of angle

displaystyle(x)

draw x in normal size (extra spacing)

textstyle(x)

draw x in normal size

scriptstyle(x)

draw x in small size

scriptscriptstyle(x)

draw x in very small size

underline(x)

draw x underlined

x ~~ y

put extra space between x and y

x + phantom(0) + y

leave gap for "0", but don't draw it

x + over(1, phantom(0))

leave vertical gap for "0" (don't draw)

frac(x, y)

x over y

over(x, y)

x over y

atop(x, y)

x over y (no horizontal bar)

sum(x[i], i==1, n)

sum x[i] for i equals 1 to n

prod(plain(P)(X==x), x)

product of P(X=x) for all values of x

integral(f(x)*dx, a, b)

definite integral of f(x) wrt x

union(A[i], i==1, n)

union of A[i] for i equals 1 to n

intersect(A[i], i==1, n)

intersection of A[i]

lim(f(x), x %->% 0)

limit of f(x) as x tends to 0

min(g(x), x > 0)

minimum of g(x) for x greater than 0

inf(S)

infimum of S

sup(S)

supremum of S

x^y + z

normal operator precedence

x^(y + z)

visible grouping of operands

x^{y + z}

invisible grouping of operands

group("(",list(a, b),"]")

specify left and right delimiters

bgroup("(",atop(x,y),")")

use scalable delimiters

group(lceil, x, rceil)

special delimiters

group(lfloor, x, rfloor)

special delimiters

The supported ‘scalable delimiters’ are | ( [ {,
lceil, lfloor and their right-hand versions.
"." is equivalent to "": the corresponding delimiter
will be omitted. Delimiter || is supported but has the same
effect as |.

The symbol font uses Adobe Symbol encoding so, for example, a lower
case mu can be obtained either by the special symbol mu or by
symbol("m"). This provides access to symbols that have no
special symbol name, for example, the universal, or forall, symbol is
symbol("\042"). To see what symbols are available in this way
use TestChars(font=5) as given in the examples for
points: some are only available on some devices.

Note to TeX users: TeX's \Upsilon is Upsilon1, TeX's
\varepsilon is close to epsilon, and there is no
equivalent of TeX's \epsilon. TeX's \varpi is close to
omega1. vartheta, varphi and varsigma are
allowed as synonyms for theta1, phi1 and sigma1.

sigma1 is also known as stigma, its Unicode name.

Control characters (e.g., \n) are not interpreted in character
strings in plotmath, unlike normal plotting.

The fonts used are taken from the current font family, and so can be
set by par(family=) in base graphics, and
gpar(fontfamily=) in package grid.

Note that bold, italic and bolditalic do not
apply to symbols, and hence not to the Greek symbols such as
mu which are displayed in the symbol font. They also do not
apply to numeric constants.

Other symbols

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 via
changing the font face or (see ‘Details’) plotmath: see the
examples section of points for a function to display
them. (‘In principle’ because some of the glyphs are missing
from some implementations of the symbol font.) Unfortunately,
postscript and pdf 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).

In a UTF-8 locale any Unicode character can be entered, perhaps as a
\uxxxx or \Uxxxxxxxx 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
X11 device using cairo: see its help page for how
installing additional fonts can help. This can often be used to
display Greek letters in bold or italic.

In non-UTF-8 locales there is normally no support for symbols not in
the languages for which the current encoding was intended.