The R Project SVN R

Rev

Rev 7745 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 7745 Rev 8061
Line 10... Line 10...
10
}
10
}
11
\details{
11
\details{
12
  A mathematical expression must obey the normal rules of syntax for any
12
  A mathematical expression must obey the normal rules of syntax for any
13
  \R expression, but it is interpreted according to very different rules
13
  \R expression, but it is interpreted according to very different rules
14
  than for normal \R expressions.
14
  than for normal \R expressions.
15
  
-
 
16
  \describe{
-
 
17
    \item{\emph{Binary operators:}}{
-
 
18
      addition, subtraction, multiplication, and division use the
-
 
19
      standard \R syntax, although multiplication only juxtaposes the
-
 
20
      arguments.
-
 
21
      
-
 
22
      For example, \code{a+b}, \code{a-b}, and \code{a/b}, produce
-
 
23
      \eqn{a+b}, \eqn{a-b}, and \eqn{a/b}, but \code{a*b} produces
-
 
24
      \eqn{ab}.}
-
 
25
    
-
 
26
    \item{\emph{Unary operators:}}{
-
 
27
      positive and negative numbers are specified with standard syntax.
-
 
28
      
-
 
29
      For example, \code{+x} produces \eqn{+x} and \code{-y} produces
-
 
30
      \eqn{-y}.}
-
 
31
    
-
 
32
    \item{\emph{Subscripts and superscripts:}}{
-
 
33
      a subscript is specified using the subsetting syntax and a
-
 
34
      superscript is specified using the power syntax.
-
 
35
      
-
 
36
      For example, \code{x[i]} produces \eqn{x_i} and \code{x^2}
-
 
37
      produces \eqn{x^2}.}
-
 
38
    
-
 
39
    \item{\emph{Accents:}}{
-
 
40
      accented expressions are specified using the special mathematical
-
 
41
      functions \code{hat} and \code{bar}.}
-
 
42
    
-
 
43
    % For example, \code{hat(x)} produces \eqn{\hat{x}} and \code{bar(x)}
-
 
44
    % produces \eqn{\bar{x}}.
-
 
45
    
-
 
46
    \item{\emph{Fractions:}}{
-
 
47
      fractions are specified using the special mathematical function
-
 
48
      \code{frac} (or its alias, \code{over}).}
-
 
49
    
-
 
50
    % For example, \code{frac(1,2)} produces \eqn{1\over2}.
-
 
51
    
-
 
52
    \item{\emph{Relations:}}{
-
 
53
      equality or assignment of terms is specified using the \code{==}
-
 
54
      relation.
-
 
55
      
-
 
56
      For example, \code{x == y} produces \eqn{x=y}.}
-
 
57
    
-
 
58
    \item{\emph{Visible grouping:}}{
-
 
59
      terms are visibly grouped by placing them within parentheses.
-
 
60
      
-
 
61
      For example, \code{(x+y)} produces \eqn{(x+y)}.}
-
 
62
    
-
 
63
    \item{\emph{Invisible grouping:}}{
-
 
64
      terms are invisibly grouped by placing them within curly braces.
-
 
65
 
-
 
66
      For example, \code{x^{2*y}} produces \eqn{x^{2y}}, whereas
-
 
67
      \code{x^2*y} produces \eqn{x^2y}.}
-
 
68
 
-
 
69
    \item{\emph{Big operators:}}{
-
 
70
      a sum, product, or integral is specified using the special
-
 
71
      mathematical function of the corresponding name.  Each of these
-
 
72
      functions takes three arguments;  the first indicates what is
-
 
73
      being summed/multiplied/integrated and the second and third
-
 
74
      specify the limits of the summation/product/integral.
-
 
75
      
-
 
76
      For example, \code{sum(x[i], i==0, n)} produces
-
 
77
      \deqn{\sum\limits_{i=0}^n x_i}{sum_{i=0}^n x_i}}
-
 
78
 
-
 
79
    \item{\emph{Radicals:}}{
-
 
80
      a square root expression is specified using the special
-
 
81
      mathematical functions \code{root} and \code{sqrt}.}
-
 
82
      
-
 
83
    % For example, \code{sqrt(x)} produces \eqn{\sqrt x}.
-
 
84
 
-
 
85
    \item{\emph{Absolute values:}}{
-
 
86
      an absolute term is specified using the special mathematical
-
 
87
      function \code{abs}.
-
 
88
 
-
 
89
      For example, \code{abs(x)} produces \eqn{|x|}.}
-
 
90
 
-
 
91
    \item{\emph{Juxtaposition:}}{
-
 
92
      multiple terms are juxtaposed using the special mathematical
-
 
93
      function \code{paste}.
-
 
94
      
-
 
95
      For example, \code{paste(over(b, 2), y, sum(x))} produces
-
 
96
      \eqn{\frac{b}{2} y \sum x}{b/2 y sum(x)}.}
-
 
97
   
-
 
98
    \item{\emph{Typeface changes:}}{
-
 
99
      the default font in mathematical expressions is italic (except for
-
 
100
      terms which are symbols).  A new typeface is specified using the
-
 
101
      special mathematical functions \code{bold}, \code{italic},
-
 
102
      \code{plain}, and \code{bolditalic}.  Note that these font
-
 
103
      specifications do not accumulate (i.e., \code{bold(italic(x)))}
-
 
104
      gives an italic `x', whereas \code{bolditalic(x)} produces a bold,
-
 
105
      italic `x').}
-
 
106
 
-
 
107
    \item{\emph{General expressions:}}{
-
 
108
      any functional expression which is not a special mathematical
-
 
109
      function is simply reproduced as a function expression.
-
 
110
 
15
 
-
 
16
  It is possible to produce many different mathematical symbols, generate
111
      For example, \code{foo(x)} produces \eqn{foo(x)}.}
17
  sub- or superscripts, produce fractions, etc.
112
  }
18
 
-
 
19
  The output from \code{example(plotmath)} includes several tables which
-
 
20
  show the available features.  In these tables, the columns of grey text
-
 
21
  show sample \R expressions, and the columns of black text show the
-
 
22
  resulting output.
113
}
23
}
-
 
24
\references{Murrell, P. and Ihaka, R. \emph{An approach to providing
-
 
25
  mathematical annotation in plots}, Journal of Computational and Graphical
-
 
26
  Statistics (In Press).}
114
\seealso{
27
\seealso{
115
  \code{\link{axis}},
28
  \code{\link{axis}},
116
  \code{\link{mtext}},
29
  \code{\link{mtext}},
117
  \code{\link{text}},
30
  \code{\link{text}},
118
  \code{\link{title}}
31
  \code{\link{title}}
Line 137... Line 50...
137
text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))",
50
text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))",
138
     cex = .8)
51
     cex = .8)
139
text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ",
52
text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ",
140
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})),
53
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})),
141
     cex= 1.2)
54
     cex= 1.2)
-
 
55
 
-
 
56
######
-
 
57
# create tables of mathematical annotation functionality
-
 
58
######
-
 
59
make.table <- function(nr, nc) {
-
 
60
    savepar <- par(mar=rep(0, 4), pty="s")
-
 
61
    plot(c(0, nc*2 + 1), c(0, -(nr + 1)), 
-
 
62
         type="n", xlab="", ylab="", axes=F)
-
 
63
    savepar
-
 
64
}
-
 
65
 
-
 
66
get.r <- function(i, nr) {
-
 
67
    i \%\% nr + 1
-
 
68
}
-
 
69
 
-
 
70
get.c <- function(i, nr) {
-
 
71
    i \%/\% nr + 1
-
 
72
}
-
 
73
 
-
 
74
draw.title.cell <- function(title, i, nr) {
-
 
75
    r <- get.r(i, nr)
-
 
76
    c <- get.c(i, nr)
-
 
77
    text(2*c - .5, -r, title)
-
 
78
    rect((2*(c - 1) + .5), -(r - .5), (2*c + .5), -(r + .5))
-
 
79
}
-
 
80
 
-
 
81
draw.plotmath.cell <- function(expr, i, nr, string = NULL) {
-
 
82
    r <- get.r(i, nr)
-
 
83
    c <- get.c(i, nr)
-
 
84
    if (is.null(string)) {
-
 
85
        string <- deparse(expr)
-
 
86
	string <- substr(string, 12, nchar(string) - 1)
-
 
87
    }
-
 
88
    text((2*(c - 1) + 1), -r, string, col="grey")
-
 
89
    text((2*c), -r, expr, adj=c(.5,.5))
-
 
90
    rect((2*(c - 1) + .5), -(r - .5), (2*c + .5), -(r + .5), border="grey")
-
 
91
}
-
 
92
 
-
 
93
nr <- 20
-
 
94
nc <- 2
-
 
95
oldpar <- make.table(nr, nc)
-
 
96
i <- 0
-
 
97
draw.title.cell("Arithmetic Operators", i, nr); i <- i + 1
-
 
98
draw.plotmath.cell(expression(x + y), i, nr); i <- i + 1
-
 
99
draw.plotmath.cell(expression(x - y), i, nr); i <- i + 1
-
 
100
draw.plotmath.cell(expression(x * y), i, nr); i <- i + 1
-
 
101
draw.plotmath.cell(expression(x / y), i, nr); i <- i + 1
-
 
102
draw.plotmath.cell(expression(x \%+-\% y), i, nr); i <- i + 1
-
 
103
draw.plotmath.cell(expression(x \%/\% y), i, nr); i <- i + 1
-
 
104
draw.plotmath.cell(expression(x \%*\% y), i, nr); i <- i + 1
-
 
105
draw.plotmath.cell(expression(-x), i, nr); i <- i + 1
-
 
106
draw.plotmath.cell(expression(+x), i, nr); i <- i + 1
-
 
107
draw.title.cell("Sub/Superscripts", i, nr); i <- i + 1
-
 
108
draw.plotmath.cell(expression(x[i]), i, nr); i <- i + 1
-
 
109
draw.plotmath.cell(expression(x^2), i, nr); i <- i + 1
-
 
110
draw.title.cell("Juxtaposition", i, nr); i <- i + 1
-
 
111
draw.plotmath.cell(expression(x * y), i, nr); i <- i + 1
-
 
112
draw.plotmath.cell(expression(paste(x, y, z)), i, nr); i <- i + 1
-
 
113
draw.title.cell("Lists", i, nr); i <- i + 1
-
 
114
draw.plotmath.cell(expression(list(x, y, z)), i, nr); i <- i + 1
-
 
115
# even columns up
-
 
116
i <- 20
-
 
117
draw.title.cell("Radicals", i, nr); i <- i + 1
-
 
118
draw.plotmath.cell(expression(sqrt(x)), i, nr); i <- i + 1
-
 
119
draw.plotmath.cell(expression(sqrt(x, y)), i, nr); i <- i + 1
-
 
120
draw.title.cell("Relations", i, nr); i <- i + 1
-
 
121
draw.plotmath.cell(expression(x == y), i, nr); i <- i + 1
-
 
122
draw.plotmath.cell(expression(x != y), i, nr); i <- i + 1
-
 
123
draw.plotmath.cell(expression(x < y), i, nr); i <- i + 1
-
 
124
draw.plotmath.cell(expression(x <= y), i, nr); i <- i + 1
-
 
125
draw.plotmath.cell(expression(x > y), i, nr); i <- i + 1
-
 
126
draw.plotmath.cell(expression(x >= y), i, nr); i <- i + 1
-
 
127
draw.plotmath.cell(expression(x \%~~\% y), i, nr); i <- i + 1
-
 
128
draw.plotmath.cell(expression(x \%=~\% y), i, nr); i <- i + 1
-
 
129
draw.plotmath.cell(expression(x \%==\% y), i, nr); i <- i + 1
-
 
130
draw.plotmath.cell(expression(x \%prop\% y), i, nr); i <- i + 1
-
 
131
draw.title.cell("Typeface", i, nr); i <- i + 1
-
 
132
draw.plotmath.cell(expression(plain(x)), i, nr); i <- i + 1
-
 
133
draw.plotmath.cell(expression(italic(x)), i, nr); i <- i + 1
-
 
134
draw.plotmath.cell(expression(bold(x)), i, nr); i <- i + 1
-
 
135
draw.plotmath.cell(expression(bolditalic(x)), i, nr); i <- i + 1
-
 
136
 
-
 
137
# Need fewer, wider columns for ellipsis ...
-
 
138
nr <- 20
-
 
139
nc <- 2
-
 
140
make.table(nr, nc)
-
 
141
i <- 0
-
 
142
draw.title.cell("Ellipsis", i, nr); i <- i + 1
-
 
143
draw.plotmath.cell(expression(list(x[1], ..., x[n])), i, nr); i <- i + 1
-
 
144
draw.plotmath.cell(expression(x[1] + ... + x[n]), i, nr); i <- i + 1
-
 
145
draw.plotmath.cell(expression(list(x[1], cdots, x[n])), i, nr); i <- i + 1
-
 
146
draw.plotmath.cell(expression(x[1] + ldots + x[n]), i, nr); i <- i + 1
-
 
147
draw.title.cell("Set Relations", i, nr); i <- i + 1
-
 
148
draw.plotmath.cell(expression(x \%subset\% y), i, nr); i <- i + 1
-
 
149
draw.plotmath.cell(expression(x \%subseteq\% y), i, nr); i <- i + 1
-
 
150
draw.plotmath.cell(expression(x \%supset\% y), i, nr); i <- i + 1
-
 
151
draw.plotmath.cell(expression(x \%supseteq\% y), i, nr); i <- i + 1
-
 
152
draw.plotmath.cell(expression(x \%notsubset\% y), i, nr); i <- i + 1
-
 
153
draw.plotmath.cell(expression(x \%in\% y), i, nr); i <- i + 1
-
 
154
draw.plotmath.cell(expression(x \%notin\% y), i, nr); i <- i + 1
-
 
155
draw.title.cell("Accents", i, nr); i <- i + 1
-
 
156
draw.plotmath.cell(expression(hat(x)), i, nr); i <- i + 1
-
 
157
draw.plotmath.cell(expression(tilde(x)), i, nr); i <- i + 1
-
 
158
draw.plotmath.cell(expression(ring(x)), i, nr); i <- i + 1
-
 
159
draw.plotmath.cell(expression(bar(xy)), i, nr); i <- i + 1
-
 
160
draw.plotmath.cell(expression(widehat(xy)), i, nr); i <- i + 1
-
 
161
draw.plotmath.cell(expression(widetilde(xy)), i, nr); i <- i + 1
-
 
162
draw.title.cell("Arrows", i, nr); i <- i + 1
-
 
163
draw.plotmath.cell(expression(x \%<->\% y), i, nr); i <- i + 1
-
 
164
draw.plotmath.cell(expression(x \%->\% y), i, nr); i <- i + 1
-
 
165
draw.plotmath.cell(expression(x \%<-\% y), i, nr); i <- i + 1
-
 
166
draw.plotmath.cell(expression(x \%up\% y), i, nr); i <- i + 1
-
 
167
draw.plotmath.cell(expression(x \%down\% y), i, nr); i <- i + 1
-
 
168
draw.plotmath.cell(expression(x \%<=>\% y), i, nr); i <- i + 1
-
 
169
draw.plotmath.cell(expression(x \%=>\% y), i, nr); i <- i + 1
-
 
170
draw.plotmath.cell(expression(x \%<=\% y), i, nr); i <- i + 1
-
 
171
draw.plotmath.cell(expression(x \%dblup\% y), i, nr); i <- i + 1
-
 
172
draw.plotmath.cell(expression(x \%dbldown\% y), i, nr); i <- i + 1
-
 
173
draw.title.cell("Symbolic Names", i, nr); i <- i + 1
-
 
174
draw.plotmath.cell(expression(Alpha - Omega), i, nr); i <- i + 1
-
 
175
draw.plotmath.cell(expression(alpha - omega), i, nr); i <- i + 1
-
 
176
draw.plotmath.cell(expression(infinity), i, nr); i <- i + 1
-
 
177
draw.plotmath.cell(expression(32 * degree), i, nr); i <- i + 1
-
 
178
draw.plotmath.cell(expression(60 * minute), i, nr); i <- i + 1
-
 
179
draw.plotmath.cell(expression(30 * second), i, nr); i <- i + 1
-
 
180
 
-
 
181
# Need even fewer, wider columns for typeface and style ...
-
 
182
nr <- 20
-
 
183
nc <- 1
-
 
184
make.table(nr, nc)
-
 
185
i <- 0
-
 
186
draw.title.cell("Style", i, nr); i <- i + 1
-
 
187
draw.plotmath.cell(expression(displaystyle(x)), i, nr); i <- i + 1
-
 
188
draw.plotmath.cell(expression(textstyle(x)), i, nr); i <- i + 1
-
 
189
draw.plotmath.cell(expression(scriptstyle(x)), i, nr); i <- i + 1
-
 
190
draw.plotmath.cell(expression(scriptscriptstyle(x)), i, nr); i <- i + 1
-
 
191
draw.title.cell("Spacing", i, nr); i <- i + 1
-
 
192
draw.plotmath.cell(expression(x ~~ y), i, nr); i <- i + 1
-
 
193
 
-
 
194
# Need fewer, taller rows for fractions ...
-
 
195
# cheat a bit to save pages
-
 
196
par(new =T)
-
 
197
nr <- 10
-
 
198
nc <- 1
-
 
199
make.table(nr, nc)
-
 
200
i <- 4
-
 
201
draw.plotmath.cell(expression(x + phantom(0) + y), i, nr); i <- i + 1
-
 
202
draw.plotmath.cell(expression(x + over(1, phantom(0))), i, nr); i <- i + 1
-
 
203
draw.title.cell("Fractions", i, nr); i <- i + 1
-
 
204
draw.plotmath.cell(expression(frac(x, y)), i, nr); i <- i + 1
-
 
205
draw.plotmath.cell(expression(over(x, y)), i, nr); i <- i + 1
-
 
206
draw.plotmath.cell(expression(atop(x, y)), i, nr); i <- i + 1
-
 
207
 
-
 
208
# Need fewer, taller rows and fewer, wider columns for big operators ...
-
 
209
nr <- 10
-
 
210
nc <- 1
-
 
211
make.table(nr, nc)
-
 
212
i <- 0
-
 
213
draw.title.cell("Big Operators", i, nr); i <- i + 1
-
 
214
draw.plotmath.cell(expression(sum(x[i], i=1, n)), i, nr); i <- i + 1
-
 
215
draw.plotmath.cell(expression(prod(plain(P)(X == x), x)), i, nr); i <- i + 1
-
 
216
draw.plotmath.cell(expression(integral(f(x) * dx, a, b)), i, nr); i <- i + 1
-
 
217
draw.plotmath.cell(expression(union(A[i], i==1, n)), i, nr); i <- i + 1
-
 
218
draw.plotmath.cell(expression(intersect(A[i], i==1, n)), i, nr); i <- i + 1
-
 
219
draw.plotmath.cell(expression(lim(f(x), x \%->\% 0)), i, nr); i <- i + 1
-
 
220
draw.plotmath.cell(expression(min(g(x), x >= 0)), i, nr); i <- i + 1
-
 
221
draw.plotmath.cell(expression(inf(S)), i, nr); i <- i + 1
-
 
222
draw.plotmath.cell(expression(sup(S)), i, nr); i <- i + 1
-
 
223
 
-
 
224
make.table(nr, nc)
-
 
225
i <- 0
-
 
226
draw.title.cell("Grouping", i, nr); i <- i + 1
-
 
227
draw.plotmath.cell(expression((x + y)*z), i, nr); i <- i + 1
-
 
228
draw.plotmath.cell(expression(x^y + z), i, nr); i <- i + 1
-
 
229
draw.plotmath.cell(expression(x^(y + z)), i, nr); i <- i + 1
-
 
230
# have to do this one by hand
-
 
231
draw.plotmath.cell(expression(x^{y + z}), i, nr, string="x^{y + z}"); i <- i + 1
-
 
232
draw.plotmath.cell(expression(group("(", list(a, b), "]")), i, nr); i <- i + 1
-
 
233
draw.plotmath.cell(expression(bgroup("(", atop(x, y), ")")), i, nr); i <- i + 1
-
 
234
draw.plotmath.cell(expression(group(lceil, x, rceil)), i, nr); i <- i + 1
-
 
235
draw.plotmath.cell(expression(group(lfloor, x, rfloor)), i, nr); i <- i + 1
-
 
236
draw.plotmath.cell(expression(group("|", x, "|")), i, nr); i <- i + 1
-
 
237
 
-
 
238
par(oldpar)
142
}
239
}
143
\keyword{aplot}
240
\keyword{aplot}