The R Project SVN R

Rev

Rev 61168 | Rev 66444 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/grDevices/man/plotmath.Rd
2
% Part of the R package, http://www.R-project.org
59039 ripley 3
% Copyright 1995-2010 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{plotmath}
56186 murdoch 7
\alias{plotmath}
43114 ripley 8
\alias{symbol}
9
\alias{plain}
10
\alias{bold}
11
\alias{italic}
12
\alias{bolditalic}
13
\alias{hat}
14
\alias{bar}
15
\alias{dot}
16
\alias{ring}
17
\alias{widehat}
18
\alias{widetilde}
19
\alias{displaystyle}
20
\alias{textstyle}
21
\alias{scriptstyle}
22
\alias{scriptscriptstyle}
23
\alias{underline}
24
\alias{phantom}
25
\alias{over}
26
\alias{frac}
27
\alias{atop}
28
\alias{integral}
29
\alias{inf}
30
\alias{sup}
31
\alias{group}
32
\alias{bgroup}
33
 
34
 
27442 ripley 35
\title{Mathematical Annotation in R}
36
\description{
37
  If the \code{text} argument to one of the text-drawing functions
38520 ripley 38
  (\code{\link{text}}, \code{\link{mtext}}, \code{\link{axis}},
39
  \code{\link{legend}}) in \R is an expression, the argument is
40
  interpreted as a mathematical expression and the output will be
41
  formatted according to TeX-like rules.  Expressions can also be used
42
  for titles, subtitles and x- and y-axis labels (but not for axis
43
  labels on \code{persp} plots).
44
 
46933 ripley 45
  In most cases other language objects (names and calls, including
57741 ripley 46
  formulas) are coerced to expressions and so can also be used.
47
}
27442 ripley 48
 
57741 ripley 49
\details{
50
  A mathematical expression must obey the normal rules of syntax for any
51
  \R expression, but it is interpreted according to very different rules
52
  than for normal \R expressions.
53
 
27442 ripley 54
  It is possible to produce many different mathematical symbols, generate
55
  sub- or superscripts, produce fractions, etc.
56
 
57
  The output from \code{demo(plotmath)} includes several tables which
58
  show the available features.  In these tables, the columns of grey text
59
  show sample \R expressions, and the columns of black text show the
60
  resulting output.
61
 
62
  The available features are also described in the tables below:
63
 
64
  \tabular{ll}{
65
    \bold{Syntax} \tab \bold{Meaning} \cr
36194 ripley 66
 
27442 ripley 67
    \code{x + y}   \tab x plus y   \cr
68
    \code{x - y}   \tab x minus y \cr
69
    \code{x*y}    \tab juxtapose x and y \cr
70
    \code{x/y}    \tab x forwardslash y \cr
71
    \code{x \%+-\% y}   \tab x plus or minus y \cr
72
    \code{x \%/\% y}   \tab x divided by y \cr
73
    \code{x \%*\% y}   \tab x times y \cr
38916 murrell 74
    \code{x \%.\% y}   \tab x cdot y \cr
27442 ripley 75
    \code{x[i]}   \tab x subscript i \cr
76
    \code{x^2}    \tab x superscript 2 \cr
77
    \code{paste(x, y, z)} \tab juxtapose x, y, and z \cr
78
    \code{sqrt(x)}   \tab square root of x \cr
79
    \code{sqrt(x, y)}   \tab yth root of x \cr
80
    \code{x == y}   \tab x equals y \cr
81
    \code{x != y}   \tab x is not equal to y \cr
82
    \code{x < y}   \tab x is less than y \cr
83
    \code{x <= y}   \tab x is less than or equal to y \cr
84
    \code{x > y}   \tab x is greater than y \cr
85
    \code{x >= y}   \tab x is greater than or equal to y \cr
86
    \code{x \%~~\% y}   \tab x is approximately equal to y \cr
87
    \code{x \%=~\% y}   \tab x and y are congruent \cr
88
    \code{x \%==\% y}   \tab x is defined as y \cr
89
    \code{x \%prop\% y}  \tab x is proportional to y \cr
60811 murrell 90
    \code{x \%~\% y}  \tab x is distributed as y \cr
27442 ripley 91
    \code{plain(x)}   \tab draw x in normal font \cr
92
    \code{bold(x)}   \tab draw x in bold font \cr
93
    \code{italic(x)}   \tab draw x in italic font \cr
94
    \code{bolditalic(x)} \tab draw x in bolditalic font \cr
42633 murrell 95
    \code{symbol(x)} \tab draw x in symbol font \cr
27442 ripley 96
    \code{list(x, y, z)} \tab comma-separated list \cr
97
    \code{...}    \tab ellipsis (height varies) \cr
98
    \code{cdots}   \tab ellipsis (vertically centred) \cr
99
    \code{ldots}   \tab ellipsis (at baseline) \cr
100
    \code{x \%subset\% y} \tab x is a proper subset of y \cr
101
    \code{x \%subseteq\% y} \tab x is a subset of y \cr
102
    \code{x \%notsubset\% y} \tab x is not a subset of y \cr
103
    \code{x \%supset\% y} \tab x is a proper superset of y \cr
104
    \code{x \%supseteq\% y} \tab x is a superset of y \cr
105
    \code{x \%in\% y}   \tab x is an element of y \cr
106
    \code{x \%notin\% y} \tab x is not an element of y \cr
107
    \code{hat(x)}   \tab x with a circumflex \cr
108
    \code{tilde(x)}   \tab x with a tilde \cr
109
    \code{dot(x)} \tab x with a dot \cr
110
    \code{ring(x)}   \tab x with a ring \cr
111
    \code{bar(xy)}   \tab xy with bar \cr
112
    \code{widehat(xy)}   \tab xy with a wide circumflex \cr
113
    \code{widetilde(xy)} \tab xy with a wide tilde \cr
114
    \code{x \%<->\% y}   \tab x double-arrow y \cr
115
    \code{x \%->\% y}   \tab x right-arrow y \cr
116
    \code{x \%<-\% y}   \tab x left-arrow y \cr
117
    \code{x \%up\% y}   \tab x up-arrow y \cr
118
    \code{x \%down\% y}  \tab x down-arrow y \cr
119
    \code{x \%<=>\% y}   \tab x is equivalent to y \cr
120
    \code{x \%=>\% y}   \tab x implies y \cr
121
    \code{x \%<=\% y}   \tab y implies x \cr
122
    \code{x \%dblup\% y}   \tab x double-up-arrow y \cr
123
    \code{x \%dbldown\% y} \tab x double-down-arrow y \cr
124
    \code{alpha} -- \code{omega} \tab Greek symbols \cr
125
    \code{Alpha} -- \code{Omega} \tab uppercase Greek symbols \cr
36211 ripley 126
    \code{theta1, phi1, sigma1, omega1} \tab cursive Greek symbols\cr
37484 ripley 127
    \code{Upsilon1} \tab capital upsilon with hook\cr
45967 ripley 128
    \code{aleph} \tab first letter of Hebrew alphabet\cr
27442 ripley 129
    \code{infinity}   \tab infinity symbol \cr
130
    \code{partialdiff} \tab partial differential symbol \cr
45967 ripley 131
    \code{nabla} \tab nabla, gradient symbol\cr
27442 ripley 132
    \code{32*degree}   \tab 32 degrees \cr
133
    \code{60*minute}   \tab 60 minutes of angle \cr
134
    \code{30*second}   \tab 30 seconds of angle \cr
135
    \code{displaystyle(x)} \tab draw x in normal size (extra spacing) \cr
136
    \code{textstyle(x)}  \tab draw x in normal size \cr
137
    \code{scriptstyle(x)} \tab draw x in small size \cr
138
    \code{scriptscriptstyle(x)} \tab draw x in very small size \cr
31430 ripley 139
    \code{underline(x)}   \tab draw x underlined\cr
27442 ripley 140
    \code{x ~~ y}        \tab put extra space between x and y \cr
141
    \code{x + phantom(0) + y} \tab leave gap for "0", but don't draw it \cr
142
    \code{x + over(1, phantom(0))} \tab leave vertical gap for "0" (don't draw) \cr
143
    \code{frac(x, y)}   \tab x over y \cr
144
    \code{over(x, y)}   \tab x over y \cr
145
    \code{atop(x, y)}   \tab x over y (no horizontal bar) \cr
146
    \code{sum(x[i], i==1, n)} \tab sum x[i] for i equals 1 to n \cr
147
    \code{prod(plain(P)(X==x), x)} \tab product of P(X=x) for all values of x \cr
148
    \code{integral(f(x)*dx, a, b)} \tab definite integral of f(x) wrt x \cr
149
    \code{union(A[i], i==1, n)} \tab union of A[i] for i equals 1 to n \cr
150
    \code{intersect(A[i], i==1, n)} \tab intersection of A[i] \cr
151
    \code{lim(f(x), x \%->\% 0)} \tab limit of f(x) as x tends to 0 \cr
152
    \code{min(g(x), x > 0)} \tab minimum of g(x) for x greater than 0 \cr
153
    \code{inf(S)}        \tab infimum of S \cr
154
    \code{sup(S)}   \tab supremum of S \cr
155
    \code{x^y + z}   \tab normal operator precedence \cr
156
    \code{x^(y + z)}    \tab visible grouping of operands \cr
157
    \code{x^{y + z}}  \tab invisible grouping of operands \cr
158
    \code{group("(",list(a, b),"]")} \tab specify left and right delimiters \cr
159
    \code{bgroup("(",atop(x,y),")")} \tab use scalable delimiters \cr
160
    \code{group(lceil, x, rceil)} \tab special delimiters \cr
161
  }
36210 ripley 162
 
43133 ripley 163
  The symbol font uses Adobe Symbol encoding so, for example, a lower
164
  case mu can be obtained either by the special symbol \code{mu} or by
165
  \code{symbol("m")}.  This provides access to symbols that have no
166
  special symbol name, for example, the universal, or forall, symbol is
51831 ripley 167
  \code{symbol("\\042")}.  To see what symbols are available in this way
168
  use \code{TestChars(font=5)} as given in the examples for
169
  \code{\link{points}}: some are only available on some devices.
42633 murrell 170
 
49626 hornik 171
  Note to TeX users: TeX's \samp{\\Upsilon} is \code{Upsilon1}, TeX's
46957 ripley 172
  \samp{\\varepsilon} is close to \code{epsilon}, and there is no
173
  equivalent of TeX's \samp{\\epsilon}.  TeX's \samp{\\varpi} is close to
36211 ripley 174
  \code{omega1}.  \code{vartheta}, \code{varphi} and \code{varsigma} are
175
  allowed as synonyms for \code{theta1}, \code{phi1} and \code{sigma1}.
36214 ripley 176
 
177
  \code{sigma1} is also known as \code{stigma}, its Unicode name.
38797 ripley 178
 
46945 murdoch 179
  Control characters (e.g. \samp{\\n}) are not interpreted in character
38797 ripley 180
  strings in plotmath, unlike normal plotting.
46107 ripley 181
 
182
  The fonts used are taken from the current font family, and so can be
183
  set by \code{\link{par}(family=)} in base graphics, and
184
  \code{\link{gpar}(fontfamily=)} in package \pkg{grid}.
51385 ripley 185
 
186
  Note that \code{bold}, \code{italic} and \code{bolditalic} do not
187
  apply to symbols, and hence not to the Greek \emph{symbols} such as
52306 ripley 188
  \code{mu} which are displayed in the symbol font.  They also do not
189
  apply to numeric constants.
27442 ripley 190
}
46933 ripley 191
\section{Other symbols}{
51385 ripley 192
  On many OSes and some graphics devices many other symbols are
193
  available as part of the standard text font, and all of the symbols in
194
  the Adobe Symbol encoding are in principle available \emph{via}
195
  changing the font face or (see \sQuote{Details}) plotmath: see the
196
  examples section of \code{\link{points}} for a function to display
197
  them.  (\sQuote{In principle} because some of the glyphs are missing
198
  from some implementations of the symbol font.)  Unfortunately,
46933 ripley 199
  \code{\link{postscript}} and \code{\link{pdf}} have support for little
51385 ripley 200
  more than European (not Greek) and CJK characters and the Adobe Symbol
201
  encoding (and in a few fonts, also Cyrillic characters).
46933 ripley 202
 
203
#ifdef unix
204
  In a UTF-8 locale any Unicode character can be entered, perhaps as a
49626 hornik 205
  \samp{\\uxxxx} or \samp{\\Uxxxxxxxx} escape sequence, but the issue is
46960 ripley 206
  whether the graphics device is able to display the character.  The
46933 ripley 207
  widest range of characters is likely to be available in the
208
  \code{\link{X11}} device using cairo: see its help page for how
51385 ripley 209
  installing additional fonts can help.  This can often be used to
210
  display Greek \emph{letters} in bold or italic.
46933 ripley 211
 
212
  In non-UTF-8 locales there is normally no support for symbols not in
213
  the languages for which the current encoding was intended.
214
#endif
215
#ifdef windows
216
  Any Unicode character can be entered into a text string \emph{via} a
49626 hornik 217
  \samp{\\uxxxx} escape, or used by number in a call to
46933 ripley 218
  \code{\link{points}}.  The \code{\link{windows}} family of devices can
51385 ripley 219
  display such characters if they are available in the font in use.
220
  This can often be used to display Greek \emph{letters} in bold or italic.
61433 ripley 221
 
51385 ripley 222
  A good way to both find out which characters are available in a font
223
  and to determine the Unicode number is to use the \sQuote{Character
224
  Map} accessory (usually on the \sQuote{Start} menu under
46960 ripley 225
  \sQuote{Accessories->System Tools}).  You can also copy-and-paste
51385 ripley 226
  characters from the \sQuote{Character Map} window to the \code{Rgui}
227
  console (but not to \code{Rterm}).
46933 ripley 228
#endif
229
}
27442 ripley 230
\references{
231
  Murrell, P. and Ihaka, R. (2000) An approach to providing
232
  mathematical annotation in plots.
233
  \emph{Journal of Computational and Graphical Statistics},
234
  \bold{9}, 582--599.
43114 ripley 235
 
236
  The symbol codes can be found in octal in the Adobe reference manuals,
237
  e.g. for Postscript
238
  \url{http://www.adobe.com/products/postscript/pdfs/PLRM.pdf}
239
  or PDF
240
  \url{http://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf}
43127 ripley 241
  and in decimal, octal and hex at
43114 ripley 242
  \url{http://www.stat.auckland.ac.nz/~paul/R/CM/AdobeSym.html}.
27442 ripley 243
}
244
\seealso{
245
  \code{demo(plotmath)},
246
  \code{\link{axis}},
247
  \code{\link{mtext}},
248
  \code{\link{text}},
249
  \code{\link{title}},
250
  \code{\link{substitute}}
251
  \code{\link{quote}}, \code{\link{bquote}}
252
}
253
\examples{
41508 ripley 254
require(graphics)
255
 
27442 ripley 256
x <- seq(-4, 4, len = 101)
257
y <- cbind(sin(x), cos(x))
258
matplot(x, y, type = "l", xaxt = "n",
259
        main = expression(paste(plain(sin) * phi, "  and  ",
260
                                plain(cos) * phi)),
261
        ylab = expression("sin" * phi, "cos" * phi), # only 1st is taken
262
        xlab = expression(paste("Phase Angle ", phi)),
263
        col.main = "blue")
264
axis(1, at = c(-pi, -pi/2, 0, pi/2, pi),
36154 ripley 265
     labels = expression(-pi, -pi/2, 0, pi/2, pi))
27442 ripley 266
 
267
 
268
## How to combine "math" and numeric variables :
269
plot(1:10, type="n", xlab="", ylab="", main = "plot math & numbers")
48951 maechler 270
theta <- 1.23 ; mtext(bquote(hat(theta) == .(theta)), line= .25)
27442 ripley 271
for(i in 2:9)
61168 ripley 272
    text(i, i+1, substitute(list(xi, eta) == group("(",list(x,y),")"),
273
                            list(x = i, y = i+1)))
38520 ripley 274
## note that both of these use calls rather than expressions.
48951 maechler 275
##
61168 ripley 276
text(1, 10,  "Derivatives:", adj = 0)
277
text(1, 9.6, expression(
278
 "             first: {f * minute}(x) " == {f * minute}(x)), adj = 0)
279
text(1, 9.0, expression(
280
 "     second: {f * second}(x) "        == {f * second}(x)), adj = 0)
27442 ripley 281
 
48951 maechler 282
 
27442 ripley 283
plot(1:10, 1:10)
284
text(4, 9, expression(hat(beta) == (X^t * X)^{-1} * X^t * y))
285
text(4, 8.4, "expression(hat(beta) == (X^t * X)^{-1} * X^t * y)",
286
     cex = .8)
287
text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n)))
288
text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))",
289
     cex = .8)
290
text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ",
291
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})),
292
     cex = 1.2)
45967 ripley 293
 
294
## some other useful symbols
295
plot.new(); plot.window(c(0,4), c(15,1))
61168 ripley 296
text(1, 1, "universal", adj = 0); text(2.5, 1,  "\\\\042")
45967 ripley 297
text(3, 1, expression(symbol("\\042")))
61168 ripley 298
text(1, 2, "existential", adj = 0); text(2.5, 2,  "\\\\044")
45967 ripley 299
text(3, 2, expression(symbol("\\044")))
61168 ripley 300
text(1, 3, "suchthat", adj = 0); text(2.5, 3,  "\\\\047")
45967 ripley 301
text(3, 3, expression(symbol("\\047")))
61168 ripley 302
text(1, 4, "therefore", adj = 0); text(2.5, 4,  "\\\\134")
45967 ripley 303
text(3, 4, expression(symbol("\\134")))
61168 ripley 304
text(1, 5, "perpendicular", adj = 0); text(2.5, 5,  "\\\\136")
45967 ripley 305
text(3, 5, expression(symbol("\\136")))
61168 ripley 306
text(1, 6, "circlemultiply", adj = 0); text(2.5, 6,  "\\\\304")
45967 ripley 307
text(3, 6, expression(symbol("\\304")))
61168 ripley 308
text(1, 7, "circleplus", adj = 0); text(2.5, 7,  "\\\\305")
45967 ripley 309
text(3, 7, expression(symbol("\\305")))
61168 ripley 310
text(1, 8, "emptyset", adj = 0); text(2.5, 8,  "\\\\306")
45967 ripley 311
text(3, 8, expression(symbol("\\306")))
61168 ripley 312
text(1, 9, "angle", adj = 0); text(2.5, 9,  "\\\\320")
45967 ripley 313
text(3, 9, expression(symbol("\\320")))
61168 ripley 314
text(1, 10, "leftangle", adj = 0); text(2.5, 10,  "\\\\341")
45967 ripley 315
text(3, 10, expression(symbol("\\341")))
61168 ripley 316
text(1, 11, "rightangle", adj = 0); text(2.5, 11,  "\\\\361")
45967 ripley 317
text(3, 11, expression(symbol("\\361")))
27442 ripley 318
}
319
\keyword{aplot}