The R Project SVN R

Rev

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

Rev 19598 Rev 23008
Line 15... Line 15...
15
  than for normal \R expressions.
15
  than for normal \R expressions.
16
 
16
 
17
  It is possible to produce many different mathematical symbols, generate
17
  It is possible to produce many different mathematical symbols, generate
18
  sub- or superscripts, produce fractions, etc.
18
  sub- or superscripts, produce fractions, etc.
19
 
19
 
20
  The output from \code{example(plotmath)} includes several tables which
20
  The output from \code{demo(plotmath)} includes several tables which
21
  show the available features.  In these tables, the columns of grey text
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
22
  show sample \R expressions, and the columns of black text show the
23
  resulting output.
23
  resulting output.
24
 
24
 
25
  The available features are also described in the tables below:
25
  The available features are also described in the tables below:
Line 119... Line 119...
119
  mathematical annotation in plots.
119
  mathematical annotation in plots.
120
  \emph{Journal of Computational and Graphical Statistics},
120
  \emph{Journal of Computational and Graphical Statistics},
121
  \bold{9}, 582--599.
121
  \bold{9}, 582--599.
122
}
122
}
123
\seealso{
123
\seealso{
-
 
124
  \code{demo(plotmath)},
124
  \code{\link{axis}},
125
  \code{\link{axis}},
125
  \code{\link{mtext}},
126
  \code{\link{mtext}},
126
  \code{\link{text}},
127
  \code{\link{text}},
127
  \code{\link{title}}
128
  \code{\link{title}}
128
}
129
}
Line 153... Line 154...
153
text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n)))
154
text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n)))
154
text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))",
155
text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))",
155
     cex = .8)
156
     cex = .8)
156
text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ",
157
text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ",
157
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})),
158
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})),
158
     cex= 1.2)
159
     cex = 1.2)
159
 
-
 
160
######
-
 
161
# create tables of mathematical annotation functionality
-
 
162
######
-
 
163
make.table <- function(nr, nc) {
-
 
164
    savepar <- par(mar=rep(0, 4), pty="s")
-
 
165
    plot(c(0, nc*2 + 1), c(0, -(nr + 1)),
-
 
166
         type="n", xlab="", ylab="", axes=FALSE)
-
 
167
    savepar
-
 
168
}
-
 
169
 
-
 
170
get.r <- function(i, nr) {
-
 
171
    i \%\% nr + 1
-
 
172
}
-
 
173
 
-
 
174
get.c <- function(i, nr) {
-
 
175
    i \%/\% nr + 1
-
 
176
}
-
 
177
 
-
 
178
draw.title.cell <- function(title, i, nr) {
-
 
179
    r <- get.r(i, nr)
-
 
180
    c <- get.c(i, nr)
-
 
181
    text(2*c - .5, -r, title)
-
 
182
    rect((2*(c - 1) + .5), -(r - .5), (2*c + .5), -(r + .5))
-
 
183
}
-
 
184
 
-
 
185
draw.plotmath.cell <- function(expr, i, nr, string = NULL) {
-
 
186
    r <- get.r(i, nr)
-
 
187
    c <- get.c(i, nr)
-
 
188
    if (is.null(string)) {
-
 
189
        string <- deparse(expr)
-
 
190
        string <- substr(string, 12, nchar(string) - 1)
-
 
191
    }
-
 
192
    text((2*(c - 1) + 1), -r, string, col="grey")
-
 
193
    text((2*c), -r, expr, adj=c(.5,.5))
-
 
194
    rect((2*(c - 1) + .5), -(r - .5), (2*c + .5), -(r + .5), border="grey")
-
 
195
}
-
 
196
 
-
 
197
nr <- 20
-
 
198
nc <- 2
-
 
199
oldpar <- make.table(nr, nc)
-
 
200
i <- 0
-
 
201
draw.title.cell("Arithmetic Operators", i, nr); i <- i + 1
-
 
202
draw.plotmath.cell(expression(x + y), i, nr); i <- i + 1
-
 
203
draw.plotmath.cell(expression(x - y), i, nr); i <- i + 1
-
 
204
draw.plotmath.cell(expression(x * y), i, nr); i <- i + 1
-
 
205
draw.plotmath.cell(expression(x / y), i, nr); i <- i + 1
-
 
206
draw.plotmath.cell(expression(x \%+-\% y), i, nr); i <- i + 1
-
 
207
draw.plotmath.cell(expression(x \%/\% y), i, nr); i <- i + 1
-
 
208
draw.plotmath.cell(expression(x \%*\% y), i, nr); i <- i + 1
-
 
209
draw.plotmath.cell(expression(-x), i, nr); i <- i + 1
-
 
210
draw.plotmath.cell(expression(+x), i, nr); i <- i + 1
-
 
211
draw.title.cell("Sub/Superscripts", i, nr); i <- i + 1
-
 
212
draw.plotmath.cell(expression(x[i]), i, nr); i <- i + 1
-
 
213
draw.plotmath.cell(expression(x^2), i, nr); i <- i + 1
-
 
214
draw.title.cell("Juxtaposition", i, nr); i <- i + 1
-
 
215
draw.plotmath.cell(expression(x * y), i, nr); i <- i + 1
-
 
216
draw.plotmath.cell(expression(paste(x, y, z)), i, nr); i <- i + 1
-
 
217
draw.title.cell("Lists", i, nr); i <- i + 1
-
 
218
draw.plotmath.cell(expression(list(x, y, z)), i, nr); i <- i + 1
-
 
219
# even columns up
-
 
220
i <- 20
-
 
221
draw.title.cell("Radicals", i, nr); i <- i + 1
-
 
222
draw.plotmath.cell(expression(sqrt(x)), i, nr); i <- i + 1
-
 
223
draw.plotmath.cell(expression(sqrt(x, y)), i, nr); i <- i + 1
-
 
224
draw.title.cell("Relations", i, nr); i <- i + 1
-
 
225
draw.plotmath.cell(expression(x == y), i, nr); i <- i + 1
-
 
226
draw.plotmath.cell(expression(x != y), i, nr); i <- i + 1
-
 
227
draw.plotmath.cell(expression(x < y), i, nr); i <- i + 1
-
 
228
draw.plotmath.cell(expression(x <= y), i, nr); i <- i + 1
-
 
229
draw.plotmath.cell(expression(x > y), i, nr); i <- i + 1
-
 
230
draw.plotmath.cell(expression(x >= y), i, nr); i <- i + 1
-
 
231
draw.plotmath.cell(expression(x \%~~\% y), i, nr); i <- i + 1
-
 
232
draw.plotmath.cell(expression(x \%=~\% y), i, nr); i <- i + 1
-
 
233
draw.plotmath.cell(expression(x \%==\% y), i, nr); i <- i + 1
-
 
234
draw.plotmath.cell(expression(x \%prop\% y), i, nr); i <- i + 1
-
 
235
draw.title.cell("Typeface", i, nr); i <- i + 1
-
 
236
draw.plotmath.cell(expression(plain(x)), i, nr); i <- i + 1
-
 
237
draw.plotmath.cell(expression(italic(x)), i, nr); i <- i + 1
-
 
238
draw.plotmath.cell(expression(bold(x)), i, nr); i <- i + 1
-
 
239
draw.plotmath.cell(expression(bolditalic(x)), i, nr); i <- i + 1
-
 
240
 
-
 
241
# Need fewer, wider columns for ellipsis ...
-
 
242
nr <- 20
-
 
243
nc <- 2
-
 
244
make.table(nr, nc)
-
 
245
i <- 0
-
 
246
draw.title.cell("Ellipsis", i, nr); i <- i + 1
-
 
247
draw.plotmath.cell(expression(list(x[1], ..., x[n])), i, nr); i <- i + 1
-
 
248
draw.plotmath.cell(expression(x[1] + ... + x[n]), i, nr); i <- i + 1
-
 
249
draw.plotmath.cell(expression(list(x[1], cdots, x[n])), i, nr); i <- i + 1
-
 
250
draw.plotmath.cell(expression(x[1] + ldots + x[n]), i, nr); i <- i + 1
-
 
251
draw.title.cell("Set Relations", i, nr); i <- i + 1
-
 
252
draw.plotmath.cell(expression(x \%subset\% y), i, nr); i <- i + 1
-
 
253
draw.plotmath.cell(expression(x \%subseteq\% y), i, nr); i <- i + 1
-
 
254
draw.plotmath.cell(expression(x \%supset\% y), i, nr); i <- i + 1
-
 
255
draw.plotmath.cell(expression(x \%supseteq\% y), i, nr); i <- i + 1
-
 
256
draw.plotmath.cell(expression(x \%notsubset\% y), i, nr); i <- i + 1
-
 
257
draw.plotmath.cell(expression(x \%in\% y), i, nr); i <- i + 1
-
 
258
draw.plotmath.cell(expression(x \%notin\% y), i, nr); i <- i + 1
-
 
259
draw.title.cell("Accents", i, nr); i <- i + 1
-
 
260
draw.plotmath.cell(expression(hat(x)), i, nr); i <- i + 1
-
 
261
draw.plotmath.cell(expression(tilde(x)), i, nr); i <- i + 1
-
 
262
draw.plotmath.cell(expression(ring(x)), i, nr); i <- i + 1
-
 
263
draw.plotmath.cell(expression(bar(xy)), i, nr); i <- i + 1
-
 
264
draw.plotmath.cell(expression(widehat(xy)), i, nr); i <- i + 1
-
 
265
draw.plotmath.cell(expression(widetilde(xy)), i, nr); i <- i + 1
-
 
266
draw.title.cell("Arrows", i, nr); i <- i + 1
-
 
267
draw.plotmath.cell(expression(x \%<->\% y), i, nr); i <- i + 1
-
 
268
draw.plotmath.cell(expression(x \%->\% y), i, nr); i <- i + 1
-
 
269
draw.plotmath.cell(expression(x \%<-\% y), i, nr); i <- i + 1
-
 
270
draw.plotmath.cell(expression(x \%up\% y), i, nr); i <- i + 1
-
 
271
draw.plotmath.cell(expression(x \%down\% y), i, nr); i <- i + 1
-
 
272
draw.plotmath.cell(expression(x \%<=>\% y), i, nr); i <- i + 1
-
 
273
draw.plotmath.cell(expression(x \%=>\% y), i, nr); i <- i + 1
-
 
274
draw.plotmath.cell(expression(x \%<=\% y), i, nr); i <- i + 1
-
 
275
draw.plotmath.cell(expression(x \%dblup\% y), i, nr); i <- i + 1
-
 
276
draw.plotmath.cell(expression(x \%dbldown\% y), i, nr); i <- i + 1
-
 
277
draw.title.cell("Symbolic Names", i, nr); i <- i + 1
-
 
278
draw.plotmath.cell(expression(Alpha - Omega), i, nr); i <- i + 1
-
 
279
draw.plotmath.cell(expression(alpha - omega), i, nr); i <- i + 1
-
 
280
draw.plotmath.cell(expression(infinity), i, nr); i <- i + 1
-
 
281
draw.plotmath.cell(expression(32 * degree), i, nr); i <- i + 1
-
 
282
draw.plotmath.cell(expression(60 * minute), i, nr); i <- i + 1
-
 
283
draw.plotmath.cell(expression(30 * second), i, nr); i <- i + 1
-
 
284
 
-
 
285
# Need even fewer, wider columns for typeface and style ...
-
 
286
nr <- 20
-
 
287
nc <- 1
-
 
288
make.table(nr, nc)
-
 
289
i <- 0
-
 
290
draw.title.cell("Style", i, nr); i <- i + 1
-
 
291
draw.plotmath.cell(expression(displaystyle(x)), i, nr); i <- i + 1
-
 
292
draw.plotmath.cell(expression(textstyle(x)), i, nr); i <- i + 1
-
 
293
draw.plotmath.cell(expression(scriptstyle(x)), i, nr); i <- i + 1
-
 
294
draw.plotmath.cell(expression(scriptscriptstyle(x)), i, nr); i <- i + 1
-
 
295
draw.title.cell("Spacing", i, nr); i <- i + 1
-
 
296
draw.plotmath.cell(expression(x ~~ y), i, nr); i <- i + 1
-
 
297
 
-
 
298
# Need fewer, taller rows for fractions ...
-
 
299
# cheat a bit to save pages
-
 
300
par(new = TRUE)
-
 
301
nr <- 10
-
 
302
nc <- 1
-
 
303
make.table(nr, nc)
-
 
304
i <- 4
-
 
305
draw.plotmath.cell(expression(x + phantom(0) + y), i, nr); i <- i + 1
-
 
306
draw.plotmath.cell(expression(x + over(1, phantom(0))), i, nr); i <- i + 1
-
 
307
draw.title.cell("Fractions", i, nr); i <- i + 1
-
 
308
draw.plotmath.cell(expression(frac(x, y)), i, nr); i <- i + 1
-
 
309
draw.plotmath.cell(expression(over(x, y)), i, nr); i <- i + 1
-
 
310
draw.plotmath.cell(expression(atop(x, y)), i, nr); i <- i + 1
-
 
311
 
-
 
312
# Need fewer, taller rows and fewer, wider columns for big operators ...
-
 
313
nr <- 10
-
 
314
nc <- 1
-
 
315
make.table(nr, nc)
-
 
316
i <- 0
-
 
317
draw.title.cell("Big Operators", i, nr); i <- i + 1
-
 
318
draw.plotmath.cell(expression(sum(x[i], i=1, n)), i, nr); i <- i + 1
-
 
319
draw.plotmath.cell(expression(prod(plain(P)(X == x), x)), i, nr); i <- i + 1
-
 
320
draw.plotmath.cell(expression(integral(f(x) * dx, a, b)), i, nr); i <- i + 1
-
 
321
draw.plotmath.cell(expression(union(A[i], i==1, n)), i, nr); i <- i + 1
-
 
322
draw.plotmath.cell(expression(intersect(A[i], i==1, n)), i, nr); i <- i + 1
-
 
323
draw.plotmath.cell(expression(lim(f(x), x \%->\% 0)), i, nr); i <- i + 1
-
 
324
draw.plotmath.cell(expression(min(g(x), x >= 0)), i, nr); i <- i + 1
-
 
325
draw.plotmath.cell(expression(inf(S)), i, nr); i <- i + 1
-
 
326
draw.plotmath.cell(expression(sup(S)), i, nr); i <- i + 1
-
 
327
 
-
 
328
make.table(nr, nc)
-
 
329
i <- 0
-
 
330
draw.title.cell("Grouping", i, nr); i <- i + 1
-
 
331
draw.plotmath.cell(expression((x + y)*z), i, nr); i <- i + 1
-
 
332
draw.plotmath.cell(expression(x^y + z), i, nr); i <- i + 1
-
 
333
draw.plotmath.cell(expression(x^(y + z)), i, nr); i <- i + 1
-
 
334
# have to do this one by hand
-
 
335
draw.plotmath.cell(expression(x^{y + z}), i, nr, string="x^{y + z}"); i <- i + 1
-
 
336
draw.plotmath.cell(expression(group("(", list(a, b), "]")), i, nr); i <- i + 1
-
 
337
draw.plotmath.cell(expression(bgroup("(", atop(x, y), ")")), i, nr); i <- i + 1
-
 
338
draw.plotmath.cell(expression(group(lceil, x, rceil)), i, nr); i <- i + 1
-
 
339
draw.plotmath.cell(expression(group(lfloor, x, rfloor)), i, nr); i <- i + 1
-
 
340
draw.plotmath.cell(expression(group("|", x, "|")), i, nr); i <- i + 1
-
 
341
 
-
 
342
par(oldpar)
-
 
343
}
160
}
344
\keyword{aplot}
161
\keyword{aplot}