[R] grid.edit() for ggplot2

Paul Murrell p.murrell at auckland.ac.nz
Tue Jun 2 01:30:03 CEST 2009


Hi


baptiste auguie wrote:
> Dear all,
> 
> 
> I'm trying to access and modify grobs in a ggplot2 plot. The basic idea 
> for raw Grid objects I understand from Paul Murrell's R graphics book, 
> or this page of examples,
> 
> http://www.stat.auckland.ac.nz/~paul/grid/copygrob/copygrobs.R
> 
> However I can't figure out how to apply this to a ggplot (basically I 
> don't know how to write a syntactically correct gPath),
> 
> 
> p <- # minimal example
> qplot(0,0)+ annotate("text",0,0,label="test")
> 
> g <- # store the plot as a grob
> ggplotGrob(p)
> 
> # structure of the grob
> grid.ls(g) # rather large!
> 
> # find a particular grob in the gTree
> getGrob(g,"texts", grep = T)
> 
> 
> # next step, modify, say, the colour of these grobs
> grid.edit() # what do I put in here?


(Ignoring whether this is the most ggplot-ish way to achieve the end 
result ...)

Let's take a closer look at the part of the grob (gTree) that you have 
selected ...

 > grid.ls(getGrob(g,"texts", grep = T))
texts.gTree.5
   GRID.text.3

... (the "5" and the "3" in those names are likely to be different for 
you).  This is a gTree with one child, which is a text grob.  I don't 
know much about how the "texts" gTree works, so it's not clear how I 
should modify that to influence the text grob, but I do know that I can 
modify the 'gp' component of the text grob.  For example ...

 > grid.edit("GRID.text.3", gp=gpar(col="red"))

... and if I wanted to be SURE that I was getting the text grob beneath 
that "texts" gTree, then I could use a gPath like this ...

 > grid.edit(gPath("texts.gTree.5", "GRID.text.3"), gp=gpar(col="blue"))

... and if I wanted to use code that had a better chance of working in 
another session (when the "5" and "3" name suffixes are likely to be 
different), I could use something like ...

 > grid.edit(gPath("texts.gTree", "GRID.text"), grep=TRUE,
             gp=gpar(col="pink"))

... does that give you what you were looking for ?

Paul


> Thanks for any piece of advice,
> 
> baptiste
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

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