[R] Help using gPath

Paul Murrell paul at stat.auckland.ac.nz
Fri Aug 10 10:32:57 CEST 2007


Emilio Gagliardi wrote:
> Hi Paul,
> I'm sorry for not posting code, I wasn't sure if it would be helpful without
> the data...should I post the code and a sample of the data?  I will remember
> to do that next time!

It's important not only to post code, but also to make sure that other 
people can run it (i.e., include real data or have the code generate 
data or use one of R's predefined data sets).

Also, isn't this "next time" ? :)

>> grid.gedit(gPath("ylabel.text.382"), gp=gpar(fontsize=16))
> OK, I think my confusion comes from the notation that current.grobTree()
> produces and what strings are required in order to make changes to the
> underlying grobs.
> But, from what you've provided, it looks like I can access each grob with
> its unique name, regardless of which parent it is nested in...that helps

Yes.  By default, grid will search the tree of all grobs to find the 
name you provide.  You can even just provide part of the name and it 
will find partial matches (depending on argument settings).  On the 
other hand, by specifying a path that specified parent and child grobs, 
you can make sure you get exactly the grob you want.

>> like to remove the left border on the first panel.  I'd like to adjust the
>> I'd guess you'd have to remove the grob "background.rect.345" and then
>> draw in just the sides you want, which would require getting to the
>> right viewport, for which you'll need to study the viewport tree (see
>> current.vpTree())
> I did some digging into this and it seems pretty complicated, is there an
> example anywhere that makes sense to the beginner? The whole viewport grob
> relationship is not clear to me. So, accessing viewports and removing
> objects and drawing new ones is beyond me at this point. I can get my mind
> around your example below because I can see the object I want to modify in
> the viewer, and the code changes a property of that object, click enter, and
> bang the object changes.  When you start talking external pointers and
> finding viewports and pushing and popping grobs I just get lost. I found the
> viewports for the grobTree, it looks like this:

There's a book that provides a full explanation and the (basic) grid 
chapter is online (see 

> viewport[ROOT]->(viewport[layout]->(viewport[axis_h_1_1]->(viewport[bottom_axis]->(viewport[labels],
> viewport[ticks])),
> viewport[axis_h_1_2]->(viewport[bottom_axis]->(viewport[labels],
> viewport[ticks])),
> viewport[axis_v_1_1]->(viewport[left_axis]->(viewport[labels],
> viewport[ticks])), viewport[panel_1_1], viewport[panel_1_2],
> viewport[strip_h_1_1], viewport[strip_h_1_2], viewport[strip_v_1_1]))
> at that point I was like, ok, I'm done. :S

Yep, the facilities for investigating the viewport and grob tree are 
basically inadequate.  Based on some work Hadley did for ggplot, the 
development version of R has a slightly better tool called grid.ls() 
that can show how the grob tree and the viewport tree intertwine.  That 
would allow you to see which viewport each grob was drawn in, which 
would help you, for example, to know which viewport you had to go to to 
replace a rectangle you want to remove.

>> Something like ...
>> grid.gedit("geom_bar.rect", gp=gpar(col="green"))
>> Again, it would really help to have some code to run.
> My apologies, I thought the grobTree was sufficient in this case.  Thanks
> very much for your help.

Sorry to harp on about it, but if I had your code I could show you an 
example of how grid.ls() might help.

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

More information about the R-help mailing list