The R Project SVN R

Rev

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

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