[R] Help using gPath

Paul Murrell paul at stat.auckland.ac.nz
Sat Aug 11 12:26:50 CEST 2007


Hi


Emilio Gagliardi wrote:
> haha Paul,
> 
> 
>     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).
> 
> 
> Oh, I hadn't thought of using the predefined datasets, thats a good idea!
> 
>     Also, isn't this "next time" ? :)
> 
> 
> By next time I meant, when I ask a question in the future, I didn't 
> think you'd respond!
> 
> So here is some code!
> 
> library(reshape)
> library(ggplot2)
> 
> theme_t <- 
> list(grid.fill="white",grid.colour="lightgrey",background.colour="black",axis.colour="dimgrey") 
> 
> ggtheme(theme_t)
> 
> grp <- 
> c(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3)
> time <- 
> c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2) 
> 
> cc <- 
> c(0.7271,0.7563,0.6979,0.8208,0.7521,0.7875,0.7563,0.7771,0.8208,0.7938,0.8083,0.7188,0.7521,0.7854,0.7979,0.7583,0.7646,0.6938,0.6813,0.7708,0.7375,0.8104,0.8104,0.7792,0.7833,0.8083,0.8021,0.7313,0.7958,0.7021 
> ,0.8167,0.8167,0.7583,0.7167,0.6563,0.6896,0.7333,0.8208,0.7396,0.8063,0.7083,0.6708,0.7292,0.7646,0.7667,0.775,0.8021,0.8125,0.7646,0.6917,0.7458,0.7833,0.7396,0.7229,0.7708,0.7729,0.8083,0.7771,0.6854,0.8417,0.7667,0.7063 
> ,0.75,0.7813,0.8271,0.7896,0.7979,0.625,0.7938,0.7583,0.7396,0.7583,0.7938,0.7333,0.7875,0.8146)
> 
> data <- as.data.frame(cbind(time,grp,cc))
> data$grp <- factor(data$grp,labels=c("Group A","Group B"))
> data$time <- factor(data$time,labels=c("Pre-test","Post-test"))
> boxplot <- qplot(grp, cc, data=data, geom="boxplot", 
> orientation="horizontal", ylim=c(0.5,1), main="Hello World!", 
> xlab="Label X", ylab="Label Y", facets=.~time, colour="red", size=2)
> boxplot + geom_jitter(aes(colour="steelblue")) + scale_colour_identity() 
> + scale_size_identity()
> grid.gedit("ylabel", gp=gpar(fontsize=16))


Great.  Thanks.  Some example code of my own below ...


>     There's a book that provides a full explanation and the (basic) grid
>     chapter is online (see
>     http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html
>     <http://www.stat.auckland.ac.nz/~paul/RGraphics/rgraphics.html>)
> 
> 
> Awesome, I'll check that out.
> 
>     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.
> 
> 
> okie dokie, I'm ready to be amazed! hehe.


Or perhaps amused ...

Here's a partial extract from a sample session after running your code
(NOTE this is using the development version of R;  grid.ls() does not 
exist in R 2.5.1 or earlier):

Inspect the grob tree with grid.ls() (similar to Hadley's 
current.grobTree(), but with different formatting) ...

 > grid.ls()
plot-surrounds
   GRID.cellGrob.118
     background
   GRID.cellGrob.119
     plot.gTree.113
       background
       guide.gTree.90
         background.rect.80
         minor-horizontal.segments.82
         minor-vertical.segments.84
# OUTPUT TRUNCATED

... It is not necessarily obvious which grob is which,
but a little trial and error (e.g., grid.edit() to change
the colour of a grob) shows that the border on the first
panel is 'guide.rect.92', which is a child of 'plot.gTree.113'
(NOTE the numbers come from a fresh R session).

Use grid.get() to grab that gTree and inspect that
further using grid.ls(), this time also showing the
viewports involved ...

 > grid.ls(grid.get("plot.gTree.113"), viewports=TRUE, fullNames=TRUE)
gTree[plot.gTree.113]
   viewport[layout]
     viewport[strip_h_1_1]
       upViewport[1]
     viewport[strip_h_1_2]
       upViewport[1]
     viewport[strip_v_1_1]
       upViewport[1]
     viewport[axis_h_1_1]
       upViewport[1]
     viewport[axis_h_1_2]
       upViewport[1]
     viewport[axis_v_1_1]
       upViewport[1]
     viewport[panel_1_1]
       upViewport[1]
     viewport[panel_1_2]
       upViewport[2]
   rect[background]
   downViewport[layout]
     downViewport[panel_1_1]
       gTree[guide.gTree.90]
         rect[background.rect.80]
         segments[minor-horizontal.segments.82]
# OUTPUT TRUNCATED

 > grid.ls(grid.get("plot.gTree.113"), viewports=TRUE, 
print=grobPathListing)
                  |plot.gTree.113
                  |plot.gTree.113::background
layout::panel_1_1|plot.gTree.113::guide.gTree.90
layout::panel_1_1|plot.gTree.113::guide.gTree.90::background.rect.80
layout::panel_1_1|plot.gTree.113::guide.gTree.90::minor-horizontal.segments.82
layout::panel_1_1|plot.gTree.113::guide.gTree.90::minor-vertical.segments.84
# OUTPUT TRUNCATED

... which shows that 'guide.rect.92' is drawn within the
viewport 'panel_1_1' (which is within the viewport called 'layout').

(The remaining code should work for you in your version of R;  it
is just grid.ls() that is new.)

Remove the original border rect, ...

 > grid.remove("guide.rect.92", global=TRUE)

... (need global=TRUE because the border appears twice as a child
of 'plot.gTree.113' [not sure why that is]) then add some lines that
only draw the top, right, and bottom borders ...

 > grid.add("plot.gTree.113",
            linesGrob(c(0, 1, 1, 0), c(1, 1, 0, 0),
                      gp=gpar(col="green"),
                      vp=vpPath("layout", "panel_1_1")))

... (I drew the new lines green so that they are easy to see).
NOTE that in order to put the new lines in the same "place" as
the original border, the new lines are added as children of the
gTree 'plot.gTree.113' and they have a vpPath to make sure
they get drawn in the right viewport within that gTree.

What would probably be ideal would be a graphical interface to the
grid.ls()-type information (something like an object explorer) that
would make it easier to see which object is which and also make it
easier to add and remove objects.  A nice student project perhaps :)

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