Re: [R] grob questions

From: Paul Murrell <>
Date: Wed 05 Oct 2005 - 05:35:11 EST


Gabor Grothendieck wrote:
> If I run the following example from:

>>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.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

> lists 'at' but not the 'labels'.
> yet if I do this then the labels are listed:
>>xx <- xaxisGrob(name = "myX", at = 1:10)

> [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.


Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392

______________________________________________ mailing list
PLEASE do read the posting guide!
Received on Wed Oct 05 05:40:48 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:40:35 EST