The R Project SVN R

Rev

Rev 75103 | Rev 79309 | 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
68948 ripley 2
% Part of the R package, https://www.R-project.org
75965 maechler 3
% Copyright 1995-2019 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
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
95
    \code{bolditalic(x)} \tab draw x in bolditalic 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
112
    \code{bar(xy)}   \tab xy with bar \cr
113
    \code{widehat(xy)}   \tab xy with a wide circumflex \cr
114
    \code{widetilde(xy)} \tab xy with a wide tilde \cr
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
27442 ripley 163
  }
75103 maechler 164
 
67502 ripley 165
  The supported \sQuote{scalable delimiters} are \code{| ( [ \{},
166
  \code{lceil}, \code{lfloor} and their right-hand versions.
167
  \code{"."} is equivalent to \code{""}: the corresponding delimiter
67628 ripley 168
  will be omitted. Delimiter \code{||} is supported but has the same
169
  effect as \code{|}.
75103 maechler 170
 
43133 ripley 171
  The symbol font uses Adobe Symbol encoding so, for example, a lower
172
  case mu can be obtained either by the special symbol \code{mu} or by
173
  \code{symbol("m")}.  This provides access to symbols that have no
174
  special symbol name, for example, the universal, or forall, symbol is
51831 ripley 175
  \code{symbol("\\042")}.  To see what symbols are available in this way
176
  use \code{TestChars(font=5)} as given in the examples for
177
  \code{\link{points}}: some are only available on some devices.
42633 murrell 178
 
49626 hornik 179
  Note to TeX users: TeX's \samp{\\Upsilon} is \code{Upsilon1}, TeX's
46957 ripley 180
  \samp{\\varepsilon} is close to \code{epsilon}, and there is no
181
  equivalent of TeX's \samp{\\epsilon}.  TeX's \samp{\\varpi} is close to
36211 ripley 182
  \code{omega1}.  \code{vartheta}, \code{varphi} and \code{varsigma} are
183
  allowed as synonyms for \code{theta1}, \code{phi1} and \code{sigma1}.
36214 ripley 184
 
185
  \code{sigma1} is also known as \code{stigma}, its Unicode name.
38797 ripley 186
 
66444 hornik 187
  Control characters (e.g., \samp{\\n}) are not interpreted in character
38797 ripley 188
  strings in plotmath, unlike normal plotting.
46107 ripley 189
 
190
  The fonts used are taken from the current font family, and so can be
191
  set by \code{\link{par}(family=)} in base graphics, and
192
  \code{\link{gpar}(fontfamily=)} in package \pkg{grid}.
51385 ripley 193
 
194
  Note that \code{bold}, \code{italic} and \code{bolditalic} do not
195
  apply to symbols, and hence not to the Greek \emph{symbols} such as
52306 ripley 196
  \code{mu} which are displayed in the symbol font.  They also do not
197
  apply to numeric constants.
27442 ripley 198
}
46933 ripley 199
\section{Other symbols}{
51385 ripley 200
  On many OSes and some graphics devices many other symbols are
201
  available as part of the standard text font, and all of the symbols in
202
  the Adobe Symbol encoding are in principle available \emph{via}
203
  changing the font face or (see \sQuote{Details}) plotmath: see the
204
  examples section of \code{\link{points}} for a function to display
205
  them.  (\sQuote{In principle} because some of the glyphs are missing
206
  from some implementations of the symbol font.)  Unfortunately,
46933 ripley 207
  \code{\link{postscript}} and \code{\link{pdf}} have support for little
51385 ripley 208
  more than European (not Greek) and CJK characters and the Adobe Symbol
209
  encoding (and in a few fonts, also Cyrillic characters).
46933 ripley 210
 
75103 maechler 211
  \describe{
212
    \item{On Unix-alikes:}{
46933 ripley 213
  In a UTF-8 locale any Unicode character can be entered, perhaps as a
49626 hornik 214
  \samp{\\uxxxx} or \samp{\\Uxxxxxxxx} escape sequence, but the issue is
46960 ripley 215
  whether the graphics device is able to display the character.  The
46933 ripley 216
  widest range of characters is likely to be available in the
217
  \code{\link{X11}} device using cairo: see its help page for how
51385 ripley 218
  installing additional fonts can help.  This can often be used to
219
  display Greek \emph{letters} in bold or italic.
46933 ripley 220
 
221
  In non-UTF-8 locales there is normally no support for symbols not in
222
  the languages for which the current encoding was intended.
75103 maechler 223
    }
224
    \item{On Windows:}{
46933 ripley 225
  Any Unicode character can be entered into a text string \emph{via} a
49626 hornik 226
  \samp{\\uxxxx} escape, or used by number in a call to
46933 ripley 227
  \code{\link{points}}.  The \code{\link{windows}} family of devices can
51385 ripley 228
  display such characters if they are available in the font in use.
229
  This can often be used to display Greek \emph{letters} in bold or italic.
61433 ripley 230
 
51385 ripley 231
  A good way to both find out which characters are available in a font
232
  and to determine the Unicode number is to use the \sQuote{Character
233
  Map} accessory (usually on the \sQuote{Start} menu under
46960 ripley 234
  \sQuote{Accessories->System Tools}).  You can also copy-and-paste
51385 ripley 235
  characters from the \sQuote{Character Map} window to the \code{Rgui}
236
  console (but not to \code{Rterm}).
75103 maechler 237
    }
238
 }
46933 ripley 239
}
27442 ripley 240
\references{
74265 hornik 241
  Murrell, P. and Ihaka, R. (2000).
242
  An approach to providing mathematical annotation in plots.
27442 ripley 243
  \emph{Journal of Computational and Graphical Statistics},
244
  \bold{9}, 582--599.
74265 hornik 245
  \doi{10.2307/1390947}.
43114 ripley 246
 
247
  The symbol codes can be found in octal in the Adobe reference manuals,
66444 hornik 248
  e.g.\sspace{}for Postscript
75965 maechler 249
  % \url{https://www.adobe.com/products/postscript/pdfs/PLRM.pdf} now points to
250
  \url{https://www.adobe.com/content/dam/acom/en/devnet/actionscript/articles/PLRM.pdf}
43114 ripley 251
  or PDF
68939 ripley 252
  \url{https://www.adobe.com/devnet/acrobat/pdfs/pdf_reference_1-7.pdf}
43127 ripley 253
  and in decimal, octal and hex at
43114 ripley 254
  \url{http://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
 
27442 ripley 268
x <- seq(-4, 4, len = 101)
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
 
27442 ripley 295
plot(1:10, 1:10)
296
text(4, 9, expression(hat(beta) == (X^t * X)^{-1} * X^t * y))
297
text(4, 8.4, "expression(hat(beta) == (X^t * X)^{-1} * X^t * y)",
298
     cex = .8)
299
text(4, 7, expression(bar(x) == sum(frac(x[i], n), i==1, n)))
300
text(4, 6.4, "expression(bar(x) == sum(frac(x[i], n), i==1, n))",
301
     cex = .8)
302
text(8, 5, expression(paste(frac(1, sigma*sqrt(2*pi)), " ",
303
                            plain(e)^{frac(-(x-mu)^2, 2*sigma^2)})),
304
     cex = 1.2)
45967 ripley 305
 
306
## some other useful symbols
307
plot.new(); plot.window(c(0,4), c(15,1))
61168 ripley 308
text(1, 1, "universal", adj = 0); text(2.5, 1,  "\\\\042")
45967 ripley 309
text(3, 1, expression(symbol("\\042")))
61168 ripley 310
text(1, 2, "existential", adj = 0); text(2.5, 2,  "\\\\044")
45967 ripley 311
text(3, 2, expression(symbol("\\044")))
61168 ripley 312
text(1, 3, "suchthat", adj = 0); text(2.5, 3,  "\\\\047")
45967 ripley 313
text(3, 3, expression(symbol("\\047")))
61168 ripley 314
text(1, 4, "therefore", adj = 0); text(2.5, 4,  "\\\\134")
45967 ripley 315
text(3, 4, expression(symbol("\\134")))
61168 ripley 316
text(1, 5, "perpendicular", adj = 0); text(2.5, 5,  "\\\\136")
45967 ripley 317
text(3, 5, expression(symbol("\\136")))
61168 ripley 318
text(1, 6, "circlemultiply", adj = 0); text(2.5, 6,  "\\\\304")
45967 ripley 319
text(3, 6, expression(symbol("\\304")))
61168 ripley 320
text(1, 7, "circleplus", adj = 0); text(2.5, 7,  "\\\\305")
45967 ripley 321
text(3, 7, expression(symbol("\\305")))
61168 ripley 322
text(1, 8, "emptyset", adj = 0); text(2.5, 8,  "\\\\306")
45967 ripley 323
text(3, 8, expression(symbol("\\306")))
61168 ripley 324
text(1, 9, "angle", adj = 0); text(2.5, 9,  "\\\\320")
45967 ripley 325
text(3, 9, expression(symbol("\\320")))
61168 ripley 326
text(1, 10, "leftangle", adj = 0); text(2.5, 10,  "\\\\341")
45967 ripley 327
text(3, 10, expression(symbol("\\341")))
61168 ripley 328
text(1, 11, "rightangle", adj = 0); text(2.5, 11,  "\\\\361")
45967 ripley 329
text(3, 11, expression(symbol("\\361")))
27442 ripley 330
}
331
\keyword{aplot}