The R Project SVN R

Rev

Rev 39581 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
28502 murrell 1
\documentclass[a4paper]{article}
2
%\VignetteIndexEntry{Writing grid Code}
3
%\VignettePackage{grid}
4
\newcommand{\grid}{{\tt grid}}
5
\newcommand{\grob}{{\tt grob}}
6
\newcommand{\gTree}{{\tt gTree}}
7
\newcommand{\R}{{\tt R}}
8
\setlength{\parindent}{0in}
9
\setlength{\parskip}{.1in}
28707 murrell 10
\setlength{\textwidth}{140mm}
11
\setlength{\oddsidemargin}{10mm}
28502 murrell 12
 
13
\newcommand{\aside}[1]{\begin{list}{}
14
                                   {\setlength{\leftmargin}{1in}
15
                                    \setlength{\rightmargin}{1in}
16
                                    \setlength{\itemindent}{0in}}
17
                       \item {\sc Aside:} {\it #1}
18
                       \end{list}}
19
 
20
\title{Writing \grid{} Code}
21
\author{Paul Murrell}
22
 
23
\begin{document}
24
\maketitle
25
 
28707 murrell 26
<<echo=FALSE, results=hide>>=
39581 murrell 27
library(grDevices)
28
library(stats) # for runif()
28707 murrell 29
library(grid)
30
ps.options(pointsize=12)
31
options(width=60)
32
 
28502 murrell 33
@
34
The \grid{} system contains a degree of complexity in order
35
to allow things like editing graphical objects, ``packing'' graphical
36
objects, and so on.  This means that many of the
37
predefined Grid graphics functions are
38
relatively complicated\footnote{Although there are exceptions;  some
39
functions, such as {\tt grid.show.viewport}, are purely for producing
40
illustrative diagrams and remain simple and procedural.}.
41
 
42
One design aim of \grid{} is to allow users to create simple 
43
graphics simply and not to force them to use  complicated concepts or
44
write complicated code
45
 unless they actually need to.  Along similar lines, it is 
46
intended that people should be able to prototype even complex graphics
47
very simply and then refine the implementation into a more 
48
sophisticated form if necessary.  
49
 
50
With the predefined graphics
51
functions being fully-developed and complicated implementations,
52
 there is a lack of examples of simple, prototype code.
53
Furthermore, given that the aim is to allow a range of ways to produce the same
54
graphical output, there is a need for examples which demonstrate the
55
various stages, from simple to complex, 
56
that a piece of \grid{} code can go through.
57
 
58
This document describes the
59
construction of a scatterplot object, like that shown below,
60
going from the simplest, prototype
61
implementation to the most complex and sophisticated.  
62
It demonstrates that if you
63
only want simple graphics output then you can do it pretty 
64
simply and quickly.  It also demonstrates how to write functions 
65
that allow your graphics to be used by other people.  Finally, it
66
demonstrates how to make your graphics fully interactive (or at least
67
as interactive as Grid will let you make it).
68
 
69
@
70
This document should be read {\em after} the \grid{} Users'
71
Guide.  Here we are assuming that the reader has an understanding
72
of viewports, layouts, and units.  For the later sections of the
73
document, it will also be helpful to have an understanding of
74
\R{}'s {\tt S3} object system.
75
 
76
\section*{Procedural \grid{}}
77
 
78
The simplest way to produce graphical output in Grid is just 
79
like producing standard R graphical output.  You simply issue a
80
series of graphics commands and each command adds more ink to the
81
plot.    
82
The purpose of the commands is simply to produce
83
graphics output;  in particular, we are not concerned with any
84
values returned by the plotting functions.  I will call this
85
{\it procedural graphics}. 
86
 
87
In order to draw a simple scatterplot, we can  issue a series
88
of commands which draw the various components of the plot.
89
 
90
Here are some random data to plot.
91
 
92
<<>>=
93
x <- runif(10)
94
y <- runif(10)
95
 
96
@
97
\noindent
98
The first step in creating the plot involves defining a ``data'' region.
99
This is a region which has sensible scales on the axes for plotting the
100
data and margins around the outside
101
for the axes to fit in, with a space for a title at the top.
102
 
103
<<datavp>>=
104
data.vp <- viewport(x=unit(5, "lines"),
105
                    y=unit(4, "lines"),
106
                    width=unit(1, "npc") - unit(7, "lines"),
107
                    height=unit(1, "npc") - unit(7, "lines"),
108
		    just=c("left", "bottom"),
109
		    xscale=range(x) + c(-.05, .05)*diff(range(x)),
110
		    yscale=range(y) + c(-.05, .05)*diff(range(y)))
111
 
112
@
113
\noindent
114
Now we create the data region and 
115
draw the components of the plot relative to it:  
116
points, axes, labels, and a 
117
title.
118
 
119
<<procplot>>=
120
pushViewport(data.vp)
121
grid.points(x, y)
122
grid.rect()
123
grid.xaxis()
124
grid.yaxis()
125
grid.text("x axis", y=unit(-3, "lines"), 
126
	  gp=gpar(fontsize=14))
127
grid.text("y axis", x=unit(-4, "lines"), 
128
	  gp=gpar(fontsize=14), rot=90)
129
grid.text("A Simple Plot", 
130
          y=unit(1, "npc") + unit(1.5, "lines"),
131
          gp=gpar(fontsize=16))
132
popViewport()
133
 
134
<<fig=TRUE, echo=FALSE, results=hide>>=
135
<<procplot>>
136
 
137
@
138
\section*{Facilitating Annotation}
139
 
140
Issuing a series of commands to produce a plot, like in the previous 
141
section, allows the user to have a great deal of flexibility.
142
It is always possible to recreate viewports in order to add 
143
further annotations.  For example, the following code
144
recreates the data region in order to place the date
145
at the bottom right corner.
146
 
147
<<ann1>>=
148
pushViewport(data.vp)
149
grid.text(date(), x=unit(1, "npc"),
150
  y = 0, just=c("right", "bottom"), gp=gpar(col="grey"))
151
popViewport()
152
 
153
<<fig=TRUE, echo=FALSE, results=hide>>=
154
<<procplot>>
155
<<ann1>>
156
 
157
@
158
 
159
When more complex arrangements of viewports are involved, there may be 
160
a bewildering array of viewports created, which may make it difficult
161
for other users to revisit a particular region of a plot.  A {\tt lattice}
162
plot is a good example.
163
In such cases, it will be more cooperative to use {\tt upViewport()}
164
rather than {\tt popViewport()} and leave the viewports that were
165
created during the drawing of the plot.  Other users can then use
166
{\tt vpPath}s to navigate to the desired region.  For example, here
167
is a slight modification of the original series of commands, where
168
the original data viewport is given a name and 
169
{\tt upViewport()} is used at the end.
170
 
171
<<results=hide>>=
172
data.vp <- viewport(name="dataregion",
173
                    x=unit(5, "lines"),
174
                    y=unit(4, "lines"),
175
                    width=unit(1, "npc") - unit(7, "lines"),
176
                    height=unit(1, "npc") - unit(7, "lines"),
177
		    just=c("left", "bottom"),
178
		    xscale=range(x) + c(-.05, .05)*diff(range(x)),
179
		    yscale=range(y) + c(-.05, .05)*diff(range(y)))
180
pushViewport(data.vp)
181
grid.points(x, y)
182
grid.rect()
183
grid.xaxis()
184
grid.yaxis()
185
grid.text("x axis", y=unit(-3, "lines"), 
186
	  gp=gpar(fontsize=14))
187
grid.text("y axis", x=unit(-4, "lines"), 
188
	  gp=gpar(fontsize=14), rot=90)
189
grid.text("A Simple Plot", 
190
          y=unit(1, "npc") + unit(1.5, "lines"),
191
          gp=gpar(fontsize=16))
192
upViewport()
193
 
194
@
195
 
196
The date is now added
197
using {\tt downViewport()} to get to the data region.
198
 
199
<<results=hide>>=
200
downViewport("dataregion")
201
grid.text(date(), x=unit(1, "npc"),
202
  y = 0, just=c("right", "bottom"), gp=gpar(col="grey"))
203
upViewport()
204
 
205
@
206
\section*{Writing a \grid{} Function}
207
 
208
Here is the scatterplot code wrapped up as a simple function.
209
 
210
<<funcplot>>=
211
splot <- function(x=runif(10), y=runif(10), title="A Simple Plot") {
212
data.vp <- viewport(name="dataregion",
213
                    x=unit(5, "lines"),
214
                    y=unit(4, "lines"),
215
                    width=unit(1, "npc") - unit(7, "lines"),
216
                    height=unit(1, "npc") - unit(7, "lines"),
217
		    just=c("left", "bottom"),
218
		    xscale=range(x) + c(-.05, .05)*diff(range(x)),
219
		    yscale=range(y) + c(-.05, .05)*diff(range(y)))
220
pushViewport(data.vp)
221
grid.points(x, y)
222
grid.rect()
223
grid.xaxis()
224
grid.yaxis()
225
grid.text("y axis", x=unit(-4, "lines"), 
226
	  gp=gpar(fontsize=14), rot=90)
227
grid.text(title, 
228
          y=unit(1, "npc") + unit(1.5, "lines"),
229
          gp=gpar(fontsize=16))
230
upViewport()
231
}
232
 
233
@
234
There are several advantages to creating a 
235
function:
236
\begin{enumerate}
237
\item We get the standard advantages of a function:  
238
we can reuse and maintain the plot code more easily.
239
\item We can slightly generalise the plot.  In this case, we can use it for 
240
different data and have a different title.  We could
241
add more arguments to allow different margins, control over
242
the axis scales, and so on.
243
\item The plot can be embedded in other graphics output.
244
\end{enumerate}
245
Here is an example which uses the {\tt splot()} function to
246
create a slightly modified scatterplot, embedded within 
247
other \grid{} output.
248
 
249
<<embed, fig=TRUE, results=hide>>=
250
grid.rect(gp=gpar(fill="grey"))
251
message <- paste("I could draw all sorts",
252
  "of stuff over here",
253
  "then create a viewport",
254
  "over there and stick",
255
  "a scatterplot in it.", sep="\n")
256
grid.text(message, x=0.25)
37142 murrell 257
grid.lines(x=unit.c(unit(0.25, "npc") + 0.5*stringWidth(message) +
28502 murrell 258
  unit(2, "mm"),
37142 murrell 259
  unit(0.5, "npc") - unit(2, "mm")), 
260
  y=0.5,
261
  arrow=arrow(angle=15, type="closed"),
28502 murrell 262
  gp=gpar(lwd=3, fill="black"))
263
pushViewport(viewport(x=0.5, height=0.5, width=0.45, just="left", 
264
  gp=gpar(cex=0.5)))
265
grid.rect(gp=gpar(fill="white"))
266
splot(1:10, 1:10, title="An Embedded Plot")
267
upViewport()
268
 
269
@
270
 
271
It is still straightforward to annotate the scatterplot as long as 
272
we have enough information about the viewports.  In this case,
273
a non-strict {\tt downViewport()} will still work (though note
274
that {\tt upViewport({\bf 0})} is required to get right back to the 
275
top level).
276
 
277
<<ann2, echo=FALSE, eval=FALSE>>=
278
downViewport("dataregion")
279
grid.text(date(), x=unit(1, "npc"),
280
  y = 0, just=c("right", "bottom"), gp=gpar(col="grey"))
281
upViewport(0)
282
 
283
<<echo=FALSE, results=hide>>=
284
<<embed>>
285
<<ann2>>
286
 
287
@
288
\section*{Creating \grid{} Graphical Objects}
289
 
290
A \grid{} function like the one in the previous section provides 
291
output which is very flexible and can be annotated in arbitrary ways
292
and can be embedded within other output.  This is likely
293
to satisfy most uses.
294
 
295
However, there are some things that cannot be done (or at least would
296
be extremely hard to do) with such a function.  The output produced by
297
the function cannot be addressed as a coherent whole.  It is
298
not possible, for example, to 
299
to change the {\tt x}
300
and {\tt y} data used in the plot and have the points and axes update
301
automatically.  There is no scatterplot object to save;  the individual
302
components exist, but they are not bound together as a whole.  If/when
303
these sorts of issues become important, it becomes necessary to 
304
create a \grid{} graphical object (a \grob{}) to represent the plot.
305
 
306
The first step is to write a function which will create a \grob{}
307
-- a {\it constructor} function.  In most cases, this will involve
308
creating a special sort of \grob{} called a \gTree{};  this is just
309
a \grob{} that can have other \grob{}s as children.  Here's an example
310
for creating an {\tt splot} \grob{}.  I have put bits of the 
311
construction into separate functions, for reasons which will become 
312
apparent later.
313
 
314
<<>>=
315
splot.data.vp <- function(x, y) {
316
  viewport(name="dataregion",
317
                    x=unit(5, "lines"),
318
                    y=unit(4, "lines"),
319
                    width=unit(1, "npc") - unit(7, "lines"),
320
                    height=unit(1, "npc") - unit(7, "lines"),
321
		    just=c("left", "bottom"),
322
		    xscale=range(x) + c(-.05, .05)*diff(range(x)),
323
		    yscale=range(y) + c(-.05, .05)*diff(range(y)))
324
}
325
 
326
splot.title <- function(title) {
327
      textGrob(title, name="title",
328
          y=unit(1, "npc") + unit(1.5, "lines"),
329
          gp=gpar(fontsize=16), vp="dataregion")
330
}
331
 
332
splot <- function(x, y, title, name=NULL, draw=TRUE, gp=gpar(), vp=NULL) {
333
  spg <- gTree(
334
    x=x, y=y, title=title,
335
    name=name,
336
    childrenvp = splot.data.vp(x, y),
337
    children=gList(rectGrob(name="border", vp="dataregion"), 
338
      xaxisGrob(name="xaxis", vp="dataregion"), 
339
      yaxisGrob(name="yaxis", vp="dataregion"),
340
      pointsGrob(x, y, name="points", vp="dataregion"),
341
      textGrob("x axis", y=unit(-3, "lines"), name="xlab",
342
	  gp=gpar(fontsize=14), vp="dataregion"),
343
      textGrob("y axis", x=unit(-4, "lines"), name="ylab",
344
	  gp=gpar(fontsize=14), rot=90, vp="dataregion"),
345
      splot.title(title)),
346
    gp=gp, vp=vp,
347
    cl="splot")
348
  if (draw)
349
    grid.draw(spg)
350
  spg
351
}
352
 
353
@
354
 
355
There are four important additions to the argument list compared
356
to the original {\tt splot()} function:
357
\begin{enumerate}
358
\item The {\tt name} argument allows a string identifier to be
359
associated with the scatterplot object we create.  This is 
360
important for being able to specify the scatterplot when we try to edit
361
it after drawing it and/or when it is part of a larger \grob{} 
362
(see later examples).
363
\item The {\tt draw} argument
364
 makes it possible to use the function in a procedural 
365
manner as before:
366
 
367
<<splotgrob, eval=FALSE, echo=FALSE>>=
368
sg <- splot(1:10, 1:10, "Same as Before", name="splot", draw=FALSE)
369
 
370
<<>>=
371
splot(1:10, 1:10, "Same as Before", name="splot")
372
downViewport("dataregion")
373
grid.text(date(), x=unit(1, "npc"),
374
  y = 0, just=c("right", "bottom"), gp=gpar(col="grey"))
375
upViewport(0)
376
 
377
@
378
\item The {\tt gp} argument allows the user to supply {\tt gpar()} 
379
settings for the scatterplot as a whole.
380
\item The {\tt vp} argument allows the user to supply a viewport
381
for the {\tt splot} \grob{} to be drawn in.  This is especially 
382
useful for specifying a {\tt vpPath} when the {\tt splot} is
383
used as a component of another \grob{} (see scatterplot matrix
384
example
385
below).
386
\end{enumerate}
387
 
388
The important parts of the \gTree{}  definition are:
389
\begin{enumerate}
390
\item The {\tt children} argument provides a list of \grob{}s which are
391
part of the scatterplot.  When the scatterplot is drawn, all children
392
will be drawn.  Notice that instead of the procedural {\tt grid.*()} functions
393
we use {\tt *Grob()} functions which just produce \grob{}s and do not
394
perform any drawing.  Also notice that I have given each of the
395
children a name;  this will make it possible to access the components
396
of the scatterplot (see later examples).
397
\item The {\tt childrenvp} argument provides a viewport (or 
398
{\tt vpStack}, {\tt vpList}, or {\tt vpTree}) which will be pushed before
399
the children are drawn.  The difference between this argument and the
400
{\tt vp} argument common to all \grob{}s is that the {\tt vp} is pushed before
401
drawing the children and then popped after, whereas the {\tt childrenvp}
402
gets pushed {\it and} then a call to {\tt upViewport()} is made
403
before the children are drawn.  This allows the children to simply 
404
specify the viewport they should be drawn in by way of a {\tt vpPath}
405
in their {\tt vp} argument.  In this way, viewports remain available
406
for further annotation such as we have already seen in procedural code.
407
\item The {\tt gp} and {\tt vp} arguments are automatically handled by
408
the \gTree{} drawing methods so that {\tt gpar()} settings will be 
409
enforced and the viewport will be pushed when the {\tt splot} is drawn.
410
\item The {\tt cl} argument means that the \grob{} created is a special
411
sort of \grob{} called {\tt splot}.  This will allow us to write methods
412
specifically for our scatterplot (see later examples).
413
\end{enumerate}
414
 
415
 
416
@
417
 
418
Now that we have a \grob{}, there are some more interesting things that
419
we can do with it.
420
First of all, the {\tt splot} \grob{} provides a container for the 
421
\grob{}s which make up the scatterplot.
422
If we modify the {\tt splot} \grob{}, it affects all of the children.
423
 
424
<<results=hide>>=
425
splot(1:10, 1:10, "Same as Before", name="splot")
426
grid.edit("splot", gp=gpar(cex=0.5))
427
 
428
<<fig=TRUE, echo=FALSE, results=hide>>=
429
<<splotgrob>>
430
sg <- editGrob(sg, gp=gpar(cex=0.5))
431
grid.draw(sg)
432
 
433
@ 
434
 
435
We can access elements of the {\tt splot} \grob{} to edit them 
436
individually.
437
 
438
<<results=hide>>=
439
splot(1:10, 1:10, "Same as Before", name="splot")
440
grid.edit(gPath("splot", "points"), gp=gpar(col=1:10))
441
 
442
<<fig=TRUE, echo=FALSE, results=hide>>=
443
<<splotgrob>>
444
sg <- editGrob(sg, gPath="points", gp=gpar(col=1:10))
445
grid.draw(sg)
446
 
447
@
448
 
449
With a little more work we can make the scatterplot a bit more dynamic.
450
The following describes a {\tt editDetails()} method for the 
451
{\tt splot} \grob{}.  This will be called whenever a scatterplot 
452
is edited and will update the components of the scatterplot.
453
 
454
<<>>=
455
editDetails.splot <- function(x, specs) {
456
  if (any(c("x", "y") %in% names(specs))) {
457
    if (is.null(specs$x))
458
      xx <- x$x
459
    else
460
      xx <- specs$x
461
    if (is.null(specs$y))
462
      yy <- x$y
463
    else
464
      yy <- specs$y
465
    x$childrenvp <- splot.data.vp(xx, yy)
466
    x <- addGrob(x, pointsGrob(xx, yy, name="points", vp="dataregion"))
467
  }
468
  x
469
}
470
splot(1:10, 1:10, "Same as Before", name="splot")
471
grid.edit("splot", x=1:100, y=(1:100)^2)
472
 
473
<<fig=TRUE, echo=FALSE, results=hide>>=
474
<<splotgrob>>
475
sg <- editGrob(sg, x=1:100, y=(1:100)^2)
476
grid.draw(sg)
477
 
478
@
479
 
480
The {\tt splot} \grob{} can also be used in the construction of
481
other \grob{}s.  Here's a simple scatterplot matrix 
482
\grob{}\footnote{{\bf Warning:}  As the number of \grob{}s in a \gTree{} gets
483
larger the construction of the \gTree{} will get slow.  If this happens,
484
the best solution is to just use a \grid{} function rather than
485
a \gTree{}, and wait for me to implement some ideas for speeding things
486
up!}.
487
 
488
<<fig=TRUE>>=
489
cellname <- function(i, j) {
490
  paste("cell", i, j, sep="")
491
}
492
 
493
splom.vpTree <- function(n) {
494
  vplist <- vector("list", n^2)
495
  for (i in 1:n)
496
    for (j in 1:n)
497
      vplist[[(i - 1)*n + j]] <- 
498
        viewport(layout.pos.row=i, layout.pos.col=j,
499
          name=cellname(i, j))
500
  vpTree(viewport(layout=grid.layout(n, n), name="cellgrid"),
501
    do.call("vpList", vplist))
502
}
503
 
504
cellpath <- function(i, j) {
505
  vpPath("cellgrid", cellname(i, j))
506
}
507
 
508
splom <- function(df, name=NULL, draw=TRUE) {
509
  n <- dim(df)[2]
510
  glist <- vector("list", n*n)
511
  for (i in 1:n)
512
    for (j in 1:n)
513
      if (i == j)
514
        glist[[(i - 1)*n + j]] <- textGrob(paste("diag", i, sep=""),
515
          gp=gpar(col="grey"), vp=cellpath(i, j))
516
      else if (j > i)
517
        glist[[(i - 1)*n + j]] <- textGrob(cellname(i, j),
518
	  name=cellname(i, j),
519
          gp=gpar(col="grey"), vp=cellpath(i, j))
520
      else
521
        glist[[(i - 1)*n + j]] <- splot(df[,j], df[,i], "", 
522
          name=paste("plot", i, j, sep=""), vp=cellpath(i, j),
523
          gp=gpar(cex=0.5), draw=FALSE)
524
  smg <- gTree(name=name, 
525
    childrenvp=splom.vpTree(n), 
526
    children=do.call("gList", glist))		
527
  if (draw)
528
    grid.draw(smg)
529
  smg
530
}
531
 
532
df <- data.frame(x=rnorm(10), y=rnorm(10), z=rnorm(10))
533
splom(df)
534
 
535
@
536
 
537
This \grob{} can be edited as usual:
538
 
539
<<>>=
540
splom(df)
541
grid.edit("plot21::xlab", label="", redraw=FALSE)
542
grid.edit("plot32::ylab", label="", redraw=FALSE)
543
grid.edit("plot21::xaxis", label=FALSE, redraw=FALSE)
544
grid.edit("plot32::yaxis", label=FALSE)
545
 
30260 murrell 546
<<splomgrob, eval=FALSE, echo=FALSE>>=
547
smg <- splom(df, draw=FALSE)
548
 
28502 murrell 549
<<fig=TRUE, echo=FALSE, results=hide>>=
550
<<splomgrob>>
551
smg <- editGrob(smg, gPath="plot21::xaxis", label=FALSE)
552
smg <- editGrob(smg, gPath="plot21::xlab", label="")
553
smg <- editGrob(smg, gPath="plot32::yaxis", label=FALSE)
554
smg <- editGrob(smg, gPath="plot32::ylab", label="")
555
grid.draw(smg)
556
 
557
@
558
 
559
But of more interest, because this is a \grob{}, is the {\it programmatic}
560
interface.  With a \grob{} (as opposed to a function) it is possible
561
to modify the description of what is being drawn via an API (as opposed
562
to having to edit the original code).  In  the following, we
563
remove one of the ``spare'' cell labels and put in its place the
564
current date.
565
 
566
<<>>=
567
splom(df, name="splom")
568
grid.remove("cell12")
569
grid.add("splom", textGrob(date(), name="date", gp=gpar(fontface="italic"),
570
  vp="cellgrid::cell12"))
571
 
572
<<fig=TRUE, echo=FALSE, results=hide>>=
573
<<splomgrob>>
574
smg <- removeGrob(smg, "cell12")
575
smg <- addGrob(smg, textGrob(date(), name="date", gp=gpar(fontface="italic"),
576
  vp="cellgrid::cell12"))
577
grid.draw(smg)
578
 
579
@
580
 
581
With the date added as a component of the scatterplot matrix, it is
582
saved as part of the matrix.  The next sequence saves the scatterplot
583
matrix, loads it again, extracts the bottom-left plot and the date 
584
and just draws those two objects together.
585
 
586
<<>>=
587
splom(df, name="splom")
588
grid.remove("cell12")
589
grid.add("splom", textGrob(date(), name="date", gp=gpar(fontface="italic"),
590
  vp="cellgrid::cell12"))
591
smg <- grid.get("splom")
592
save(smg, file="splom.RData")
593
load("splom.RData")
594
plot <- getGrob(smg, "plot31")
595
date <- getGrob(smg, "date")
596
plot <- editGrob(plot, vp=NULL, gp=gpar(cex=1))
597
date <- editGrob(date, y=unit(1, "npc") - unit(1, "lines"), vp=NULL)
598
grid.newpage()
599
grid.draw(plot)
600
grid.draw(date)
601
 
602
<<fig=TRUE, echo=FALSE, results=hide>>=
603
<<splomgrob>>
604
smg <- removeGrob(smg, "cell12")
605
smg <- addGrob(smg, textGrob(date(), name="date", gp=gpar(fontface="italic"),
606
  vp="cellgrid::cell12"))
607
save(smg, file="splom.RData")
608
load("splom.RData")
609
plot <- getGrob(smg, "plot31")
610
date <- getGrob(smg, "date")
611
plot <- editGrob(plot, vp=NULL, gp=gpar(cex=1))
612
date <- editGrob(date, y=unit(1, "npc") - unit(1, "lines"), vp=NULL)
613
grid.draw(plot)
614
grid.draw(date)
615
 
616
@
617
 
618
All of this may seem a bit irrelevant to interactive use, but it
619
does provide a basis for creating an editable plot interface like 
28707 murrell 620
M Kondrin's {\tt Rgrace} package.
28502 murrell 621
 
30260 murrell 622
% Start a new page
623
% Not echoed, not evaluated
624
% ONLY here for checkVignettes so that all output doesn't
625
% end up on one enormous page
626
 
627
<<eval=FALSE, echo=FALSE>>=
628
grid.newpage()
629
 
630
@
28502 murrell 631
\end{document}