The R Project SVN R

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
27442 ripley 1
\name{plotmath}
2
\alias{plotmath}
3
\title{Mathematical Annotation in R}
4
\description{
5
  If the \code{text} argument to one of the text-drawing functions
6
  (\code{\link{text}}, \code{\link{mtext}}, \code{\link{axis}}) in \R
7
  is an expression, the argument is interpreted as a mathematical
8
  expression and the output will be formatted according to TeX-like
9
  rules.  Expressions can also be used for titles, subtitles and
10
  x- and y-axis labels (but not for axis labels on \code{persp} plots).
11
}
12
\details{
13
  A mathematical expression must obey the normal rules of syntax for any
14
  \R expression, but it is interpreted according to very different rules
15
  than for normal \R expressions.
16
 
17
  It is possible to produce many different mathematical symbols, generate
18
  sub- or superscripts, produce fractions, etc.
19
 
20
  The output from \code{demo(plotmath)} includes several tables which
21
  show the available features.  In these tables, the columns of grey text
22
  show sample \R expressions, and the columns of black text show the
23
  resulting output.
24
 
25
  The available features are also described in the tables below:
26
 
27
  \tabular{ll}{
28
    \bold{Syntax} \tab \bold{Meaning} \cr
29
    \code{x + y}   \tab x plus y   \cr
30
    \code{x - y}   \tab x minus y \cr
31
    \code{x*y}    \tab juxtapose x and y \cr
32
    \code{x/y}    \tab x forwardslash y \cr
33
    \code{x \%+-\% y}   \tab x plus or minus y \cr
34
    \code{x \%/\% y}   \tab x divided by y \cr
35
    \code{x \%*\% y}   \tab x times y \cr
36
    \code{x[i]}   \tab x subscript i \cr
37
    \code{x^2}    \tab x superscript 2 \cr
38
    \code{paste(x, y, z)} \tab juxtapose x, y, and z \cr
39
    \code{sqrt(x)}   \tab square root of x \cr
40
    \code{sqrt(x, y)}   \tab yth root of x \cr
41
    \code{x == y}   \tab x equals y \cr
42
    \code{x != y}   \tab x is not equal to y \cr
43
    \code{x < y}   \tab x is less than y \cr
44
    \code{x <= y}   \tab x is less than or equal to y \cr
45
    \code{x > y}   \tab x is greater than y \cr
46
    \code{x >= y}   \tab x is greater than or equal to y \cr
47
    \code{x \%~~\% y}   \tab x is approximately equal to y \cr
48
    \code{x \%=~\% y}   \tab x and y are congruent \cr
49
    \code{x \%==\% y}   \tab x is defined as y \cr
50
    \code{x \%prop\% y}  \tab x is proportional to y \cr
51
    \code{plain(x)}   \tab draw x in normal font \cr
52
    \code{bold(x)}   \tab draw x in bold font \cr
53
    \code{italic(x)}   \tab draw x in italic font \cr
54
    \code{bolditalic(x)} \tab draw x in bolditalic font \cr
55
    \code{list(x, y, z)} \tab comma-separated list \cr
56
    \code{...}    \tab ellipsis (height varies) \cr
57
    \code{cdots}   \tab ellipsis (vertically centred) \cr
58
    \code{ldots}   \tab ellipsis (at baseline) \cr
59
    \code{x \%subset\% y} \tab x is a proper subset of y \cr
60
    \code{x \%subseteq\% y} \tab x is a subset of y \cr
61
    \code{x \%notsubset\% y} \tab x is not a subset of y \cr
62
    \code{x \%supset\% y} \tab x is a proper superset of y \cr
63
    \code{x \%supseteq\% y} \tab x is a superset of y \cr
64
    \code{x \%in\% y}   \tab x is an element of y \cr
65
    \code{x \%notin\% y} \tab x is not an element of y \cr
66
    \code{hat(x)}   \tab x with a circumflex \cr
67
    \code{tilde(x)}   \tab x with a tilde \cr
68
    \code{dot(x)} \tab x with a dot \cr
69
    \code{ring(x)}   \tab x with a ring \cr
70
    \code{bar(xy)}   \tab xy with bar \cr
71
    \code{widehat(xy)}   \tab xy with a wide circumflex \cr
72
    \code{widetilde(xy)} \tab xy with a wide tilde \cr
73
    \code{x \%<->\% y}   \tab x double-arrow y \cr
74
    \code{x \%->\% y}   \tab x right-arrow y \cr
75
    \code{x \%<-\% y}   \tab x left-arrow y \cr
76
    \code{x \%up\% y}   \tab x up-arrow y \cr
77
    \code{x \%down\% y}  \tab x down-arrow y \cr
78
    \code{x \%<=>\% y}   \tab x is equivalent to y \cr
79
    \code{x \%=>\% y}   \tab x implies y \cr
80
    \code{x \%<=\% y}   \tab y implies x \cr
81
    \code{x \%dblup\% y}   \tab x double-up-arrow y \cr
82
    \code{x \%dbldown\% y} \tab x double-down-arrow y \cr
83
    \code{alpha} -- \code{omega} \tab Greek symbols \cr
84
    \code{Alpha} -- \code{Omega} \tab uppercase Greek symbols \cr
85
    \code{infinity}   \tab infinity symbol \cr
86
    \code{partialdiff} \tab partial differential symbol \cr
87
    \code{32*degree}   \tab 32 degrees \cr
88
    \code{60*minute}   \tab 60 minutes of angle \cr
89
    \code{30*second}   \tab 30 seconds of angle \cr
90
    \code{displaystyle(x)} \tab draw x in normal size (extra spacing) \cr
91
    \code{textstyle(x)}  \tab draw x in normal size \cr
92
    \code{scriptstyle(x)} \tab draw x in small size \cr
93
    \code{scriptscriptstyle(x)} \tab draw x in very small size \cr
94
    \code{x ~~ y}        \tab put extra space between x and y \cr
95
    \code{x + phantom(0) + y} \tab leave gap for "0", but don't draw it \cr
96
    \code{x + over(1, phantom(0))} \tab leave vertical gap for "0" (don't draw) \cr
97
    \code{frac(x, y)}   \tab x over y \cr
98
    \code{over(x, y)}   \tab x over y \cr
99
    \code{atop(x, y)}   \tab x over y (no horizontal bar) \cr
100
    \code{sum(x[i], i==1, n)} \tab sum x[i] for i equals 1 to n \cr
101
    \code{prod(plain(P)(X==x), x)} \tab product of P(X=x) for all values of x \cr
102
    \code{integral(f(x)*dx, a, b)} \tab definite integral of f(x) wrt x \cr
103
    \code{union(A[i], i==1, n)} \tab union of A[i] for i equals 1 to n \cr
104
    \code{intersect(A[i], i==1, n)} \tab intersection of A[i] \cr
105
    \code{lim(f(x), x \%->\% 0)} \tab limit of f(x) as x tends to 0 \cr
106
    \code{min(g(x), x > 0)} \tab minimum of g(x) for x greater than 0 \cr
107
    \code{inf(S)}        \tab infimum of S \cr
108
    \code{sup(S)}   \tab supremum of S \cr
109
    \code{x^y + z}   \tab normal operator precedence \cr
110
    \code{x^(y + z)}    \tab visible grouping of operands \cr
111
    \code{x^{y + z}}  \tab invisible grouping of operands \cr
112
    \code{group("(",list(a, b),"]")} \tab specify left and right delimiters \cr
113
    \code{bgroup("(",atop(x,y),")")} \tab use scalable delimiters \cr
114
    \code{group(lceil, x, rceil)} \tab special delimiters \cr
115
  }
116
}
117
\references{
118
  Murrell, P. and Ihaka, R. (2000) An approach to providing
119
  mathematical annotation in plots.
120
  \emph{Journal of Computational and Graphical Statistics},
121
  \bold{9}, 582--599.
122
}
123
\seealso{
124
  \code{demo(plotmath)},
125
  \code{\link{axis}},
126
  \code{\link{mtext}},
127
  \code{\link{text}},
128
  \code{\link{title}},
129
  \code{\link{substitute}}
130
  \code{\link{quote}}, \code{\link{bquote}}
131
}
132
\examples{
133
x <- seq(-4, 4, len = 101)
134
y <- cbind(sin(x), cos(x))
135
matplot(x, y, type = "l", xaxt = "n",
136
        main = expression(paste(plain(sin) * phi, "  and  ",
137
                                plain(cos) * phi)),
138
        ylab = expression("sin" * phi, "cos" * phi), # only 1st is taken
139
        xlab = expression(paste("Phase Angle ", phi)),
140
        col.main = "blue")
141
axis(1, at = c(-pi, -pi/2, 0, pi/2, pi),
142
     lab = expression(-pi, -pi/2, 0, pi/2, pi))
143
 
144
 
145
## How to combine "math" and numeric variables :
146
plot(1:10, type="n", xlab="", ylab="", main = "plot math & numbers")
147
theta <- 1.23 ; mtext(bquote(hat(theta) == .(theta)))
148
for(i in 2:9)
149
    text(i,i+1, substitute(list(xi,eta) == group("(",list(x,y),")"),
150
                           list(x=i, y=i+1)))
151
 
152
plot(1:10, 1:10)
153
text(4, 9, expression(hat(beta) == (X^t * X)^{-1} * X^t * y))
154
text(4, 8.4, "expression(hat(beta) == (X^t * X)^{-1} * X^t * y)",
155
     cex = .8)
156
text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n)))
157
text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))",
158
     cex = .8)
159
text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ",
160
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})),
161
     cex = 1.2)
162
}
163
\keyword{aplot}