The R Project SVN R

Rev

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

Rev Author Line No. Line
56186 murdoch 1
 
63202 ripley 2
R Under development (unstable) (2013-07-06 r63201) -- "Unsuffered Consequences"
61787 ripley 3
Copyright (C) 2013 The R Foundation for Statistical Computing
61044 murrell 4
Platform: x86_64-unknown-linux-gnu (64-bit)
56186 murdoch 5
 
6
R is free software and comes with ABSOLUTELY NO WARRANTY.
7
You are welcome to redistribute it under certain conditions.
8
Type 'license()' or 'licence()' for distribution details.
9
 
10
  Natural language support but running in an English locale
11
 
12
R is a collaborative project with many contributors.
13
Type 'contributors()' for more information and
14
'citation()' on how to cite R or R packages in publications.
15
 
16
Type 'demo()' for some demos, 'help()' for on-line help, or
17
'help.start()' for an HTML browser interface to help.
18
Type 'q()' to quit R.
19
 
20
> pkgname <- "grid"
21
> source(file.path(R.home("share"), "R", "examples-header.R"))
22
> options(warn = 1)
23
> library('grid')
24
> 
61787 ripley 25
> base::assign(".oldSearch", base::search(), pos = 'CheckExEnv')
56186 murdoch 26
> cleanEx()
27
> nameEx("Grid")
28
> ### * Grid
29
> 
30
> flush(stderr()); flush(stdout())
31
> 
32
> ### Name: Grid
33
> ### Title: Grid Graphics
34
> ### Aliases: Grid
35
> ### Keywords: dplot
36
> 
37
> ### ** Examples
38
> 
61435 ripley 39
> ## Diagram of a simple layout
56186 murdoch 40
> grid.show.layout(grid.layout(4,2,
41
+                      heights=unit(rep(1, 4),
42
+                                   c("lines", "lines", "lines", "null")),
43
+                      widths=unit(c(1, 1), "inches")))
44
> ## Diagram of a sample viewport
45
> grid.show.viewport(viewport(x=0.6, y=0.6,
46
+                             w=unit(1, "inches"), h=unit(1, "inches")))
47
> ## A flash plotting example
48
> grid.multipanel(vp=viewport(0.5, 0.5, 0.8, 0.8))
49
> 
50
> 
51
> 
52
> cleanEx()
53
> nameEx("arrow")
54
> ### * arrow
55
> 
56
> flush(stderr()); flush(stdout())
57
> 
58
> ### Name: arrow
59
> ### Title: Describe arrows to add to a line.
60
> ### Aliases: arrow
61
> ### Keywords: dplot
62
> 
63
> ### ** Examples
64
> 
65
> arrow()
66
$angle
67
[1] 30
68
 
69
$length
70
[1] 0.25inches
71
 
72
$ends
73
[1] 2
74
 
75
$type
76
[1] 1
77
 
78
attr(,"class")
79
[1] "arrow"
80
> 
81
> 
82
> 
83
> cleanEx()
57578 ripley 84
> nameEx("calcStringMetric")
85
> ### * calcStringMetric
86
> 
87
> flush(stderr()); flush(stdout())
88
> 
89
> ### Name: calcStringMetric
90
> ### Title: Calculate Metric Information for Text
91
> ### Aliases: calcStringMetric
92
> ### Keywords: dplot
93
> 
94
> ### ** Examples
95
> 
96
> grid.newpage()
97
> grid.segments(.01, .5, .99, .5, gp=gpar(col="grey"))
98
> metrics <- calcStringMetric(letters)
99
> grid.rect(x=1:26/27,
100
+           width=unit(metrics$width, "inches"),
101
+           height=unit(metrics$ascent, "inches"),
102
+           just="bottom",
103
+           gp=gpar(col="red"))
104
> grid.rect(x=1:26/27,
105
+           width=unit(metrics$width, "inches"),
106
+           height=unit(metrics$descent, "inches"),
107
+           just="top",
108
+           gp=gpar(col="red"))
109
> grid.text(letters, x=1:26/27, just="bottom")
110
> 
111
> test <- function(x) {
112
+     grid.text(x, just="bottom")
113
+     metric <- calcStringMetric(x)
114
+     if (is.character(x)) {
115
+         grid.rect(width=unit(metric$width, "inches"),
116
+                   height=unit(metric$ascent, "inches"),
117
+                   just="bottom",
118
+                   gp=gpar(col=rgb(1,0,0,.5)))
119
+         grid.rect(width=unit(metric$width, "inches"),
120
+                   height=unit(metric$descent, "inches"),
121
+                   just="top",
122
+                   gp=gpar(col=rgb(1,0,0,.5)))
123
+     } else {
124
+         grid.rect(width=unit(metric$width, "inches"),
125
+                   y=unit(.5, "npc") + unit(metric[2], "inches"),
126
+                   height=unit(metric$ascent, "inches"),
127
+                   just="bottom",
128
+                   gp=gpar(col=rgb(1,0,0,.5)))
129
+         grid.rect(width=unit(metric$width, "inches"),
130
+                   height=unit(metric$descent, "inches"),
131
+                   just="bottom",
61435 ripley 132
+                   gp=gpar(col=rgb(1,0,0,.5)))
57578 ripley 133
+     }
134
+ }
135
> 
136
> tests <- list("t",
137
+               "test",
138
+               "testy",
139
+               "test\ntwo",
140
+               expression(x),
141
+               expression(y),
142
+               expression(x + y),
143
+               expression(a + b),
144
+               expression(atop(x + y, 2)))
145
> 
146
> grid.newpage()
147
> nrowcol <- n2mfrow(length(tests))
148
> pushViewport(viewport(layout=grid.layout(nrowcol[1], nrowcol[2]),
149
+                       gp=gpar(cex=5, lwd=.5)))
150
> for (i in 1:length(tests)) {
151
+     col <- (i - 1) %% nrowcol[2] + 1
152
+     row <- (i - 1) %/% nrowcol[2] + 1
153
+     pushViewport(viewport(layout.pos.row=row, layout.pos.col=col))
154
+     test(tests[[i]])
155
+     popViewport()
156
+ }
157
> 
158
> 
159
> 
160
> 
161
> cleanEx()
56186 murdoch 162
> nameEx("convertNative")
163
> ### * convertNative
164
> 
165
> flush(stderr()); flush(stdout())
166
> 
167
> ### Name: convertNative
168
> ### Title: Convert a Unit Object to Native units
169
> ### Aliases: convertNative
170
> ### Keywords: dplot
171
> 
172
> ### ** Examples
173
> 
174
> grid.newpage()
175
> pushViewport(viewport(width=unit(.5, "npc"),
176
+                        height=unit(.5, "npc")))
177
> grid.rect()
178
> w <- convertNative(unit(1, "inches"))
179
Warning: 'convertNative' is deprecated.
180
Use 'convertUnit' instead.
181
See help("Deprecated")
182
> h <- convertNative(unit(1, "inches"), "y")
183
Warning: 'convertNative' is deprecated.
184
Use 'convertUnit' instead.
185
See help("Deprecated")
186
> # This rectangle starts off life as 1in square, but if you
187
> # resize the device it will no longer be 1in square
188
> grid.rect(width=unit(w, "native"), height=unit(h, "native"),
189
+           gp=gpar(col="red"))
190
> popViewport(1)
191
> 
192
> # How to use grid.convert(), etc instead
193
> convertNative(unit(1, "inches")) ==
194
+   convertX(unit(1, "inches"), "native", valueOnly=TRUE)
195
Warning: 'convertNative' is deprecated.
196
Use 'convertUnit' instead.
197
See help("Deprecated")
198
[1] TRUE
199
> convertNative(unit(1, "inches"), "y", "dimension") ==
200
+   convertHeight(unit(1, "inches"), "native", valueOnly=TRUE)
201
Warning: 'convertNative' is deprecated.
202
Use 'convertUnit' instead.
203
See help("Deprecated")
204
[1] TRUE
205
> 
206
> 
207
> 
208
> cleanEx()
209
> nameEx("current.viewport")
210
> ### * current.viewport
211
> 
212
> flush(stderr()); flush(stdout())
213
> 
214
> ### Name: Querying the Viewport Tree
215
> ### Title: Get the Current Grid Viewport (Tree)
216
> ### Aliases: current.viewport current.vpTree current.vpPath
217
> ###   current.transform
218
> ### Keywords: dplot
219
> 
220
> ### ** Examples
221
> 
222
> grid.newpage()
223
> pushViewport(viewport(width=0.8, height=0.8, name="A"))
224
> pushViewport(viewport(x=0.1, width=0.3, height=0.6,
225
+   just="left", name="B"))
226
> upViewport(1)
227
> pushViewport(viewport(x=0.5, width=0.4, height=0.8,
228
+   just="left", name="C"))
229
> pushViewport(viewport(width=0.8, height=0.8, name="D"))
230
> current.vpPath()
57578 ripley 231
GRID.VP.67::A::C::D 
56186 murdoch 232
> upViewport(1)
233
> current.vpPath()
57578 ripley 234
GRID.VP.67::A::C 
56186 murdoch 235
> current.vpTree()
57578 ripley 236
viewport[ROOT]->(viewport[GRID.VP.67]->(viewport[A]->(viewport[B], viewport[C]->(viewport[D])))) 
56186 murdoch 237
> current.viewport()
238
viewport[C] 
239
> current.vpTree(all=FALSE)
240
viewport[C]->(viewport[D]) 
241
> popViewport(0)
242
> 
243
> 
244
> 
245
> cleanEx()
246
> nameEx("gEdit")
247
> ### * gEdit
248
> 
249
> flush(stderr()); flush(stdout())
250
> 
251
> ### Name: gEdit
252
> ### Title: Create and Apply Edit Objects
253
> ### Aliases: gEdit gEditList applyEdit applyEdits
254
> ### Keywords: dplot
255
> 
256
> ### ** Examples
257
> 
258
> grid.rect(gp=gpar(col="red"))
259
> # same thing, but more verbose
260
> grid.draw(applyEdit(rectGrob(), gEdit(gp=gpar(col="red"))))
261
> 
262
> 
263
> 
264
> cleanEx()
265
> nameEx("gPath")
266
> ### * gPath
267
> 
268
> flush(stderr()); flush(stdout())
269
> 
270
> ### Name: gPath
271
> ### Title: Concatenate Grob Names
272
> ### Aliases: gPath
273
> ### Keywords: dplot
274
> 
275
> ### ** Examples
276
> 
277
> gPath("g1", "g2")
278
g1::g2 
279
> 
280
> 
281
> 
282
> cleanEx()
283
> nameEx("getNames")
284
> ### * getNames
285
> 
286
> flush(stderr()); flush(stdout())
287
> 
288
> ### Name: getNames
289
> ### Title: List the names of grobs on the display list
290
> ### Aliases: getNames
291
> ### Keywords: dplot
292
> 
293
> ### ** Examples
294
> 
295
> grid.grill()
296
> getNames()
57578 ripley 297
[1] "GRID.text.168"     "GRID.rect.169"     "GRID.rect.170"    
298
[4] "GRID.segments.171" "GRID.segments.172"
56186 murdoch 299
> 
300
> 
301
> 
302
> cleanEx()
303
> nameEx("gpar")
304
> ### * gpar
305
> 
306
> flush(stderr()); flush(stdout())
307
> 
308
> ### Name: gpar
309
> ### Title: Handling Grid Graphical Parameters
310
> ### Aliases: gpar get.gpar
311
> ### Keywords: dplot
312
> 
313
> ### ** Examples
314
> 
315
> gp <- get.gpar()
316
> utils::str(gp)
317
List of 14
318
 $ fill      : chr "transparent"
319
 $ col       : chr "black"
320
 $ lty       : chr "solid"
321
 $ lwd       : num 1
322
 $ cex       : num 1
323
 $ fontsize  : num 12
324
 $ lineheight: num 1.2
325
 $ font      : int 1
326
 $ fontfamily: chr ""
327
 $ alpha     : num 1
328
 $ lineend   : chr "round"
329
 $ linejoin  : chr "round"
330
 $ linemitre : num 10
331
 $ lex       : num 1
332
 - attr(*, "class")= chr "gpar"
333
> ## These *do* nothing but produce a "gpar" object:
334
> gpar(col = "red")
335
$col
336
[1] "red"
337
 
338
> gpar(col = "blue", lty = "solid", lwd = 3, fontsize = 16)
339
$col
340
[1] "blue"
341
 
342
$lty
343
[1] "solid"
344
 
345
$lwd
346
[1] 3
347
 
348
$fontsize
349
[1] 16
350
 
351
> get.gpar(c("col", "lty"))
352
$col
353
[1] "black"
354
 
355
$lty
356
[1] "solid"
357
 
358
> grid.newpage()
359
> vp <- viewport(w = .8, h = .8, gp = gpar(col="blue"))
360
> grid.draw(gTree(children=gList(rectGrob(gp = gpar(col="red")),
361
+                      textGrob(paste("The rect is its own colour (red)",
362
+                                     "but this text is the colour",
363
+                                     "set by the gTree (green)",
364
+                                     sep = "\n"))),
365
+       gp = gpar(col="green"), vp = vp))
366
> grid.text("This text is the colour set by the viewport (blue)",
367
+           y = 1, just = c("center", "bottom"),
368
+           gp = gpar(fontsize=20), vp = vp)
369
> grid.newpage()
370
> ## example with multiple values for a parameter
371
> pushViewport(viewport())
372
> grid.points(1:10/11, 1:10/11, gp = gpar(col=1:10))
373
> popViewport()
374
> 
375
> 
376
> 
377
> cleanEx()
57725 murrell 378
> nameEx("grid.DLapply")
379
> ### * grid.DLapply
380
> 
381
> flush(stderr()); flush(stdout())
382
> 
383
> ### Name: grid.DLapply
384
> ### Title: Modify the Grid Display List
385
> ### Aliases: grid.DLapply
386
> ### Keywords: dplot
387
> 
388
> ### ** Examples
389
> 
390
> grid.newpage()
391
> grid.rect(width=.5, height=.5, gp=gpar(fill="grey"))
392
> grid.DLapply(function(x) { if (is.grob(x)) x$gp <- gpar(); x })
393
NULL
394
> grid.refresh()
395
> 
396
> 
397
> 
398
> cleanEx()
56186 murdoch 399
> nameEx("grid.arrows")
400
> ### * grid.arrows
401
> 
402
> flush(stderr()); flush(stdout())
403
> 
404
> ### Name: grid.arrows
405
> ### Title: Draw Arrows
406
> ### Aliases: grid.arrows arrowsGrob
407
> ### Keywords: dplot
408
> 
409
> ### ** Examples
410
> 
411
> ## Not run: 
412
> ##D ## to avoid lots of deprecation warnings
413
> ##D pushViewport(viewport(layout=grid.layout(2, 4)))
414
> ##D pushViewport(viewport(layout.pos.col=1,
415
> ##D                        layout.pos.row=1))
416
> ##D grid.rect(gp=gpar(col="grey"))
417
> ##D grid.arrows()
418
> ##D popViewport()
419
> ##D pushViewport(viewport(layout.pos.col=2,
420
> ##D                        layout.pos.row=1))
421
> ##D grid.rect(gp=gpar(col="grey"))
422
> ##D grid.arrows(angle=15, type="closed")
423
> ##D popViewport()
424
> ##D pushViewport(viewport(layout.pos.col=3,
425
> ##D                        layout.pos.row=1))
426
> ##D grid.rect(gp=gpar(col="grey"))
427
> ##D grid.arrows(angle=5, length=unit(0.1, "npc"),
428
> ##D             type="closed", gp=gpar(fill="white"))
429
> ##D popViewport()
430
> ##D pushViewport(viewport(layout.pos.col=4,
431
> ##D                        layout.pos.row=1))
432
> ##D grid.rect(gp=gpar(col="grey"))
433
> ##D grid.arrows(x=unit(0:80/100, "npc"),
434
> ##D             y=unit(1 - (0:80/100)^2, "npc"))
435
> ##D popViewport()
436
> ##D pushViewport(viewport(layout.pos.col=1,
437
> ##D                        layout.pos.row=2))
438
> ##D grid.rect(gp=gpar(col="grey"))
439
> ##D grid.arrows(ends="both")
440
> ##D popViewport()
441
> ##D pushViewport(viewport(layout.pos.col=2,
442
> ##D                        layout.pos.row=2))
443
> ##D grid.rect(gp=gpar(col="grey"))
444
> ##D # Recycling arguments
445
> ##D grid.arrows(x=unit(1:10/11, "npc"), y=unit(1:3/4, "npc"))
446
> ##D popViewport()
447
> ##D pushViewport(viewport(layout.pos.col=3,
448
> ##D                        layout.pos.row=2))
449
> ##D grid.rect(gp=gpar(col="grey"))
450
> ##D # Drawing arrows on a segments grob
451
> ##D gs <- segmentsGrob(x0=unit(1:4/5, "npc"),
452
> ##D                    x1=unit(1:4/5, "npc"))
453
> ##D grid.arrows(grob=gs, length=unit(0.1, "npc"),
454
> ##D             type="closed", gp=gpar(fill="white"))
455
> ##D popViewport()
456
> ##D pushViewport(viewport(layout.pos.col=4,
457
> ##D                        layout.pos.row=2))
458
> ##D grid.rect(gp=gpar(col="grey"))
459
> ##D # Arrows on a lines grob
460
> ##D # Name these because going to grid.edit them later
461
> ##D gl <- linesGrob(name="curve", x=unit(0:80/100, "npc"),
462
> ##D                 y=unit((0:80/100)^2, "npc"))
463
> ##D grid.arrows(name="arrowOnLine", grob=gl, angle=15, type="closed",
464
> ##D             gp=gpar(fill="black"))
465
> ##D popViewport()
466
> ##D pushViewport(viewport(layout.pos.col=1,
467
> ##D                        layout.pos.row=2))
468
> ##D grid.move.to(x=0.5, y=0.8)
469
> ##D popViewport()
470
> ##D pushViewport(viewport(layout.pos.col=4,
471
> ##D                        layout.pos.row=1))
472
> ##D # Arrows on a line.to grob
473
> ##D glt <- lineToGrob(x=0.5, y=0.2, gp=gpar(lwd=3))
474
> ##D grid.arrows(grob=glt, ends="first", gp=gpar(lwd=3))
475
> ##D popViewport(2)
476
> ##D grid.edit(gPath("arrowOnLine", "curve"), y=unit((0:80/100)^3, "npc"))
477
> ## End(Not run)
478
> 
479
> 
480
> 
481
> cleanEx()
57725 murrell 482
> nameEx("grid.bezier")
483
> ### * grid.bezier
484
> 
485
> flush(stderr()); flush(stdout())
486
> 
487
> ### Name: grid.bezier
488
> ### Title: Draw a Bezier Curve
489
> ### Aliases: grid.bezier bezierGrob
490
> ### Keywords: dplot
491
> 
492
> ### ** Examples
493
> 
494
> x <- c(0.2, 0.2, 0.4, 0.4)
495
> y <- c(0.2, 0.4, 0.4, 0.2)
496
> 
497
> grid.newpage()
498
> grid.bezier(x, y)
499
> grid.bezier(c(x, x + .4), c(y + .4, y + .4),
500
+             id=rep(1:2, each=4))
501
> grid.segments(.4, .6, .6, .6)
502
> grid.bezier(x, y,
503
+             gp=gpar(lwd=3, fill="black"),
504
+             arrow=arrow(type="closed"),
505
+             vp=viewport(x=.9))
506
> 
507
> 
508
> 
509
> cleanEx()
56186 murdoch 510
> nameEx("grid.cap")
511
> ### * grid.cap
512
> 
513
> flush(stderr()); flush(stdout())
514
> 
515
> ### Name: grid.cap
516
> ### Title: Capture a raster image
517
> ### Aliases: grid.cap
518
> ### Keywords: dplot
519
> 
520
> ### ** Examples
521
> 
522
> ## Not run: 
523
> ##D dev.new(width=.5, height=.5)
524
> ##D grid.rect()
525
> ##D grid.text("hi")
526
> ##D cap <- grid.cap()
527
> ##D dev.off()
528
> ##D 
529
> ##D grid.raster(cap, width=.5, height=.5, interpolate=FALSE)
530
> ## End(Not run)
531
> 
532
> 
533
> 
534
> cleanEx()
535
> nameEx("grid.clip")
536
> ### * grid.clip
537
> 
538
> flush(stderr()); flush(stdout())
539
> 
540
> ### Name: grid.clip
541
> ### Title: Set the Clipping Region
542
> ### Aliases: grid.clip clipGrob
543
> ### Keywords: dplot
544
> 
545
> ### ** Examples
546
> 
547
> # draw across entire viewport, but clipped
548
> grid.clip(x = 0.3, width = 0.1)
549
> grid.lines(gp=gpar(col="green", lwd=5))
550
> # draw across entire viewport, but clipped (in different place)
551
> grid.clip(x = 0.7, width = 0.1)
552
> grid.lines(gp=gpar(col="red", lwd=5))
553
> # Viewport sets new clip region
554
> pushViewport(viewport(width=0.5, height=0.5, clip=TRUE))
555
> grid.lines(gp=gpar(col="grey", lwd=3))
61435 ripley 556
> # Return to original viewport;  get
56186 murdoch 557
> # clip region from previous grid.clip()
558
> # (NOT from previous viewport clip region)
559
> popViewport()
560
> grid.lines(gp=gpar(col="black"))
561
> 
562
> 
563
> 
564
> cleanEx()
565
> nameEx("grid.convert")
566
> ### * grid.convert
567
> 
568
> flush(stderr()); flush(stdout())
569
> 
570
> ### Name: grid.convert
571
> ### Title: Convert Between Different grid Coordinate Systems
572
> ### Aliases: grid.convert grid.convertX grid.convertY grid.convertWidth
573
> ###   grid.convertHeight convertUnit convertX convertY convertWidth
574
> ###   convertHeight
575
> ### Keywords: dplot
576
> 
577
> ### ** Examples
578
> 
579
> ## A tautology
580
> convertX(unit(1, "inches"), "inches")
581
[1] 1inches
582
> ## The physical units
583
> convertX(unit(2.54, "cm"), "inches")
584
[1] 1inches
585
> convertX(unit(25.4, "mm"), "inches")
586
[1] 1inches
587
> convertX(unit(72.27, "points"), "inches")
588
[1] 1inches
589
> convertX(unit(1/12*72.27, "picas"), "inches")
590
[1] 1inches
591
> convertX(unit(72, "bigpts"), "inches")
592
[1] 1inches
593
> convertX(unit(1157/1238*72.27, "dida"), "inches")
594
[1] 1inches
595
> convertX(unit(1/12*1157/1238*72.27, "cicero"), "inches")
596
[1] 1inches
597
> convertX(unit(65536*72.27, "scaledpts"), "inches")
598
[1] 1inches
599
> convertX(unit(1/2.54, "inches"), "cm")
600
[1] 1cm
601
> convertX(unit(1/25.4, "inches"), "mm")
602
[1] 1mm
603
> convertX(unit(1/72.27, "inches"), "points")
604
[1] 1points
605
> convertX(unit(1/(1/12*72.27), "inches"), "picas")
606
[1] 1picas
607
> convertX(unit(1/72, "inches"), "bigpts")
608
[1] 1bigpts
609
> convertX(unit(1/(1157/1238*72.27), "inches"), "dida")
610
[1] 1dida
611
> convertX(unit(1/(1/12*1157/1238*72.27), "inches"), "cicero")
612
[1] 1cicero
613
> convertX(unit(1/(65536*72.27), "inches"), "scaledpts")
614
[1] 1scaledpts
615
> 
616
> pushViewport(viewport(width=unit(1, "inches"),
617
+                        height=unit(2, "inches"),
618
+                        xscale=c(0, 1),
619
+                        yscale=c(1, 3)))
620
>   ## Location versus dimension
621
>   convertY(unit(2, "native"), "inches")
622
[1] 1inches
623
>   convertHeight(unit(2, "native"), "inches")
624
[1] 2inches
625
>   ## From "x" to "y" (the conversion is via "inches")
626
>   convertUnit(unit(1, "native"), "native",
627
+                axisFrom="x", axisTo="y")
628
[1] 2native
629
>   ## Convert several values at once
630
>   convertX(unit(c(0.5, 2.54), c("npc", "cm")),
631
+                 c("inches", "native"))
632
[1] 0.5inches 1native  
633
> popViewport()
634
> ## Convert a complex unit
635
> convertX(unit(1, "strwidth", "Hello"), "native")
636
[1] 0.0558333333333333native
637
> 
638
> 
639
> 
640
> cleanEx()
641
> nameEx("grid.curve")
642
> ### * grid.curve
643
> 
644
> flush(stderr()); flush(stdout())
645
> 
646
> ### Name: grid.curve
647
> ### Title: Draw a Curve Between Locations
648
> ### Aliases: grid.curve curveGrob arcCurvature
649
> ### Keywords: dplot
650
> 
651
> ### ** Examples
652
> 
653
> curveTest <- function(i, j, ...) {
654
+   pushViewport(viewport(layout.pos.col=j, layout.pos.row=i))
655
+   do.call("grid.curve", c(list(x1=.25, y1=.25, x2=.75, y2=.75), list(...)))
656
+   grid.text(sub("list\\((.*)\\)", "\\1",
657
+                 deparse(substitute(list(...)))),
658
+             y=unit(1, "npc"))
659
+   popViewport()
660
+ }
661
> # grid.newpage()
662
> pushViewport(plotViewport(c(0, 0, 1, 0),
663
+                           layout=grid.layout(2, 1, heights=c(2, 1))))
664
> pushViewport(viewport(layout.pos.row=1,
665
+                       layout=grid.layout(3, 3, respect=TRUE)))
666
> curveTest(1, 1)
667
> curveTest(1, 2, inflect=TRUE)
668
> curveTest(1, 3, angle=135)
669
> curveTest(2, 1, arrow=arrow())
670
> curveTest(2, 2, ncp=8)
671
> curveTest(2, 3, shape=0)
672
> curveTest(3, 1, curvature=-1)
673
> curveTest(3, 2, square=FALSE)
674
> curveTest(3, 3, debug=TRUE)
675
> popViewport()
676
> pushViewport(viewport(layout.pos.row=2,
677
+                       layout=grid.layout(3, 3)))
678
> curveTest(1, 1)
679
> curveTest(1, 2, inflect=TRUE)
680
> curveTest(1, 3, angle=135)
681
> curveTest(2, 1, arrow=arrow())
682
> curveTest(2, 2, ncp=8)
683
> curveTest(2, 3, shape=0)
684
> curveTest(3, 1, curvature=-1)
685
> curveTest(3, 2, square=FALSE)
686
> curveTest(3, 3, debug=TRUE)
687
> popViewport(2)
688
> 
689
> 
690
> 
691
> cleanEx()
61044 murrell 692
> nameEx("grid.delay")
693
> ### * grid.delay
694
> 
695
> flush(stderr()); flush(stdout())
696
> 
697
> ### Name: grid.delay
698
> ### Title: Encapsulate calculations and generating a grob
699
> ### Aliases: grid.delay delayGrob
700
> ### Keywords: dplot
701
> 
702
> ### ** Examples
703
> 
704
> grid.delay({
705
+               w <- convertWidth(unit(1, "inches"), "npc")
706
+               rectGrob(width=w)
707
+             },
708
+             list())
709
> 
710
> 
711
> 
712
> cleanEx()
56186 murdoch 713
> nameEx("grid.draw")
714
> ### * grid.draw
715
> 
716
> flush(stderr()); flush(stdout())
717
> 
718
> ### Name: grid.draw
719
> ### Title: Draw a grid grob
720
> ### Aliases: grid.draw
721
> ### Keywords: dplot
722
> 
723
> ### ** Examples
724
> 
725
> grid.newpage()
726
> ## Create a graphical object, but don't draw it
727
> l <- linesGrob()
728
> ## Draw it
729
> grid.draw(l)
730
> 
731
> 
732
> 
733
> cleanEx()
734
> nameEx("grid.edit")
735
> ### * grid.edit
736
> 
737
> flush(stderr()); flush(stdout())
738
> 
739
> ### Name: grid.edit
740
> ### Title: Edit the Description of a Grid Graphical Object
741
> ### Aliases: grid.edit grid.gedit editGrob
742
> ### Keywords: dplot
743
> 
744
> ### ** Examples
745
> 
746
> grid.newpage()
747
> grid.xaxis(name = "xa", vp = viewport(width=.5, height=.5))
748
> grid.edit("xa", gp = gpar(col="red"))
749
> # won't work because no ticks (at is NULL)
750
> try(grid.edit(gPath("xa", "ticks"), gp = gpar(col="green")))
751
Error in editDLfromGPath(gPath, specs, strict, grep, global, redraw) : 
752
  'gPath' (xa::ticks) not found
753
> grid.edit("xa", at = 1:4/5)
754
> # Now it should work
755
> try(grid.edit(gPath("xa", "ticks"), gp = gpar(col="green")))
756
> 
757
> 
758
> 
759
> cleanEx()
61044 murrell 760
> nameEx("grid.force")
761
> ### * grid.force
762
> 
763
> flush(stderr()); flush(stdout())
764
> 
765
> ### Name: grid.force
766
> ### Title: Force a grob into its components
767
> ### Aliases: grid.force grid.revert
768
> ### Keywords: dplot
769
> 
770
> ### ** Examples
771
> 
772
> grid.newpage()
773
> pushViewport(viewport(width=.5, height=.5))
774
> grid.xaxis()
775
> grid.ls()
776
GRID.text.248
777
GRID.xaxis.249
778
> grid.force()
779
> grid.ls()
780
GRID.text.248
781
GRID.xaxis.249
782
  major
783
  ticks
784
  labels
785
> grid.revert()
786
> grid.ls()
787
GRID.text.248
788
GRID.xaxis.249
789
> 
790
> 
791
> 
792
> cleanEx()
56186 murdoch 793
> nameEx("grid.frame")
794
> ### * grid.frame
795
> 
796
> flush(stderr()); flush(stdout())
797
> 
798
> ### Name: grid.frame
799
> ### Title: Create a Frame for Packing Objects
800
> ### Aliases: grid.frame frameGrob
801
> ### Keywords: dplot
802
> 
803
> ### ** Examples
804
> 
805
> grid.newpage()
806
> grid.frame(name="gf", draw=TRUE)
807
> grid.pack("gf", rectGrob(gp=gpar(fill="grey")), width=unit(1, "null"))
808
> grid.pack("gf", textGrob("hi there"), side="right")
809
> 
810
> 
811
> 
812
> cleanEx()
813
> nameEx("grid.function")
814
> ### * grid.function
815
> 
816
> flush(stderr()); flush(stdout())
817
> 
818
> ### Name: grid.function
819
> ### Title: Draw a curve representing a function
820
> ### Aliases: grid.function functionGrob grid.abline
821
> ### Keywords: dplot
822
> 
823
> ### ** Examples
824
> 
825
>     # abline
826
>     # NOTE: in ROOT viewport on screen, (0, 0) at top-left
827
>     #       and "native" is pixels!
828
>     grid.function(function(x) list(x=x, y=0 + 1*x))
829
>     # a more "normal" viewport with default normalized "native" coords
830
>     grid.newpage()
831
>     pushViewport(viewport())
832
>     grid.function(function(x) list(x=x, y=0 + 1*x))
833
>     # slightly simpler
834
>     grid.newpage()
835
>     pushViewport(viewport())
836
>     grid.abline()
837
>     # sine curve
838
>     grid.newpage()
839
>     pushViewport(viewport(xscale=c(0, 2*pi), yscale=c(-1, 1)))
840
>     grid.function(function(x) list(x=x, y=sin(x)))
841
>     # constrained sine curve
842
>     grid.newpage()
843
>     pushViewport(viewport(xscale=c(0, 2*pi), yscale=c(-1, 1)))
844
>     grid.function(function(x) list(x=x, y=sin(x)),
845
+                   range=0:1)
846
>     # inverse sine curve
847
>     grid.newpage()
848
>     pushViewport(viewport(xscale=c(-1, 1), yscale=c(0, 2*pi)))
849
>     grid.function(function(y) list(x=sin(y), y=y),
850
+                   range="y")
851
>     # parametric function
852
>     grid.newpage()
853
>     pushViewport(viewport(xscale=c(-1, 1), yscale=c(-1, 1)))
854
>     grid.function(function(t) list(x=cos(t), y=sin(t)),
855
+                   range=c(0, 9*pi/5))
856
>     # physical abline
857
>     grid.newpage()
858
>     grid.function(function(x) list(x=x, y=0 + 1*x),
859
+                   units="in")
860
> 
861
> 
862
> 
863
> cleanEx()
864
> nameEx("grid.get")
865
> ### * grid.get
866
> 
867
> flush(stderr()); flush(stdout())
868
> 
869
> ### Name: grid.get
870
> ### Title: Get a Grid Graphical Object
871
> ### Aliases: grid.get grid.gget getGrob
872
> ### Keywords: dplot
873
> 
874
> ### ** Examples
875
> 
876
> grid.xaxis(name="xa")
877
> grid.get("xa")
878
xaxis[xa] 
879
> grid.get(gPath("xa", "ticks"))
880
NULL
881
> 
882
> grid.draw(gTree(name="gt", children=gList(xaxisGrob(name="axis"))))
883
> grid.get(gPath("gt", "axis", "ticks"))
884
NULL
885
> 
886
> 
887
> 
888
> cleanEx()
889
> nameEx("grid.grab")
890
> ### * grid.grab
891
> 
892
> flush(stderr()); flush(stdout())
893
> 
894
> ### Name: grid.grab
895
> ### Title: Grab the current grid output
896
> ### Aliases: grid.grab grid.grabExpr
897
> ### Keywords: dplot
898
> 
899
> ### ** Examples
900
> 
901
> pushViewport(viewport(w=.5, h=.5))
902
> grid.rect()
903
> grid.points(stats::runif(10), stats::runif(10))
904
> popViewport()
905
> grab <- grid.grab()
906
> grid.newpage()
907
> grid.draw(grab)
908
> 
909
> 
910
> 
911
> cleanEx()
912
> nameEx("grid.layout")
913
> ### * grid.layout
914
> 
915
> flush(stderr()); flush(stdout())
916
> 
917
> ### Name: grid.layout
918
> ### Title: Create a Grid Layout
919
> ### Aliases: grid.layout
920
> ### Keywords: dplot
921
> 
922
> ### ** Examples
923
> 
924
> ## A variety of layouts (some a bit mid-bending ...)
925
> layout.torture()
926
> ## Demonstration of layout justification
927
> grid.newpage()
928
> testlay <- function(just="centre") {
929
+   pushViewport(viewport(layout=grid.layout(1, 1, widths=unit(1, "inches"),
930
+                           heights=unit(0.25, "npc"),
931
+                           just=just)))
932
+   pushViewport(viewport(layout.pos.col=1, layout.pos.row=1))
933
+   grid.rect()
934
+   grid.text(paste(just, collapse="-"))
935
+   popViewport(2)
936
+ }
937
> testlay()
938
> testlay(c("left", "top"))
939
> testlay(c("right", "top"))
940
> testlay(c("right", "bottom"))
941
> testlay(c("left", "bottom"))
942
> testlay(c("left"))
943
> testlay(c("right"))
944
> testlay(c("bottom"))
945
> testlay(c("top"))
946
> 
947
> 
948
> 
949
> cleanEx()
950
> nameEx("grid.lines")
951
> ### * grid.lines
952
> 
953
> flush(stderr()); flush(stdout())
954
> 
955
> ### Name: grid.lines
956
> ### Title: Draw Lines in a Grid Viewport
957
> ### Aliases: grid.lines linesGrob grid.polyline polylineGrob
958
> ### Keywords: dplot
959
> 
960
> ### ** Examples
961
> 
962
> grid.lines()
963
> # Using id (NOTE: locations are not in consecutive blocks)
964
> grid.newpage()
965
> grid.polyline(x=c((0:4)/10, rep(.5, 5), (10:6)/10, rep(.5, 5)),
966
+              y=c(rep(.5, 5), (10:6/10), rep(.5, 5), (0:4)/10),
967
+              id=rep(1:5, 4),
968
+              gp=gpar(col=1:5, lwd=3))
969
> # Using id.lengths
970
> grid.newpage()
971
> grid.polyline(x=outer(c(0, .5, 1, .5), 5:1/5),
972
+              y=outer(c(.5, 1, .5, 0), 5:1/5),
973
+              id.lengths=rep(4, 5),
974
+              gp=gpar(col=1:5, lwd=3))
975
> 
976
> 
977
> 
978
> cleanEx()
979
> nameEx("grid.locator")
980
> ### * grid.locator
981
> 
982
> flush(stderr()); flush(stdout())
983
> 
984
> ### Name: grid.locator
985
> ### Title: Capture a Mouse Click
986
> ### Aliases: grid.locator
987
> ### Keywords: dplot
988
> 
989
> ### ** Examples
990
> 
991
> if (interactive()) {
992
+   ## Need to write a more sophisticated unit as.character method
993
+   unittrim <- function(unit) {
994
+     sub("^([0-9]+|[0-9]+[.][0-9])[0-9]*", "\\1", as.character(unit))
995
+   }
996
+   do.click <- function(unit) {
997
+     click.locn <- grid.locator(unit)
998
+     grid.segments(unit.c(click.locn$x, unit(0, "npc")),
999
+                   unit.c(unit(0, "npc"), click.locn$y),
1000
+                   click.locn$x, click.locn$y,
1001
+                   gp=gpar(lty="dashed", col="grey"))
1002
+     grid.points(click.locn$x, click.locn$y, pch=16, size=unit(1, "mm"))
1003
+     clickx <- unittrim(click.locn$x)
1004
+     clicky <- unittrim(click.locn$y)
61157 ripley 1005
+     grid.text(paste0("(", clickx, ", ", clicky, ")"),
56186 murdoch 1006
+               click.locn$x + unit(2, "mm"), click.locn$y,
1007
+               just="left")
1008
+   }
1009
+   do.click("inches")
1010
+   pushViewport(viewport(width=0.5, height=0.5,
1011
+                          xscale=c(0, 100), yscale=c(0, 10)))
1012
+   grid.rect()
1013
+   grid.xaxis()
1014
+   grid.yaxis()
1015
+   do.click("native")
61435 ripley 1016
+   popViewport()
56186 murdoch 1017
+ }
1018
> 
1019
> 
1020
> 
1021
> cleanEx()
1022
> nameEx("grid.ls")
1023
> ### * grid.ls
1024
> 
1025
> flush(stderr()); flush(stdout())
1026
> 
1027
> ### Name: grid.ls
1028
> ### Title: List the names of grobs or viewports
1029
> ### Aliases: grid.ls nestedListing pathListing grobPathListing
1030
> ### Keywords: dplot
1031
> 
1032
> ### ** Examples
1033
> 
1034
> # A gTree, called "parent", with childrenvp vpTree (vp2 within vp1)
1035
> # and child grob, called "child", with vp vpPath (down to vp2)
1036
> sampleGTree <- gTree(name="parent",
1037
+                      children=gList(grob(name="child", vp="vp1::vp2")),
1038
+                      childrenvp=vpTree(parent=viewport(name="vp1"),
1039
+                                        children=vpList(viewport(name="vp2"))))
1040
> grid.ls(sampleGTree)
1041
parent
1042
  child
1043
> # Show viewports too
1044
> grid.ls(sampleGTree, view=TRUE)
1045
parent
1046
  vp1
1047
    vp2
1048
      2
1049
  vp1
1050
    vp2
1051
      child
1052
      2
1053
> # Only show viewports
1054
> grid.ls(sampleGTree, view=TRUE, grob=FALSE)
1055
vp1
1056
  vp2
1057
    2
1058
vp1
1059
  vp2
1060
    2
1061
> # Alternate displays
1062
> # nested listing, custom indent
1063
> grid.ls(sampleGTree, view=TRUE, print=nestedListing, gindent="--")
1064
parent
1065
--vp1
1066
----vp2
1067
------2
1068
--vp1
1069
----vp2
1070
------child
1071
------2
1072
> # path listing
1073
> grid.ls(sampleGTree, view=TRUE, print=pathListing)
1074
            | parent
1075
vp1
1076
vp1::vp2
1077
vp1::vp2::2
1078
vp1
1079
vp1::vp2
1080
vp1::vp2    | parent::child
1081
vp1::vp2::2
1082
> # path listing, without grobs aligned
1083
> grid.ls(sampleGTree, view=TRUE, print=pathListing, gAlign=FALSE)
1084
 | parent
1085
vp1
1086
vp1::vp2
1087
vp1::vp2::2
1088
vp1
1089
vp1::vp2
1090
vp1::vp2 | parent::child
1091
vp1::vp2::2
61435 ripley 1092
> # grob path listing
56186 murdoch 1093
> grid.ls(sampleGTree, view=TRUE, print=grobPathListing)
1094
         | parent
1095
vp1::vp2 | parent::child
1096
> # path listing, grobs only
1097
> grid.ls(sampleGTree, print=pathListing)
1098
 | parent
1099
 | parent::child
1100
> # path listing, viewports only
1101
> grid.ls(sampleGTree, view=TRUE, grob=FALSE, print=pathListing)
1102
vp1
1103
vp1::vp2
1104
vp1::vp2::2
1105
vp1
1106
vp1::vp2
1107
vp1::vp2::2
1108
> # raw flat listing
1109
> str(grid.ls(sampleGTree, view=TRUE, print=FALSE))
1110
List of 6
1111
 $ name   : chr [1:8] "parent" "vp1" "vp2" "2" ...
1112
 $ gDepth : num [1:8] 0 1 1 1 1 1 1 1
1113
 $ vpDepth: num [1:8] 0 0 1 2 0 1 2 2
1114
 $ gPath  : chr [1:8] "" "parent" "parent" "parent" ...
1115
 $ vpPath : chr [1:8] "" "" "vp1" "vp1::vp2" ...
60841 ripley 1116
 $ type   : chr [1:8] "gTreeListing" "vpListing" "vpListing" "vpUpListing" ...
56186 murdoch 1117
 - attr(*, "class")= chr "flatGridListing"
1118
> 
1119
> 
1120
> 
1121
> cleanEx()
1122
> nameEx("grid.move.to")
1123
> ### * grid.move.to
1124
> 
1125
> flush(stderr()); flush(stdout())
1126
> 
1127
> ### Name: grid.move.to
1128
> ### Title: Move or Draw to a Specified Position
1129
> ### Aliases: grid.move.to grid.line.to moveToGrob lineToGrob
1130
> ### Keywords: dplot
1131
> 
1132
> ### ** Examples
1133
> 
1134
> grid.newpage()
1135
> grid.move.to(0.5, 0.5)
1136
> grid.line.to(1, 1)
1137
> grid.line.to(0.5, 0)
1138
> pushViewport(viewport(x=0, y=0, w=0.25, h=0.25, just=c("left", "bottom")))
1139
> grid.rect()
1140
> grid.grill()
1141
> grid.line.to(0.5, 0.5)
1142
> popViewport()
1143
> 
1144
> 
1145
> 
1146
> cleanEx()
1147
> nameEx("grid.null")
1148
> ### * grid.null
1149
> 
1150
> flush(stderr()); flush(stdout())
1151
> 
1152
> ### Name: grid.null
1153
> ### Title: Null Graphical Object
1154
> ### Aliases: grid.null nullGrob
1155
> ### Keywords: dplot
1156
> 
1157
> ### ** Examples
1158
> 
1159
> grid.newpage()
1160
> grid.null(name="ref")
1161
> grid.rect(height=grobHeight("ref"))
1162
> grid.segments(0, 0, grobX("ref", 0), grobY("ref", 0))
1163
> 
1164
> 
1165
> 
1166
> cleanEx()
1167
> nameEx("grid.path")
1168
> ### * grid.path
1169
> 
1170
> flush(stderr()); flush(stdout())
1171
> 
1172
> ### Name: grid.path
1173
> ### Title: Draw a Path
1174
> ### Aliases: grid.path pathGrob
1175
> ### Keywords: dplot
1176
> 
1177
> ### ** Examples
1178
> 
56281 ripley 1179
> pathSample <- function(x, y, rule, gp = gpar()) {
56186 murdoch 1180
+     if (is.na(rule))
56281 ripley 1181
+         grid.path(x, y, id = rep(1:2, each = 4), gp = gp)
61435 ripley 1182
+     else
56281 ripley 1183
+         grid.path(x, y, id = rep(1:2, each = 4), rule = rule, gp = gp)
56186 murdoch 1184
+     if (!is.na(rule))
56281 ripley 1185
+         grid.text(paste("Rule:", rule), y = 0, just = "bottom")
56186 murdoch 1186
+ }
1187
> 
1188
> pathTriplet <- function(x, y, title) {
56281 ripley 1189
+     pushViewport(viewport(height = 0.9, layout = grid.layout(1, 3),
1190
+                           gp = gpar(cex = .7)))
1191
+     grid.rect(y = 1, height = unit(1, "char"), just = "top",
1192
+               gp = gpar(col = NA, fill = "grey"))
1193
+     grid.text(title, y = 1, just = "top")
1194
+     pushViewport(viewport(layout.pos.col = 1))
1195
+     pathSample(x, y, rule = "winding",
1196
+                gp = gpar(fill = "grey"))
56186 murdoch 1197
+     popViewport()
56281 ripley 1198
+     pushViewport(viewport(layout.pos.col = 2))
1199
+     pathSample(x, y, rule = "evenodd",
1200
+                gp = gpar(fill = "grey"))
56186 murdoch 1201
+     popViewport()
56281 ripley 1202
+     pushViewport(viewport(layout.pos.col = 3))
1203
+     pathSample(x, y, rule = NA)
56186 murdoch 1204
+     popViewport()
1205
+     popViewport()
1206
+ }
1207
> 
1208
> pathTest <- function() {
1209
+     grid.newpage()
56281 ripley 1210
+     pushViewport(viewport(layout = grid.layout(5, 1)))
1211
+     pushViewport(viewport(layout.pos.row = 1))
1212
+     pathTriplet(c(.1, .1, .9, .9, .2, .2, .8, .8),
1213
+                 c(.1, .9, .9, .1, .2, .8, .8, .2),
1214
+                 "Nested rectangles, both clockwise")
56186 murdoch 1215
+     popViewport()
56281 ripley 1216
+     pushViewport(viewport(layout.pos.row = 2))
1217
+     pathTriplet(c(.1, .1, .9, .9, .2, .8, .8, .2),
1218
+                 c(.1, .9, .9, .1, .2, .2, .8, .8),
1219
+                 "Nested rectangles, outer clockwise, inner anti-clockwise")
56186 murdoch 1220
+     popViewport()
56281 ripley 1221
+     pushViewport(viewport(layout.pos.row = 3))
1222
+     pathTriplet(c(.1, .1, .4, .4, .6, .9, .9, .6),
1223
+                 c(.1, .4, .4, .1, .6, .6, .9, .9),
1224
+                 "Disjoint rectangles")
56186 murdoch 1225
+     popViewport()
56281 ripley 1226
+     pushViewport(viewport(layout.pos.row = 4))
1227
+     pathTriplet(c(.1, .1, .6, .6, .4, .4, .9, .9),
1228
+                 c(.1, .6, .6, .1, .4, .9, .9, .4),
1229
+                 "Overlapping rectangles, both clockwise")
56186 murdoch 1230
+     popViewport()
56281 ripley 1231
+     pushViewport(viewport(layout.pos.row = 5))
1232
+     pathTriplet(c(.1, .1, .6, .6, .4, .9, .9, .4),
1233
+                 c(.1, .6, .6, .1, .4, .4, .9, .9),
1234
+                 "Overlapping rectangles, one clockwise, other anti-clockwise")
56186 murdoch 1235
+     popViewport()
1236
+     popViewport()
1237
+ }
1238
> 
1239
> pathTest()
1240
> 
1241
> 
1242
> 
1243
> cleanEx()
1244
> nameEx("grid.plot.and.legend")
1245
> ### * grid.plot.and.legend
1246
> 
1247
> flush(stderr()); flush(stdout())
1248
> 
1249
> ### Name: grid.plot.and.legend
1250
> ### Title: A Simple Plot and Legend Demo
1251
> ### Aliases: grid.plot.and.legend
1252
> ### Keywords: dplot
1253
> 
1254
> ### ** Examples
1255
> 
1256
> grid.plot.and.legend()
1257
> 
1258
> 
1259
> 
1260
> cleanEx()
1261
> nameEx("grid.polygon")
1262
> ### * grid.polygon
1263
> 
1264
> flush(stderr()); flush(stdout())
1265
> 
1266
> ### Name: grid.polygon
1267
> ### Title: Draw a Polygon
1268
> ### Aliases: grid.polygon polygonGrob
1269
> ### Keywords: dplot
1270
> 
1271
> ### ** Examples
1272
> 
1273
> grid.polygon()
1274
> # Using id (NOTE: locations are not in consecutive blocks)
1275
> grid.newpage()
1276
> grid.polygon(x=c((0:4)/10, rep(.5, 5), (10:6)/10, rep(.5, 5)),
1277
+              y=c(rep(.5, 5), (10:6/10), rep(.5, 5), (0:4)/10),
1278
+              id=rep(1:5, 4),
1279
+              gp=gpar(fill=1:5))
1280
> # Using id.lengths
1281
> grid.newpage()
1282
> grid.polygon(x=outer(c(0, .5, 1, .5), 5:1/5),
1283
+              y=outer(c(.5, 1, .5, 0), 5:1/5),
1284
+              id.lengths=rep(4, 5),
1285
+              gp=gpar(fill=1:5))
1286
> 
1287
> 
1288
> 
1289
> cleanEx()
1290
> nameEx("grid.raster")
1291
> ### * grid.raster
1292
> 
1293
> flush(stderr()); flush(stdout())
1294
> 
1295
> ### Name: grid.raster
1296
> ### Title: Render a raster object
1297
> ### Aliases: grid.raster rasterGrob
1298
> ### Keywords: dplot
1299
> 
1300
> ### ** Examples
1301
> 
1302
> redGradient <- matrix(hcl(0, 80, seq(50, 80, 10)),
1303
+                       nrow=4, ncol=5)
61435 ripley 1304
> # interpolated
56186 murdoch 1305
> grid.newpage()
1306
> grid.raster(redGradient)
1307
> # blocky
1308
> grid.newpage()
1309
> grid.raster(redGradient, interpolate=FALSE)
1310
> # blocky and stretched
1311
> grid.newpage()
1312
> grid.raster(redGradient, interpolate=FALSE, height=unit(1, "npc"))
1313
> 
1314
> # The same raster drawn several times
1315
> grid.newpage()
1316
> grid.raster(0, x=1:3/4, y=1:3/4, w=.1, interp=FALSE)
1317
> 
1318
> 
1319
> 
1320
> cleanEx()
1321
> nameEx("grid.record")
1322
> ### * grid.record
1323
> 
1324
> flush(stderr()); flush(stdout())
1325
> 
1326
> ### Name: grid.record
1327
> ### Title: Encapsulate calculations and drawing
1328
> ### Aliases: grid.record recordGrob
1329
> ### Keywords: dplot
1330
> 
1331
> ### ** Examples
1332
> 
1333
> grid.record({
1334
+               w <- convertWidth(unit(1, "inches"), "npc")
1335
+               grid.rect(width=w)
1336
+             },
1337
+             list())
1338
> 
1339
> 
1340
> 
1341
> cleanEx()
60969 ripley 1342
> nameEx("grid.reorder")
1343
> ### * grid.reorder
1344
> 
1345
> flush(stderr()); flush(stdout())
1346
> 
1347
> ### Name: grid.reorder
1348
> ### Title: Reorder the children of a gTree
1349
> ### Aliases: grid.reorder reorderGrob
1350
> ### Keywords: dplot
1351
> 
1352
> ### ** Examples
1353
> 
1354
> # gTree with two children, "red-rect" and "blue-rect" (in that order)
1355
> gt <- gTree(children=gList(
1356
+                 rectGrob(gp=gpar(col=NA, fill="red"),
1357
+                          width=.8, height=.2, name="red-rect"),
1358
+                 rectGrob(gp=gpar(col=NA, fill="blue"),
1359
+                          width=.2, height=.8, name="blue-rect")),
1360
+             name="gt")
1361
> grid.newpage()
1362
> grid.draw(gt)
1363
> # Spec entire order as numeric (blue-rect, red-rect)
1364
> grid.reorder("gt", 2:1)
1365
> # Spec entire order as character
1366
> grid.reorder("gt", c("red-rect", "blue-rect"))
1367
> # Only spec the one I want behind as character
1368
> grid.reorder("gt", "blue-rect")
1369
> # Only spec the one I want in front as character
1370
> grid.reorder("gt", "blue-rect", back=FALSE)
1371
> 
1372
> 
1373
> 
1374
> cleanEx()
56186 murdoch 1375
> nameEx("grid.roundrect")
1376
> ### * grid.roundrect
1377
> 
1378
> flush(stderr()); flush(stdout())
1379
> 
1380
> ### Name: roundrect
1381
> ### Title: Draw a rectangle with rounded corners
1382
> ### Aliases: roundrect grid.roundrect roundrectGrob
1383
> ### Keywords: dplot
1384
> 
1385
> ### ** Examples
1386
> 
1387
> grid.roundrect(width=.5, height=.5, name="rr")
1388
> theta <- seq(0, 360, length=50)
61435 ripley 1389
> for (i in 1:50)
56186 murdoch 1390
+     grid.circle(x=grobX("rr", theta[i]),
1391
+                 y=grobY("rr", theta[i]),
1392
+                 r=unit(1, "mm"),
1393
+                 gp=gpar(fill="black"))
1394
> 
1395
> 
1396
> 
1397
> cleanEx()
1398
> nameEx("grid.show.layout")
1399
> ### * grid.show.layout
1400
> 
1401
> flush(stderr()); flush(stdout())
1402
> 
1403
> ### Name: grid.show.layout
1404
> ### Title: Draw a Diagram of a Grid Layout
1405
> ### Aliases: grid.show.layout
1406
> ### Keywords: dplot
1407
> 
1408
> ### ** Examples
1409
> 
61435 ripley 1410
> ## Diagram of a simple layout
56186 murdoch 1411
> grid.show.layout(grid.layout(4,2,
1412
+                      heights=unit(rep(1, 4),
1413
+                                   c("lines", "lines", "lines", "null")),
1414
+                      widths=unit(c(1, 1), "inches")))
1415
> 
1416
> 
1417
> 
1418
> cleanEx()
1419
> nameEx("grid.show.viewport")
1420
> ### * grid.show.viewport
1421
> 
1422
> flush(stderr()); flush(stdout())
1423
> 
1424
> ### Name: grid.show.viewport
1425
> ### Title: Draw a Diagram of a Grid Viewport
1426
> ### Aliases: grid.show.viewport
1427
> ### Keywords: dplot
1428
> 
1429
> ### ** Examples
1430
> 
1431
> ## Diagram of a sample viewport
1432
> grid.show.viewport(viewport(x=0.6, y=0.6,
1433
+                             w=unit(1, "inches"), h=unit(1, "inches")))
1434
> grid.show.viewport(viewport(layout.pos.row=2, layout.pos.col=2:3),
1435
+                    grid.layout(3, 4))
1436
> 
1437
> 
1438
> 
1439
> cleanEx()
1440
> nameEx("grid.text")
1441
> ### * grid.text
1442
> 
1443
> flush(stderr()); flush(stdout())
1444
> 
1445
> ### Name: grid.text
1446
> ### Title: Draw Text
1447
> ### Aliases: grid.text textGrob
1448
> ### Keywords: dplot
1449
> 
1450
> ### ** Examples
1451
> 
1452
> grid.newpage()
1453
> x <- stats::runif(20)
1454
> y <- stats::runif(20)
1455
> rot <- stats::runif(20, 0, 360)
1456
> grid.text("SOMETHING NICE AND BIG", x=x, y=y, rot=rot,
1457
+           gp=gpar(fontsize=20, col="grey"))
1458
> grid.text("SOMETHING NICE AND BIG", x=x, y=y, rot=rot,
1459
+           gp=gpar(fontsize=20), check=TRUE)
1460
> grid.newpage()
1461
> draw.text <- function(just, i, j) {
1462
+   grid.text("ABCD", x=x[j], y=y[i], just=just)
1463
+   grid.text(deparse(substitute(just)), x=x[j], y=y[i] + unit(2, "lines"),
1464
+             gp=gpar(col="grey", fontsize=8))
1465
+ }
1466
> x <- unit(1:4/5, "npc")
1467
> y <- unit(1:4/5, "npc")
1468
> grid.grill(h=y, v=x, gp=gpar(col="grey"))
1469
> draw.text(c("bottom"), 1, 1)
1470
> draw.text(c("left", "bottom"), 2, 1)
1471
> draw.text(c("right", "bottom"), 3, 1)
1472
> draw.text(c("centre", "bottom"), 4, 1)
1473
> draw.text(c("centre"), 1, 2)
1474
> draw.text(c("left", "centre"), 2, 2)
1475
> draw.text(c("right", "centre"), 3, 2)
1476
> draw.text(c("centre", "centre"), 4, 2)
1477
> draw.text(c("top"), 1, 3)
1478
> draw.text(c("left", "top"), 2, 3)
1479
> draw.text(c("right", "top"), 3, 3)
1480
> draw.text(c("centre", "top"), 4, 3)
1481
> draw.text(c(), 1, 4)
1482
> draw.text(c("left"), 2, 4)
1483
> draw.text(c("right"), 3, 4)
1484
> draw.text(c("centre"), 4, 4)
1485
> 
1486
> 
1487
> 
1488
> cleanEx()
1489
> nameEx("grid.xspline")
1490
> ### * grid.xspline
1491
> 
1492
> flush(stderr()); flush(stdout())
1493
> 
1494
> ### Name: grid.xspline
1495
> ### Title: Draw an Xspline
1496
> ### Aliases: grid.xspline xsplineGrob
1497
> ### Keywords: dplot
1498
> 
1499
> ### ** Examples
1500
> 
1501
> x <- c(0.25, 0.25, 0.75, 0.75)
1502
> y <- c(0.25, 0.75, 0.75, 0.25)
1503
> 
1504
> xsplineTest <- function(s, i, j, open) {
1505
+   pushViewport(viewport(layout.pos.col=j, layout.pos.row=i))
1506
+   grid.points(x, y, default.units="npc", pch=16, size=unit(2, "mm"))
1507
+   grid.xspline(x, y, shape=s, open=open, gp=gpar(fill="grey"))
1508
+   grid.text(s, gp=gpar(col="grey"),
1509
+             x=unit(x, "npc") + unit(c(-1, -1, 1, 1), "mm"),
1510
+             y=unit(y, "npc") + unit(c(-1, 1, 1, -1), "mm"),
1511
+             hjust=c(1, 1, 0, 0),
1512
+             vjust=c(1, 0, 0, 1))
1513
+   popViewport()
1514
+ }
1515
> 
1516
> pushViewport(viewport(width=.5, x=0, just="left",
1517
+                       layout=grid.layout(3, 3, respect=TRUE)))
1518
> pushViewport(viewport(layout.pos.row=1))
1519
> grid.text("Open Splines", y=1, just="bottom")
1520
> popViewport()
1521
> xsplineTest(c(0, -1, -1, 0), 1, 1, TRUE)
1522
> xsplineTest(c(0, -1,  0, 0), 1, 2, TRUE)
1523
> xsplineTest(c(0, -1,  1, 0), 1, 3, TRUE)
1524
> xsplineTest(c(0,  0, -1, 0), 2, 1, TRUE)
1525
> xsplineTest(c(0,  0,  0, 0), 2, 2, TRUE)
1526
> xsplineTest(c(0,  0,  1, 0), 2, 3, TRUE)
1527
> xsplineTest(c(0,  1, -1, 0), 3, 1, TRUE)
1528
> xsplineTest(c(0,  1,  0, 0), 3, 2, TRUE)
1529
> xsplineTest(c(0,  1,  1, 0), 3, 3, TRUE)
1530
> popViewport()
1531
> pushViewport(viewport(width=.5, x=1, just="right",
1532
+                       layout=grid.layout(3, 3, respect=TRUE)))
1533
> pushViewport(viewport(layout.pos.row=1))
1534
> grid.text("Closed Splines", y=1, just="bottom")
1535
> popViewport()
1536
> xsplineTest(c(-1, -1, -1, -1), 1, 1, FALSE)
1537
> xsplineTest(c(-1, -1,  0, -1), 1, 2, FALSE)
1538
> xsplineTest(c(-1, -1,  1, -1), 1, 3, FALSE)
1539
> xsplineTest(c( 0,  0, -1,  0), 2, 1, FALSE)
1540
> xsplineTest(c( 0,  0,  0,  0), 2, 2, FALSE)
1541
> xsplineTest(c( 0,  0,  1,  0), 2, 3, FALSE)
1542
> xsplineTest(c( 1,  1, -1,  1), 3, 1, FALSE)
1543
> xsplineTest(c( 1,  1,  0,  1), 3, 2, FALSE)
1544
> xsplineTest(c( 1,  1,  1,  1), 3, 3, FALSE)
1545
> popViewport()
1546
> 
1547
> 
1548
> 
1549
> cleanEx()
1550
> nameEx("showGrob")
1551
> ### * showGrob
1552
> 
1553
> flush(stderr()); flush(stdout())
1554
> 
1555
> ### Name: showGrob
1556
> ### Title: Label grid grobs.
1557
> ### Aliases: showGrob
1558
> ### Keywords: dplot
1559
> 
1560
> ### ** Examples
1561
> 
1562
>     grid.newpage()
1563
>     gt <- gTree(childrenvp=vpStack(
1564
+                   viewport(x=0, width=.5, just="left", name="vp"),
1565
+                   viewport(y=.5, height=.5, just="bottom", name="vp2")),
1566
+                 children=gList(rectGrob(vp="vp::vp2", name="child")),
1567
+                 name="parent")
1568
>     grid.draw(gt)
1569
>     showGrob()
1570
>     showGrob(gPath="child")
1571
>     showGrob(recurse=FALSE)
1572
>     showGrob(depth=1)
1573
>     showGrob(depth=2)
1574
>     showGrob(depth=1:2)
1575
>     showGrob(gt)
1576
>     showGrob(gt, gPath="child")
1577
>     showGrob(just="left", gp=gpar(col="red", cex=.5), rot=45)
1578
>     showGrob(labelfun=function(grob, ...) {
1579
+         x <- grobX(grob, "west")
1580
+         y <- grobY(grob, "north")
1581
+         gTree(children=gList(rectGrob(x=x, y=y,
1582
+                 width=stringWidth(grob$name) + unit(2, "mm"),
1583
+                 height=stringHeight(grob$name) + unit(2, "mm"),
1584
+                 gp=gpar(col=NA, fill=rgb(1, 0, 0, .5)),
1585
+                 just=c("left", "top")),
1586
+                 textGrob(grob$name,
1587
+                          x=x + unit(1, "mm"), y=y - unit(1, "mm"),
1588
+                          just=c("left", "top"))))
1589
+     })
1590
> 
1591
> ## Not run: 
1592
> ##D     # Examples from higher-level packages
1593
> ##D 
1594
> ##D     library(lattice)
1595
> ##D     # Ctrl-c after first example
1596
> ##D     example(histogram)
1597
> ##D     showGrob()
1598
> ##D     showGrob(gPath="plot_01.ylab")
61435 ripley 1599
> ##D 
56186 murdoch 1600
> ##D     library(ggplot2)
1601
> ##D     # Ctrl-c after first example
1602
> ##D     example(qplot)
1603
> ##D     showGrob()
1604
> ##D     showGrob(recurse=FALSE)
1605
> ##D     showGrob(gPath="panel-3-3")
1606
> ##D     showGrob(gPath="axis.title", grep=TRUE)
1607
> ##D     showGrob(depth=2)
1608
> ## End(Not run)
1609
> 
1610
> 
1611
> 
1612
> cleanEx()
1613
> nameEx("showViewport")
1614
> ### * showViewport
1615
> 
1616
> flush(stderr()); flush(stdout())
1617
> 
1618
> ### Name: showViewport
1619
> ### Title: Display grid viewports.
1620
> ### Aliases: showViewport
1621
> ### Keywords: dplot
1622
> 
1623
> ### ** Examples
1624
> 
61044 murrell 1625
> showViewport(viewport(width=.5, height=.5, name="vp"))
56186 murdoch 1626
> 
61044 murrell 1627
> showViewport(vpStack(viewport(width=.5, height=.5, name="vp1"),
1628
+                      viewport(width=.5, height=.5, name="vp2")),
56186 murdoch 1629
+              newpage=TRUE)
1630
> 
61044 murrell 1631
> showViewport(vpStack(viewport(width=.5, height=.5, name="vp1"),
1632
+                      viewport(width=.5, height=.5, name="vp2")),
56186 murdoch 1633
+              fill=rgb(1:0, 0:1, 0, .1),
1634
+              newpage=TRUE)
1635
> 
1636
> 
1637
> 
1638
> cleanEx()
1639
> nameEx("unit")
1640
> ### * unit
1641
> 
1642
> flush(stderr()); flush(stdout())
1643
> 
1644
> ### Name: unit
1645
> ### Title: Function to Create a Unit Object
1646
> ### Aliases: unit
1647
> ### Keywords: dplot
1648
> 
1649
> ### ** Examples
1650
> 
1651
> unit(1, "npc")
1652
[1] 1npc
1653
> unit(1:3/4, "npc")
1654
[1] 0.25npc 0.5npc  0.75npc
1655
> unit(1:3/4, "npc") + unit(1, "inches")
1656
[1] 0.25npc+1inches 0.5npc+1inches  0.75npc+1inches
1657
> min(unit(0.5, "npc"), unit(1, "inches"))
1658
[1] min(0.5npc, 1inches)
1659
> unit.c(unit(0.5, "npc"), unit(2, "inches") + unit(1:3/4, "npc"),
1660
+        unit(1, "strwidth", "hi there"))
1661
[1] 0.5npc          2inches+0.25npc 2inches+0.5npc  2inches+0.75npc
1662
[5] 1strwidth      
1663
> 
1664
> 
1665
> 
1666
> cleanEx()
1667
> nameEx("unit.length")
1668
> ### * unit.length
1669
> 
1670
> flush(stderr()); flush(stdout())
1671
> 
1672
> ### Name: unit.length
1673
> ### Title: Length of a Unit Object
1674
> ### Aliases: unit.length
1675
> ### Keywords: dplot
1676
> 
1677
> ### ** Examples
1678
> 
1679
> length(unit(1:3, "npc"))
1680
[1] 3
1681
> length(unit(1:3, "npc") + unit(1, "inches"))
1682
[1] 3
1683
> length(max(unit(1:3, "npc") + unit(1, "inches")))
1684
[1] 1
1685
> length(max(unit(1:3, "npc") + unit(1, "strwidth", "a"))*4)
1686
[1] 1
1687
> length(unit(1:3, "npc") + unit(1, "strwidth", "a")*4)
1688
[1] 3
1689
> 
1690
> 
1691
> 
1692
> cleanEx()
1693
> nameEx("unit.pmin")
1694
> ### * unit.pmin
1695
> 
1696
> flush(stderr()); flush(stdout())
1697
> 
1698
> ### Name: unit.pmin
1699
> ### Title: Parallel Unit Minima and Maxima
1700
> ### Aliases: unit.pmin unit.pmax
1701
> ### Keywords: dplot
1702
> 
1703
> ### ** Examples
1704
> 
1705
> max(unit(1:3, "cm"), unit(0.5, "npc"))
1706
[1] max(1cm, 2cm, 3cm, 0.5npc)
1707
> unit.pmax(unit(1:3, "cm"), unit(0.5, "npc"))
1708
[1] max(1cm, 0.5npc) max(2cm, 0.5npc) max(3cm, 0.5npc)
1709
> 
1710
> 
1711
> 
1712
> cleanEx()
1713
> nameEx("unit.rep")
1714
> ### * unit.rep
1715
> 
1716
> flush(stderr()); flush(stdout())
1717
> 
1718
> ### Name: unit.rep
1719
> ### Title: Replicate Elements of Unit Objects
1720
> ### Aliases: unit.rep
1721
> ### Keywords: dplot
1722
> 
1723
> ### ** Examples
1724
> 
1725
> rep(unit(1:3, "npc"), 3)
1726
[1] 1npc 2npc 3npc 1npc 2npc 3npc 1npc 2npc 3npc
1727
> rep(unit(1:3, "npc"), 1:3)
1728
[1] 1npc 2npc 2npc 3npc 3npc 3npc
1729
> rep(unit(1:3, "npc") + unit(1, "inches"), 3)
1730
[1] 1npc+1inches 2npc+1inches 3npc+1inches 1npc+1inches 2npc+1inches
1731
[6] 3npc+1inches 1npc+1inches 2npc+1inches 3npc+1inches
1732
> rep(max(unit(1:3, "npc") + unit(1, "inches")), 3)
1733
[1] max(1npc+1inches, 2npc+1inches, 3npc+1inches)
1734
[2] max(1npc+1inches, 2npc+1inches, 3npc+1inches)
1735
[3] max(1npc+1inches, 2npc+1inches, 3npc+1inches)
1736
> rep(max(unit(1:3, "npc") + unit(1, "strwidth", "a"))*4, 3)
1737
[1] 4*max(1npc+1strwidth, 2npc+1strwidth, 3npc+1strwidth)
1738
[2] 4*max(1npc+1strwidth, 2npc+1strwidth, 3npc+1strwidth)
1739
[3] 4*max(1npc+1strwidth, 2npc+1strwidth, 3npc+1strwidth)
1740
> rep(unit(1:3, "npc") + unit(1, "strwidth", "a")*4, 3)
1741
[1] 1npc+4*1strwidth 2npc+4*1strwidth 3npc+4*1strwidth 1npc+4*1strwidth
1742
[5] 2npc+4*1strwidth 3npc+4*1strwidth 1npc+4*1strwidth 2npc+4*1strwidth
1743
[9] 3npc+4*1strwidth
1744
> 
1745
> 
1746
> 
1747
> cleanEx()
1748
> nameEx("viewport")
1749
> ### * viewport
1750
> 
1751
> flush(stderr()); flush(stdout())
1752
> 
1753
> ### Name: Grid Viewports
1754
> ### Title: Create a Grid Viewport
1755
> ### Aliases: viewport vpList vpStack vpTree
1756
> ### Keywords: dplot
1757
> 
1758
> ### ** Examples
1759
> 
1760
> # Diagram of a sample viewport
1761
> grid.show.viewport(viewport(x=0.6, y=0.6,
1762
+                    w=unit(1, "inches"), h=unit(1, "inches")))
1763
> # Demonstrate viewport clipping
1764
> clip.demo <- function(i, j, clip1, clip2) {
1765
+   pushViewport(viewport(layout.pos.col=i,
1766
+                          layout.pos.row=j))
1767
+   pushViewport(viewport(width=0.6, height=0.6, clip=clip1))
1768
+   grid.rect(gp=gpar(fill="white"))
1769
+   grid.circle(r=0.55, gp=gpar(col="red", fill="pink"))
1770
+   popViewport()
1771
+   pushViewport(viewport(width=0.6, height=0.6, clip=clip2))
1772
+   grid.polygon(x=c(0.5, 1.1, 0.6, 1.1, 0.5, -0.1, 0.4, -0.1),
1773
+                y=c(0.6, 1.1, 0.5, -0.1, 0.4, -0.1, 0.5, 1.1),
1774
+                gp=gpar(col="blue", fill="light blue"))
1775
+   popViewport(2)
1776
+ }
1777
> 
1778
> grid.newpage()
1779
> grid.rect(gp=gpar(fill="grey"))
1780
> pushViewport(viewport(layout=grid.layout(2, 2)))
1781
> clip.demo(1, 1, FALSE, FALSE)
1782
> clip.demo(1, 2, TRUE, FALSE)
1783
> clip.demo(2, 1, FALSE, TRUE)
1784
> clip.demo(2, 2, TRUE, TRUE)
1785
> popViewport()
1786
> # Demonstrate turning clipping off
1787
> grid.newpage()
1788
> pushViewport(viewport(w=.5, h=.5, clip="on"))
1789
> grid.rect()
1790
> grid.circle(r=.6, gp=gpar(lwd=10))
1791
> pushViewport(viewport(clip="inherit"))
1792
> grid.circle(r=.6, gp=gpar(lwd=5, col="grey"))
1793
> pushViewport(viewport(clip="off"))
1794
> grid.circle(r=.6)
1795
> popViewport(3)
1796
> # Demonstrate vpList, vpStack, and vpTree
1797
> grid.newpage()
1798
> tree <- vpTree(viewport(w=0.8, h=0.8, name="A"),
1799
+                vpList(vpStack(viewport(x=0.1, y=0.1, w=0.5, h=0.5,
1800
+                                        just=c("left", "bottom"), name="B"),
61435 ripley 1801
+                               viewport(x=0.1, y=0.1, w=0.5, h=0.5,
56186 murdoch 1802
+                                        just=c("left", "bottom"), name="C"),
61435 ripley 1803
+                               viewport(x=0.1, y=0.1, w=0.5, h=0.5,
56186 murdoch 1804
+                                        just=c("left", "bottom"), name="D")),
1805
+                       viewport(x=0.5, w=0.4, h=0.9,
1806
+                                just="left", name="E")))
1807
> pushViewport(tree)
1808
> for (i in LETTERS[1:5]) {
1809
+   seekViewport(i)
1810
+   grid.rect()
1811
+   grid.text(current.vpTree(FALSE),
1812
+             x=unit(1, "mm"), y=unit(1, "npc") - unit(1, "mm"),
1813
+             just=c("left", "top"),
1814
+             gp=gpar(fontsize=8))
1815
+ }
1816
> 
1817
> 
1818
> 
1819
> cleanEx()
1820
> nameEx("viewports")
1821
> ### * viewports
1822
> 
1823
> flush(stderr()); flush(stdout())
1824
> 
1825
> ### Name: Working with Viewports
1826
> ### Title: Maintaining and Navigating the Grid Viewport Tree
1827
> ### Aliases: pushViewport downViewport seekViewport popViewport upViewport
1828
> ### Keywords: dplot
1829
> 
1830
> ### ** Examples
1831
> 
1832
> # push the same viewport several times
1833
> grid.newpage()
1834
> vp <- viewport(width=0.5, height=0.5)
1835
> pushViewport(vp)
1836
> grid.rect(gp=gpar(col="blue"))
1837
> grid.text("Quarter of the device",
1838
+   y=unit(1, "npc") - unit(1, "lines"), gp=gpar(col="blue"))
1839
> pushViewport(vp)
1840
> grid.rect(gp=gpar(col="red"))
1841
> grid.text("Quarter of the parent viewport",
1842
+   y=unit(1, "npc") - unit(1, "lines"), gp=gpar(col="red"))
1843
> popViewport(2)
1844
> # push several viewports then navigate amongst them
1845
> grid.newpage()
1846
> grid.rect(gp=gpar(col="grey"))
1847
> grid.text("Top-level viewport",
1848
+   y=unit(1, "npc") - unit(1, "lines"), gp=gpar(col="grey"))
1849
> if (interactive()) Sys.sleep(1.0)
1850
> pushViewport(viewport(width=0.8, height=0.7, name="A"))
1851
> grid.rect(gp=gpar(col="blue"))
1852
> grid.text("1. Push Viewport A",
1853
+   y=unit(1, "npc") - unit(1, "lines"), gp=gpar(col="blue"))
1854
> if (interactive()) Sys.sleep(1.0)
1855
> pushViewport(viewport(x=0.1, width=0.3, height=0.6,
1856
+   just="left", name="B"))
1857
> grid.rect(gp=gpar(col="red"))
1858
> grid.text("2. Push Viewport B (in A)",
1859
+   y=unit(1, "npc") - unit(1, "lines"), gp=gpar(col="red"))
1860
> if (interactive()) Sys.sleep(1.0)
1861
> upViewport(1)
1862
> grid.text("3. Up from B to A",
1863
+   y=unit(1, "npc") - unit(2, "lines"), gp=gpar(col="blue"))
1864
> if (interactive()) Sys.sleep(1.0)
1865
> pushViewport(viewport(x=0.5, width=0.4, height=0.8,
1866
+   just="left", name="C"))
1867
> grid.rect(gp=gpar(col="green"))
1868
> grid.text("4. Push Viewport C (in A)",
1869
+   y=unit(1, "npc") - unit(1, "lines"), gp=gpar(col="green"))
1870
> if (interactive()) Sys.sleep(1.0)
1871
> pushViewport(viewport(width=0.8, height=0.6, name="D"))
1872
> grid.rect()
1873
> grid.text("5. Push Viewport D (in C)",
1874
+   y=unit(1, "npc") - unit(1, "lines"))
1875
> if (interactive()) Sys.sleep(1.0)
1876
> upViewport(0)
1877
> grid.text("6. Up from D to top-level",
1878
+   y=unit(1, "npc") - unit(2, "lines"), gp=gpar(col="grey"))
1879
> if (interactive()) Sys.sleep(1.0)
1880
> downViewport("D")
1881
> grid.text("7. Down from top-level to D",
1882
+   y=unit(1, "npc") - unit(2, "lines"))
1883
> if (interactive()) Sys.sleep(1.0)
1884
> seekViewport("B")
1885
> grid.text("8. Seek from D to B",
1886
+   y=unit(1, "npc") - unit(2, "lines"), gp=gpar(col="red"))
1887
> pushViewport(viewport(width=0.9, height=0.5, name="A"))
1888
> grid.rect()
1889
> grid.text("9. Push Viewport A (in B)",
1890
+   y=unit(1, "npc") - unit(1, "lines"))
1891
> if (interactive()) Sys.sleep(1.0)
1892
> seekViewport("A")
1893
> grid.text("10. Seek from B to A (in ROOT)",
1894
+   y=unit(1, "npc") - unit(3, "lines"), gp=gpar(col="blue"))
1895
> if (interactive()) Sys.sleep(1.0)
1896
> seekViewport(vpPath("B", "A"))
1897
> grid.text("11. Seek from\nA (in ROOT)\nto A (in B)")
1898
> popViewport(0)
1899
> 
1900
> 
1901
> 
1902
> cleanEx()
1903
> nameEx("vpPath")
1904
> ### * vpPath
1905
> 
1906
> flush(stderr()); flush(stdout())
1907
> 
1908
> ### Name: vpPath
1909
> ### Title: Concatenate Viewport Names
1910
> ### Aliases: vpPath
1911
> ### Keywords: dplot
1912
> 
1913
> ### ** Examples
1914
> 
1915
> vpPath("vp1", "vp2")
1916
vp1::vp2 
1917
> 
1918
> 
1919
> 
1920
> cleanEx()
1921
> nameEx("xsplinePoints")
1922
> ### * xsplinePoints
1923
> 
1924
> flush(stderr()); flush(stdout())
1925
> 
1926
> ### Name: xsplinePoints
57725 murrell 1927
> ### Title: Return the points that would be used to draw an Xspline (or a
1928
> ###   Bezier curve).
1929
> ### Aliases: xsplinePoints bezierPoints
56186 murdoch 1930
> ### Keywords: dplot
1931
> 
1932
> ### ** Examples
1933
> 
1934
> grid.newpage()
1935
> xsg <- xsplineGrob(c(.1, .1, .9, .9), c(.1, .9, .9, .1), shape=1)
1936
> grid.draw(xsg)
1937
> trace <- xsplinePoints(xsg)
1938
> grid.circle(trace$x, trace$y, default.units="inches", r=unit(.5, "mm"))
1939
> 
57725 murrell 1940
> grid.newpage()
1941
> vp <- viewport(width=.5)
1942
> xg <- xsplineGrob(x=c(0, .2, .4, .2, .5, .7, .9, .7),
1943
+                   y=c(.5, 1, .5, 0, .5, 1, .5, 0),
1944
+                   id=rep(1:2, each=4),
1945
+                   shape=1,
1946
+                   vp=vp)
1947
> grid.draw(xg)
1948
> trace <- xsplinePoints(xg)
1949
> pushViewport(vp)
61044 murrell 1950
> invisible(lapply(trace, function(t) grid.lines(t$x, t$y, gp=gpar(col="red"))))
57725 murrell 1951
> popViewport()
56186 murdoch 1952
> 
57725 murrell 1953
> grid.newpage()
1954
> bg <- bezierGrob(c(.2, .2, .8, .8), c(.2, .8, .8, .2))
1955
> grid.draw(bg)
1956
> trace <- bezierPoints(bg)
1957
> grid.circle(trace$x, trace$y, default.units="inches", r=unit(.5, "mm"))
56186 murdoch 1958
> 
57725 murrell 1959
> 
1960
> 
56186 murdoch 1961
> ### * <FOOTER>
1962
> ###
62439 ripley 1963
> options(digits = 7L)
61787 ripley 1964
> base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n")
63202 ripley 1965
Time elapsed:  1.206 0.014 1.224 0 0 
56186 murdoch 1966
> grDevices::dev.off()
1967
null device 
1968
          1 
1969
> ###
1970
> ### Local variables: ***
1971
> ### mode: outline-minor ***
1972
> ### outline-regexp: "\\(> \\)?### [*]+" ***
1973
> ### End: ***
1974
> quit('no')