The R Project SVN R

Rev

Rev 88593 | 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
68948 ripley 2
% Part of the R package, https://www.R-project.org
82890 maechler 3
% Copyright 1995-2022 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{plotmath}
56186 murdoch 7
\alias{plotmath}
82890 maechler 8
\alias{symbol}% also for R symbols aka names in ../../base/man/name.Rd
43114 ripley 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
85927 hornik 70
    \code{x/y}    \tab x \I{forwardslash} y \cr
27442 ripley 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
85925 hornik 74
    \code{x \%.\% y}   \tab x \I{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
85254 hornik 79
    \code{sqrt(x, y)}   \tab y-th root of x \cr
27442 ripley 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
72446 murrell 86
    \code{!x} \tab not x \cr
27442 ripley 87
    \code{x \%~~\% y}   \tab x is approximately equal to y \cr
88
    \code{x \%=~\% y}   \tab x and y are congruent \cr
89
    \code{x \%==\% y}   \tab x is defined as y \cr
90
    \code{x \%prop\% y}  \tab x is proportional to y \cr
60811 murrell 91
    \code{x \%~\% y}  \tab x is distributed as y \cr
27442 ripley 92
    \code{plain(x)}   \tab draw x in normal font \cr
93
    \code{bold(x)}   \tab draw x in bold font \cr
94
    \code{italic(x)}   \tab draw x in italic font \cr
85925 hornik 95
    \code{bolditalic(x)} \tab draw x in bold italic font \cr
42633 murrell 96
    \code{symbol(x)} \tab draw x in symbol font \cr
27442 ripley 97
    \code{list(x, y, z)} \tab comma-separated list \cr
98
    \code{...}    \tab ellipsis (height varies) \cr
99
    \code{cdots}   \tab ellipsis (vertically centred) \cr
100
    \code{ldots}   \tab ellipsis (at baseline) \cr
101
    \code{x \%subset\% y} \tab x is a proper subset of y \cr
102
    \code{x \%subseteq\% y} \tab x is a subset of y \cr
103
    \code{x \%notsubset\% y} \tab x is not a subset of y \cr
104
    \code{x \%supset\% y} \tab x is a proper superset of y \cr
105
    \code{x \%supseteq\% y} \tab x is a superset of y \cr
106
    \code{x \%in\% y}   \tab x is an element of y \cr
107
    \code{x \%notin\% y} \tab x is not an element of y \cr
108
    \code{hat(x)}   \tab x with a circumflex \cr
109
    \code{tilde(x)}   \tab x with a tilde \cr
110
    \code{dot(x)} \tab x with a dot \cr
111
    \code{ring(x)}   \tab x with a ring \cr
85908 hornik 112
    \code{bar(xy)}   \tab \I{xy} with bar \cr
113
    \code{widehat(xy)}   \tab \I{xy} with a wide circumflex \cr
114
    \code{widetilde(xy)} \tab \I{xy} with a wide tilde \cr
27442 ripley 115
    \code{x \%<->\% y}   \tab x double-arrow y \cr
116
    \code{x \%->\% y}   \tab x right-arrow y \cr
117
    \code{x \%<-\% y}   \tab x left-arrow y \cr
118
    \code{x \%up\% y}   \tab x up-arrow y \cr
119
    \code{x \%down\% y}  \tab x down-arrow y \cr
120
    \code{x \%<=>\% y}   \tab x is equivalent to y \cr
121
    \code{x \%=>\% y}   \tab x implies y \cr
122
    \code{x \%<=\% y}   \tab y implies x \cr
123
    \code{x \%dblup\% y}   \tab x double-up-arrow y \cr
124
    \code{x \%dbldown\% y} \tab x double-down-arrow y \cr
125
    \code{alpha} -- \code{omega} \tab Greek symbols \cr
126
    \code{Alpha} -- \code{Omega} \tab uppercase Greek symbols \cr
36211 ripley 127
    \code{theta1, phi1, sigma1, omega1} \tab cursive Greek symbols\cr
37484 ripley 128
    \code{Upsilon1} \tab capital upsilon with hook\cr
45967 ripley 129
    \code{aleph} \tab first letter of Hebrew alphabet\cr
27442 ripley 130
    \code{infinity}   \tab infinity symbol \cr
131
    \code{partialdiff} \tab partial differential symbol \cr
45967 ripley 132
    \code{nabla} \tab nabla, gradient symbol\cr
27442 ripley 133
    \code{32*degree}   \tab 32 degrees \cr
134
    \code{60*minute}   \tab 60 minutes of angle \cr
135
    \code{30*second}   \tab 30 seconds of angle \cr
136
    \code{displaystyle(x)} \tab draw x in normal size (extra spacing) \cr
137
    \code{textstyle(x)}  \tab draw x in normal size \cr
138
    \code{scriptstyle(x)} \tab draw x in small size \cr
139
    \code{scriptscriptstyle(x)} \tab draw x in very small size \cr
31430 ripley 140
    \code{underline(x)}   \tab draw x underlined\cr
27442 ripley 141
    \code{x ~~ y}        \tab put extra space between x and y \cr
142
    \code{x + phantom(0) + y} \tab leave gap for "0", but don't draw it \cr
143
    \code{x + over(1, phantom(0))} \tab leave vertical gap for "0" (don't draw) \cr
144
    \code{frac(x, y)}   \tab x over y \cr
145
    \code{over(x, y)}   \tab x over y \cr
146
    \code{atop(x, y)}   \tab x over y (no horizontal bar) \cr
147
    \code{sum(x[i], i==1, n)} \tab sum x[i] for i equals 1 to n \cr
148
    \code{prod(plain(P)(X==x), x)} \tab product of P(X=x) for all values of x \cr
149
    \code{integral(f(x)*dx, a, b)} \tab definite integral of f(x) wrt x \cr
150
    \code{union(A[i], i==1, n)} \tab union of A[i] for i equals 1 to n \cr
151
    \code{intersect(A[i], i==1, n)} \tab intersection of A[i] \cr
152
    \code{lim(f(x), x \%->\% 0)} \tab limit of f(x) as x tends to 0 \cr
153
    \code{min(g(x), x > 0)} \tab minimum of g(x) for x greater than 0 \cr
154
    \code{inf(S)}        \tab infimum of S \cr
155
    \code{sup(S)}   \tab supremum of S \cr
156
    \code{x^y + z}   \tab normal operator precedence \cr
157
    \code{x^(y + z)}    \tab visible grouping of operands \cr
158
    \code{x^{y + z}}  \tab invisible grouping of operands \cr
159
    \code{group("(",list(a, b),"]")} \tab specify left and right delimiters \cr
160
    \code{bgroup("(",atop(x,y),")")} \tab use scalable delimiters \cr
161
    \code{group(lceil, x, rceil)} \tab special delimiters \cr
67502 ripley 162
    \code{group(lfloor, x, rfloor)} \tab special delimiters \cr
78742 murrell 163
    \code{group(langle, list(x, y), rangle)} \tab special delimiters \cr
27442 ripley 164
  }
75103 maechler 165
 
90027 hornik 166
  The supported \sQuote{scalable delimiters} are \samp{| ( [ \{}
78742 murrell 167
  and their right-hand versions.
67502 ripley 168
  \code{"."} is equivalent to \code{""}: the corresponding delimiter
67628 ripley 169
  will be omitted. Delimiter \code{||} is supported but has the same
170
  effect as \code{|}.
78742 murrell 171
  The special delimiters \code{lceil}, \code{lfloor}, \code{langle}
172
  (and their right-hand versions) are not scalable.
75103 maechler 173
 
82809 ripley 174
  Note that \code{paste} does not insert spaces when juxtaposing, unlike
175
  (by default) the \R function of that name.
82890 maechler 176
 
43133 ripley 177
  The symbol font uses Adobe Symbol encoding so, for example, a lower
178
  case mu can be obtained either by the special symbol \code{mu} or by
179
  \code{symbol("m")}.  This provides access to symbols that have no
85925 hornik 180
  special symbol name, for example, the universal, or \I{forall}, symbol is
51831 ripley 181
  \code{symbol("\\042")}.  To see what symbols are available in this way
182
  use \code{TestChars(font=5)} as given in the examples for
183
  \code{\link{points}}: some are only available on some devices.
42633 murrell 184
 
49626 hornik 185
  Note to TeX users: TeX's \samp{\\Upsilon} is \code{Upsilon1}, TeX's
46957 ripley 186
  \samp{\\varepsilon} is close to \code{epsilon}, and there is no
187
  equivalent of TeX's \samp{\\epsilon}.  TeX's \samp{\\varpi} is close to
36211 ripley 188
  \code{omega1}.  \code{vartheta}, \code{varphi} and \code{varsigma} are
189
  allowed as synonyms for \code{theta1}, \code{phi1} and \code{sigma1}.
36214 ripley 190
 
191
  \code{sigma1} is also known as \code{stigma}, its Unicode name.
38797 ripley 192
 
66444 hornik 193
  Control characters (e.g., \samp{\\n}) are not interpreted in character
85927 hornik 194
  strings in \I{plotmath}, unlike normal plotting.
46107 ripley 195
 
196
  The fonts used are taken from the current font family, and so can be
197
  set by \code{\link{par}(family=)} in base graphics, and
198
  \code{\link{gpar}(fontfamily=)} in package \pkg{grid}.
51385 ripley 199
 
200
  Note that \code{bold}, \code{italic} and \code{bolditalic} do not
201
  apply to symbols, and hence not to the Greek \emph{symbols} such as
52306 ripley 202
  \code{mu} which are displayed in the symbol font.  They also do not
203
  apply to numeric constants.
27442 ripley 204
}
46933 ripley 205
\section{Other symbols}{
51385 ripley 206
  On many OSes and some graphics devices many other symbols are
207
  available as part of the standard text font, and all of the symbols in
208
  the Adobe Symbol encoding are in principle available \emph{via}
85927 hornik 209
  changing the font face or (see \sQuote{Details}) \I{plotmath}: see the
51385 ripley 210
  examples section of \code{\link{points}} for a function to display
211
  them.  (\sQuote{In principle} because some of the glyphs are missing
212
  from some implementations of the symbol font.)  Unfortunately,
85390 ripley 213
  \code{\link{pdf}} and \code{\link{postscript}} have support for little
51385 ripley 214
  more than European (not Greek) and CJK characters and the Adobe Symbol
215
  encoding (and in a few fonts, also Cyrillic characters).
46933 ripley 216
 
75103 maechler 217
  \describe{
218
    \item{On Unix-alikes:}{
46933 ripley 219
  In a UTF-8 locale any Unicode character can be entered, perhaps as a
49626 hornik 220
  \samp{\\uxxxx} or \samp{\\Uxxxxxxxx} escape sequence, but the issue is
46960 ripley 221
  whether the graphics device is able to display the character.  The
46933 ripley 222
  widest range of characters is likely to be available in the
85953 hornik 223
  \code{\link{X11}} device using \I{cairo}: see its help page for how
51385 ripley 224
  installing additional fonts can help.  This can often be used to
225
  display Greek \emph{letters} in bold or italic.
46933 ripley 226
 
85390 ripley 227
  On macOS the \code{\link{quartz}} device and the default system fonts
228
  have quite large coverage.
229
 
46933 ripley 230
  In non-UTF-8 locales there is normally no support for symbols not in
231
  the languages for which the current encoding was intended.
75103 maechler 232
    }
233
    \item{On Windows:}{
46933 ripley 234
  Any Unicode character can be entered into a text string \emph{via} a
49626 hornik 235
  \samp{\\uxxxx} escape, or used by number in a call to
46933 ripley 236
  \code{\link{points}}.  The \code{\link{windows}} family of devices can
51385 ripley 237
  display such characters if they are available in the font in use.
238
  This can often be used to display Greek \emph{letters} in bold or italic.
61433 ripley 239
 
51385 ripley 240
  A good way to both find out which characters are available in a font
241
  and to determine the Unicode number is to use the \sQuote{Character
242
  Map} accessory (usually on the \sQuote{Start} menu under
46960 ripley 243
  \sQuote{Accessories->System Tools}).  You can also copy-and-paste
51385 ripley 244
  characters from the \sQuote{Character Map} window to the \code{Rgui}
245
  console (but not to \code{Rterm}).
75103 maechler 246
    }
247
 }
46933 ripley 248
}
27442 ripley 249
\references{
88593 hornik 250
  \bibshow{R:Murrell+Ihaka:2000}
43114 ripley 251
 
80459 murrell 252
  A list of
253
  the symbol codes can be found in decimal, octal and hex at
79309 hornik 254
  \url{https://www.stat.auckland.ac.nz/~paul/R/CM/AdobeSym.html}.
27442 ripley 255
}
256
\seealso{
257
  \code{demo(plotmath)},
258
  \code{\link{axis}},
259
  \code{\link{mtext}},
260
  \code{\link{text}},
261
  \code{\link{title}},
262
  \code{\link{substitute}}
263
  \code{\link{quote}}, \code{\link{bquote}}
264
}
265
\examples{
41508 ripley 266
require(graphics)
267
 
80079 hornik 268
x <- seq(-4, 4, length.out = 101)
27442 ripley 269
y <- cbind(sin(x), cos(x))
270
matplot(x, y, type = "l", xaxt = "n",
271
        main = expression(paste(plain(sin) * phi, "  and  ",
272
                                plain(cos) * phi)),
273
        ylab = expression("sin" * phi, "cos" * phi), # only 1st is taken
274
        xlab = expression(paste("Phase Angle ", phi)),
275
        col.main = "blue")
276
axis(1, at = c(-pi, -pi/2, 0, pi/2, pi),
36154 ripley 277
     labels = expression(-pi, -pi/2, 0, pi/2, pi))
27442 ripley 278
 
279
 
280
## How to combine "math" and numeric variables :
281
plot(1:10, type="n", xlab="", ylab="", main = "plot math & numbers")
48951 maechler 282
theta <- 1.23 ; mtext(bquote(hat(theta) == .(theta)), line= .25)
27442 ripley 283
for(i in 2:9)
61168 ripley 284
    text(i, i+1, substitute(list(xi, eta) == group("(",list(x,y),")"),
285
                            list(x = i, y = i+1)))
38520 ripley 286
## note that both of these use calls rather than expressions.
48951 maechler 287
##
61168 ripley 288
text(1, 10,  "Derivatives:", adj = 0)
289
text(1, 9.6, expression(
290
 "             first: {f * minute}(x) " == {f * minute}(x)), adj = 0)
291
text(1, 9.0, expression(
292
 "     second: {f * second}(x) "        == {f * second}(x)), adj = 0)
27442 ripley 293
 
48951 maechler 294
 
82668 maechler 295
## note the "{ .. }" trick to get "chained" equations:
296
plot(1:10, 1:10, main = quote(1 <= {1 < 2}))
27442 ripley 297
text(4, 9, expression(hat(beta) == (X^t * X)^{-1} * X^t * y))
298
text(4, 8.4, "expression(hat(beta) == (X^t * X)^{-1} * X^t * y)",
299
     cex = .8)
300
text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n)))
301
text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))",
302
     cex = .8)
303
text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ",
304
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})),
305
     cex = 1.2)
45967 ripley 306
 
307
## some other useful symbols
308
plot.new(); plot.window(c(0,4), c(15,1))
61168 ripley 309
text(1, 1, "universal", adj = 0); text(2.5, 1,  "\\\\042")
45967 ripley 310
text(3, 1, expression(symbol("\\042")))
61168 ripley 311
text(1, 2, "existential", adj = 0); text(2.5, 2,  "\\\\044")
45967 ripley 312
text(3, 2, expression(symbol("\\044")))
61168 ripley 313
text(1, 3, "suchthat", adj = 0); text(2.5, 3,  "\\\\047")
45967 ripley 314
text(3, 3, expression(symbol("\\047")))
61168 ripley 315
text(1, 4, "therefore", adj = 0); text(2.5, 4,  "\\\\134")
45967 ripley 316
text(3, 4, expression(symbol("\\134")))
61168 ripley 317
text(1, 5, "perpendicular", adj = 0); text(2.5, 5,  "\\\\136")
45967 ripley 318
text(3, 5, expression(symbol("\\136")))
61168 ripley 319
text(1, 6, "circlemultiply", adj = 0); text(2.5, 6,  "\\\\304")
45967 ripley 320
text(3, 6, expression(symbol("\\304")))
61168 ripley 321
text(1, 7, "circleplus", adj = 0); text(2.5, 7,  "\\\\305")
45967 ripley 322
text(3, 7, expression(symbol("\\305")))
61168 ripley 323
text(1, 8, "emptyset", adj = 0); text(2.5, 8,  "\\\\306")
45967 ripley 324
text(3, 8, expression(symbol("\\306")))
61168 ripley 325
text(1, 9, "angle", adj = 0); text(2.5, 9,  "\\\\320")
45967 ripley 326
text(3, 9, expression(symbol("\\320")))
61168 ripley 327
text(1, 10, "leftangle", adj = 0); text(2.5, 10,  "\\\\341")
45967 ripley 328
text(3, 10, expression(symbol("\\341")))
61168 ripley 329
text(1, 11, "rightangle", adj = 0); text(2.5, 11,  "\\\\361")
45967 ripley 330
text(3, 11, expression(symbol("\\361")))
27442 ripley 331
}
332
\keyword{aplot}