| 60126 |
ripley |
1 |
# Copyright (C) 2002-2009 The R Core Team
|
|
|
2 |
|
| 37961 |
maechler |
3 |
require(datasets)
|
| 41513 |
ripley |
4 |
require(grDevices); require(graphics)
|
| 27442 |
ripley |
5 |
|
|
|
6 |
## --- "math annotation" in plots :
|
|
|
7 |
|
|
|
8 |
######
|
|
|
9 |
# create tables of mathematical annotation functionality
|
|
|
10 |
######
|
|
|
11 |
make.table <- function(nr, nc) {
|
|
|
12 |
savepar <- par(mar=rep(0, 4), pty="s")
|
|
|
13 |
plot(c(0, nc*2 + 1), c(0, -(nr + 1)),
|
|
|
14 |
type="n", xlab="", ylab="", axes=FALSE)
|
|
|
15 |
savepar
|
|
|
16 |
}
|
|
|
17 |
|
|
|
18 |
get.r <- function(i, nr) {
|
|
|
19 |
i %% nr + 1
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
get.c <- function(i, nr) {
|
|
|
23 |
i %/% nr + 1
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
draw.title.cell <- function(title, i, nr) {
|
|
|
27 |
r <- get.r(i, nr)
|
|
|
28 |
c <- get.c(i, nr)
|
|
|
29 |
text(2*c - .5, -r, title)
|
|
|
30 |
rect((2*(c - 1) + .5), -(r - .5), (2*c + .5), -(r + .5))
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
draw.plotmath.cell <- function(expr, i, nr, string = NULL) {
|
|
|
34 |
r <- get.r(i, nr)
|
|
|
35 |
c <- get.c(i, nr)
|
|
|
36 |
if (is.null(string)) {
|
|
|
37 |
string <- deparse(expr)
|
|
|
38 |
string <- substr(string, 12, nchar(string) - 1)
|
|
|
39 |
}
|
|
|
40 |
text((2*(c - 1) + 1), -r, string, col="grey")
|
|
|
41 |
text((2*c), -r, expr, adj=c(.5,.5))
|
|
|
42 |
rect((2*(c - 1) + .5), -(r - .5), (2*c + .5), -(r + .5), border="grey")
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
nr <- 20
|
|
|
46 |
nc <- 2
|
|
|
47 |
oldpar <- make.table(nr, nc)
|
|
|
48 |
i <- 0
|
|
|
49 |
draw.title.cell("Arithmetic Operators", i, nr); i <- i + 1
|
|
|
50 |
draw.plotmath.cell(expression(x + y), i, nr); i <- i + 1
|
|
|
51 |
draw.plotmath.cell(expression(x - y), i, nr); i <- i + 1
|
|
|
52 |
draw.plotmath.cell(expression(x * y), i, nr); i <- i + 1
|
|
|
53 |
draw.plotmath.cell(expression(x / y), i, nr); i <- i + 1
|
|
|
54 |
draw.plotmath.cell(expression(x %+-% y), i, nr); i <- i + 1
|
|
|
55 |
draw.plotmath.cell(expression(x %/% y), i, nr); i <- i + 1
|
|
|
56 |
draw.plotmath.cell(expression(x %*% y), i, nr); i <- i + 1
|
| 38916 |
murrell |
57 |
draw.plotmath.cell(expression(x %.% y), i, nr); i <- i + 1
|
| 27442 |
ripley |
58 |
draw.plotmath.cell(expression(-x), i, nr); i <- i + 1
|
|
|
59 |
draw.plotmath.cell(expression(+x), i, nr); i <- i + 1
|
|
|
60 |
draw.title.cell("Sub/Superscripts", i, nr); i <- i + 1
|
|
|
61 |
draw.plotmath.cell(expression(x[i]), i, nr); i <- i + 1
|
|
|
62 |
draw.plotmath.cell(expression(x^2), i, nr); i <- i + 1
|
|
|
63 |
draw.title.cell("Juxtaposition", i, nr); i <- i + 1
|
|
|
64 |
draw.plotmath.cell(expression(x * y), i, nr); i <- i + 1
|
|
|
65 |
draw.plotmath.cell(expression(paste(x, y, z)), i, nr); i <- i + 1
|
|
|
66 |
draw.title.cell("Radicals", i, nr); i <- i + 1
|
|
|
67 |
draw.plotmath.cell(expression(sqrt(x)), i, nr); i <- i + 1
|
|
|
68 |
draw.plotmath.cell(expression(sqrt(x, y)), i, nr); i <- i + 1
|
| 60811 |
murrell |
69 |
draw.title.cell("Lists", i, nr); i <- i + 1
|
|
|
70 |
draw.plotmath.cell(expression(list(x, y, z)), i, nr); i <- i + 1
|
| 27442 |
ripley |
71 |
draw.title.cell("Relations", i, nr); i <- i + 1
|
|
|
72 |
draw.plotmath.cell(expression(x == y), i, nr); i <- i + 1
|
|
|
73 |
draw.plotmath.cell(expression(x != y), i, nr); i <- i + 1
|
|
|
74 |
draw.plotmath.cell(expression(x < y), i, nr); i <- i + 1
|
|
|
75 |
draw.plotmath.cell(expression(x <= y), i, nr); i <- i + 1
|
|
|
76 |
draw.plotmath.cell(expression(x > y), i, nr); i <- i + 1
|
|
|
77 |
draw.plotmath.cell(expression(x >= y), i, nr); i <- i + 1
|
|
|
78 |
draw.plotmath.cell(expression(x %~~% y), i, nr); i <- i + 1
|
|
|
79 |
draw.plotmath.cell(expression(x %=~% y), i, nr); i <- i + 1
|
|
|
80 |
draw.plotmath.cell(expression(x %==% y), i, nr); i <- i + 1
|
|
|
81 |
draw.plotmath.cell(expression(x %prop% y), i, nr); i <- i + 1
|
| 60811 |
murrell |
82 |
draw.plotmath.cell(expression(x %~% y), i, nr); i <- i + 1
|
| 27442 |
ripley |
83 |
draw.title.cell("Typeface", i, nr); i <- i + 1
|
|
|
84 |
draw.plotmath.cell(expression(plain(x)), i, nr); i <- i + 1
|
|
|
85 |
draw.plotmath.cell(expression(italic(x)), i, nr); i <- i + 1
|
|
|
86 |
draw.plotmath.cell(expression(bold(x)), i, nr); i <- i + 1
|
|
|
87 |
draw.plotmath.cell(expression(bolditalic(x)), i, nr); i <- i + 1
|
| 31463 |
maechler |
88 |
draw.plotmath.cell(expression(underline(x)), i, nr); i <- i + 1
|
| 27442 |
ripley |
89 |
|
|
|
90 |
# Need fewer, wider columns for ellipsis ...
|
|
|
91 |
nr <- 20
|
|
|
92 |
nc <- 2
|
|
|
93 |
make.table(nr, nc)
|
|
|
94 |
i <- 0
|
|
|
95 |
draw.title.cell("Ellipsis", i, nr); i <- i + 1
|
|
|
96 |
draw.plotmath.cell(expression(list(x[1], ..., x[n])), i, nr); i <- i + 1
|
|
|
97 |
draw.plotmath.cell(expression(x[1] + ... + x[n]), i, nr); i <- i + 1
|
|
|
98 |
draw.plotmath.cell(expression(list(x[1], cdots, x[n])), i, nr); i <- i + 1
|
|
|
99 |
draw.plotmath.cell(expression(x[1] + ldots + x[n]), i, nr); i <- i + 1
|
|
|
100 |
draw.title.cell("Set Relations", i, nr); i <- i + 1
|
|
|
101 |
draw.plotmath.cell(expression(x %subset% y), i, nr); i <- i + 1
|
|
|
102 |
draw.plotmath.cell(expression(x %subseteq% y), i, nr); i <- i + 1
|
|
|
103 |
draw.plotmath.cell(expression(x %supset% y), i, nr); i <- i + 1
|
|
|
104 |
draw.plotmath.cell(expression(x %supseteq% y), i, nr); i <- i + 1
|
|
|
105 |
draw.plotmath.cell(expression(x %notsubset% y), i, nr); i <- i + 1
|
|
|
106 |
draw.plotmath.cell(expression(x %in% y), i, nr); i <- i + 1
|
|
|
107 |
draw.plotmath.cell(expression(x %notin% y), i, nr); i <- i + 1
|
|
|
108 |
draw.title.cell("Accents", i, nr); i <- i + 1
|
|
|
109 |
draw.plotmath.cell(expression(hat(x)), i, nr); i <- i + 1
|
|
|
110 |
draw.plotmath.cell(expression(tilde(x)), i, nr); i <- i + 1
|
|
|
111 |
draw.plotmath.cell(expression(ring(x)), i, nr); i <- i + 1
|
|
|
112 |
draw.plotmath.cell(expression(bar(xy)), i, nr); i <- i + 1
|
|
|
113 |
draw.plotmath.cell(expression(widehat(xy)), i, nr); i <- i + 1
|
|
|
114 |
draw.plotmath.cell(expression(widetilde(xy)), i, nr); i <- i + 1
|
|
|
115 |
draw.title.cell("Arrows", i, nr); i <- i + 1
|
|
|
116 |
draw.plotmath.cell(expression(x %<->% y), i, nr); i <- i + 1
|
|
|
117 |
draw.plotmath.cell(expression(x %->% y), i, nr); i <- i + 1
|
|
|
118 |
draw.plotmath.cell(expression(x %<-% y), i, nr); i <- i + 1
|
|
|
119 |
draw.plotmath.cell(expression(x %up% y), i, nr); i <- i + 1
|
|
|
120 |
draw.plotmath.cell(expression(x %down% y), 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 %dblup% y), i, nr); i <- i + 1
|
|
|
125 |
draw.plotmath.cell(expression(x %dbldown% y), i, nr); i <- i + 1
|
|
|
126 |
draw.title.cell("Symbolic Names", i, nr); i <- i + 1
|
|
|
127 |
draw.plotmath.cell(expression(Alpha - Omega), i, nr); i <- i + 1
|
|
|
128 |
draw.plotmath.cell(expression(alpha - omega), i, nr); i <- i + 1
|
| 36212 |
ripley |
129 |
draw.plotmath.cell(expression(phi1 + sigma1), i, nr); i <- i + 1
|
|
|
130 |
draw.plotmath.cell(expression(Upsilon1), i, nr); i <- i + 1
|
| 27442 |
ripley |
131 |
draw.plotmath.cell(expression(infinity), i, nr); i <- i + 1
|
|
|
132 |
draw.plotmath.cell(expression(32 * degree), i, nr); i <- i + 1
|
|
|
133 |
draw.plotmath.cell(expression(60 * minute), i, nr); i <- i + 1
|
|
|
134 |
draw.plotmath.cell(expression(30 * second), i, nr); i <- i + 1
|
|
|
135 |
|
|
|
136 |
# Need even fewer, wider columns for typeface and style ...
|
|
|
137 |
nr <- 20
|
|
|
138 |
nc <- 1
|
|
|
139 |
make.table(nr, nc)
|
|
|
140 |
i <- 0
|
|
|
141 |
draw.title.cell("Style", i, nr); i <- i + 1
|
|
|
142 |
draw.plotmath.cell(expression(displaystyle(x)), i, nr); i <- i + 1
|
|
|
143 |
draw.plotmath.cell(expression(textstyle(x)), i, nr); i <- i + 1
|
|
|
144 |
draw.plotmath.cell(expression(scriptstyle(x)), i, nr); i <- i + 1
|
|
|
145 |
draw.plotmath.cell(expression(scriptscriptstyle(x)), i, nr); i <- i + 1
|
|
|
146 |
draw.title.cell("Spacing", i, nr); i <- i + 1
|
|
|
147 |
draw.plotmath.cell(expression(x ~~ y), i, nr); i <- i + 1
|
|
|
148 |
|
|
|
149 |
# Need fewer, taller rows for fractions ...
|
|
|
150 |
# cheat a bit to save pages
|
|
|
151 |
par(new = TRUE)
|
|
|
152 |
nr <- 10
|
|
|
153 |
nc <- 1
|
|
|
154 |
make.table(nr, nc)
|
|
|
155 |
i <- 4
|
|
|
156 |
draw.plotmath.cell(expression(x + phantom(0) + y), i, nr); i <- i + 1
|
|
|
157 |
draw.plotmath.cell(expression(x + over(1, phantom(0))), i, nr); i <- i + 1
|
|
|
158 |
draw.title.cell("Fractions", i, nr); i <- i + 1
|
|
|
159 |
draw.plotmath.cell(expression(frac(x, y)), i, nr); i <- i + 1
|
|
|
160 |
draw.plotmath.cell(expression(over(x, y)), i, nr); i <- i + 1
|
|
|
161 |
draw.plotmath.cell(expression(atop(x, y)), i, nr); i <- i + 1
|
|
|
162 |
|
|
|
163 |
# Need fewer, taller rows and fewer, wider columns for big operators ...
|
|
|
164 |
nr <- 10
|
|
|
165 |
nc <- 1
|
|
|
166 |
make.table(nr, nc)
|
|
|
167 |
i <- 0
|
|
|
168 |
draw.title.cell("Big Operators", i, nr); i <- i + 1
|
|
|
169 |
draw.plotmath.cell(expression(sum(x[i], i=1, n)), i, nr); i <- i + 1
|
|
|
170 |
draw.plotmath.cell(expression(prod(plain(P)(X == x), x)), i, nr); i <- i + 1
|
|
|
171 |
draw.plotmath.cell(expression(integral(f(x) * dx, a, b)), i, nr); i <- i + 1
|
|
|
172 |
draw.plotmath.cell(expression(union(A[i], i==1, n)), i, nr); i <- i + 1
|
|
|
173 |
draw.plotmath.cell(expression(intersect(A[i], i==1, n)), i, nr); i <- i + 1
|
|
|
174 |
draw.plotmath.cell(expression(lim(f(x), x %->% 0)), i, nr); i <- i + 1
|
|
|
175 |
draw.plotmath.cell(expression(min(g(x), x >= 0)), i, nr); i <- i + 1
|
|
|
176 |
draw.plotmath.cell(expression(inf(S)), i, nr); i <- i + 1
|
|
|
177 |
draw.plotmath.cell(expression(sup(S)), i, nr); i <- i + 1
|
|
|
178 |
|
|
|
179 |
make.table(nr, nc)
|
|
|
180 |
i <- 0
|
|
|
181 |
draw.title.cell("Grouping", i, nr); i <- i + 1
|
|
|
182 |
draw.plotmath.cell(expression((x + y)*z), i, nr); i <- i + 1
|
|
|
183 |
draw.plotmath.cell(expression(x^y + z), i, nr); i <- i + 1
|
|
|
184 |
draw.plotmath.cell(expression(x^(y + z)), i, nr); i <- i + 1
|
|
|
185 |
# have to do this one by hand
|
|
|
186 |
draw.plotmath.cell(expression(x^{y + z}), i, nr, string="x^{y + z}"); i <- i + 1
|
|
|
187 |
draw.plotmath.cell(expression(group("(", list(a, b), "]")), i, nr); i <- i + 1
|
|
|
188 |
draw.plotmath.cell(expression(bgroup("(", atop(x, y), ")")), i, nr); i <- i + 1
|
|
|
189 |
draw.plotmath.cell(expression(group(lceil, x, rceil)), i, nr); i <- i + 1
|
|
|
190 |
draw.plotmath.cell(expression(group(lfloor, x, rfloor)), i, nr); i <- i + 1
|
|
|
191 |
draw.plotmath.cell(expression(group("|", x, "|")), i, nr); i <- i + 1
|
|
|
192 |
|
|
|
193 |
par(oldpar)
|