plotmath {base}R Documentation

Mathematical Annotation in R

Description

If the text argument to one of the text-drawing functions (text, mtext, axis) in R is an expression, the argument is interpreted as a mathematical expression and the output will be formatted according to TeX-like rules.

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.

Binary operators: addition, subtraction, multiplication, and division use the standard R syntax, although multiplication only juxtaposes the arguments. For example, a+b, a-b, and a/b, produce a+b, a-b, and a/b, but a*b produces ab.

Unary operators: positive and negative numbers are specified with standard syntax. For example, +x produces +x and -y produces -y.

Subscripts and superscripts: a subscript is specified using the subsetting syntax and a superscript is specified using the power syntax. For example, x[i] produces x_i and x^2 produces x^2.

Accents: accented expressions are specified using the special mathematical functions hat and bar.

Fractions: fractions are specified using the special mathematical function frac (or its alias, over).

Relations: equality or assignment of terms is specified using the == relation. For example, x == y produces x=y.

Visible grouping: terms are visibly grouped by placing them within parentheses. For example, (x+y) produces (x+y).

Invisible grouping: terms are invisibly grouped by placing them within curly braces. For example, x^{2*y} produces x^{2y}, whereas x^2*y produces x^2y.

Big operators: a sum, product, or integral is specified using the special mathematical function of the corresponding name. Each of these functions takes three arguments; the first indicates what is being summed/multiplied/integrated and the second and third specify the limits of the summation/product/integral. For example, sum(x[i], i==0, n) produces

sum_{i=0}^n x_i

.

Radicals: a square root expression is specified using the special mathematical functions root and sqrt.

Absolute values: an absolute term is specified using the special mathematical function abs. For example, abs(x) produces |x|.

Juxtaposition: multiple terms are juxtaposed using the special mathematical function paste. For example, paste(over(b, 2), y, sum(x)) produces b/2 y sum(x).

Typeface changes: the default font in mathematical expressions is italic (except for terms which are symbols). A new typeface is specified using the special mathematical functions bold, italic, plain, and bolditalic. Note that these font specifications do not accumulate (i.e., bold(italic(x))) gives an italic `x', whereas bolditalic(x) produces a bold, italic `x').

General expressions: any functional expression which is not a special mathematical function is simply reproduced as a function expression. For example, foo(x) produces foo(x).

See Also

axis, mtext, text, title

Examples

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),
     lab = expression(-pi, -pi/2, 0, pi/2, pi))

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)


[Package Contents]