[R] grob questions

Paul Murrell p.murrell at auckland.ac.nz
Tue Oct 4 21:35:11 CEST 2005


Hi


Gabor Grothendieck wrote:
> If I run the following example from:
> http://www.stat.auckland.ac.nz/~paul/grid/doc/grobs.pdf
> 
> 
>>grid.newpage()
>>pushViewport(viewport(w = 0.5, h = 0.5))
>>myplot <- gTree(name = "myplot", children = gList(rectGrob(name = "box",
> 
> + gp = gpar(col = "grey")), xaxisGrob(name = "xaxis")))
> 
>>grid.draw(myplot)
>>grid.edit("myplot::xaxis", at = 1:10/11)
>>grid.edit("myplot::xaxis::labels", label = round(1:10/11, 2))
>>grid.edit("myplot::xaxis::labels", y = unit(-1, "lines"))
> 
> 
> then
> 
> 
>>str(myplot$children$xaxis)
> 
> 
> lists 'at' but not the 'labels'.
> 
> yet if I do this then the labels are listed:
> 
> 
>>xx <- xaxisGrob(name = "myX", at = 1:10)
>>childNames(xx)
> 
> [1] "major"  "ticks"  "labels"
> 
> 
> 1. How do I get to labels in the first case?


First, if the xaxisGrob has at=NULL then it calculates its tick-marks at 
drawing time (based on the viewport it gets drawn in).  So it does not 
store any labels (it doesn't know what they are until it gets drawn). If 
you specify a non-NULL 'at' then the axis creates labels.

Second, grid grobs are standard R objects (copy-on-modify) so the object 
'myplot' is not the same object that is being modified by the calls to 
grid.edit().  grid.edit() destructively modifies a copy of the grob that 
grid has stored on its display list;  you refer to the appropriate grob 
via its name (not via an R object).  By comparison, editGrob() takes a 
grob and returns a modified copy of the grob, for example ...

myplot <- editGrob(myplot, at = 1:10/11)

The naming scheme for grid functions is:  grid.<*>() functions for 
producing or working with graphical *output* (drawing grobs or working 
with grobs that have been drawn) and <*>Grob() functions for working 
(off-screen) with grobs.


> 2. Is there a better construct than myplot$children$xaxis?


The getGrob(), editGrob(), etc functions for working with grobs (and 
sub-grobs) off-screen and grid.get(), grid.edit(), etc for working with 
graphical output.

"Recent changes in grid graphics". R News, 5(1):12-20, May 2005 
describes this some more.

Paul
-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/




More information about the R-help mailing list