[R] lattice grob

Paul Murrell p.murrell at auckland.ac.nz
Wed Mar 24 23:22:53 CET 2010


Hi

baptiste auguie wrote:
> Thanks Felix and Paul. I had overlooked grid.grabExpr, assuming that
> one had to draw on a device before grabbing the output.
> 
> Now I'm not sure if there's any difference between either solution,
> I'll go for the shortest.

As Felix pointed out, one possible problem with the grid.gradExpr() 
approach is that you get a copy of what was drawn on the current device, 
which may be dependent on things like how big the current device is, 
what fonts it uses, etc.

The drawDetails() approach means that 'lattice' will calculate what to 
draw every time you want to draw, so it should adapt to different device 
sizes and different device properties more gracefully.

On the other hand, the drawDetails() approach only records a VERY 
high-level description of what you are drawing (a 'lattice' object), so 
you cannot fiddle about with the low-level details of what you draw. 
For example,  following  ...

latticeGrob <- function(p, ...){
   grob(p=p, ..., cl="lattice")
}
drawDetails.lattice <- function(x, recording=FALSE){
   lattice:::plot.trellis(x$p, newpage=FALSE)
}
p1 <- xyplot(1:10 ~ 1:10)
g1 <- latticeGrob(p1)
grid.draw(g1)

... grid.ls() gives you ...

 > grid.ls()
GRID.lattice.53

... whereas the grid.gradExpr() approach records all of the bits and 
pieces of the drawing, e.g., following ...

p1 <- xyplot(1:10 ~ 1:10)
g1 <- grid.grabExpr(print(p1))
grid.draw(g1)

... grid.ls() gives you ...

 > grid.ls()
GRID.gTree.94
   GRID.rect.85
   plot1.xlab
   plot1.ylab
   GRID.segments.86
   GRID.segments.87
   GRID.text.88
   GRID.segments.89
   GRID.text.90
   GRID.segments.91
   GRID.points.92
   GRID.rect.93


So it's a bit of a trade-off.

Paul


> Best,
> 
> baptiste
> 
> On 22 March 2010 00:18, Felix Andrews <felix at nfrac.org> wrote:
>> What's wrong with using grid.grabExpr?
>>
>> p1 <- xyplot(1:10 ~ 1:10)
>> g1 <- grid.grabExpr(print(p1))
>>
>> I can imagine there would be potential problems to do with the
>> plot-time aspect and layout calculations...
>>
>>
>>
>> On 19 March 2010 21:51, baptiste auguie <baptiste.auguie at googlemail.com> wrote:
>>> Dear list,
>>>
>>> I'm trying to arrange various grid objects on a page using a
>>> frameGrob. It works fine with basic grobs (textGrob, gTree, etc.), and
>>> also with ggplot2 objects using the ggplotGrob() function. I am
>>> however stuck with lattice. As far as I understand, lattice produces a
>>> list of class trellis, which is eventually displayed using the
>>> plot.trellis method. I am not sure if/how one can convert this list
>>> into a high-level grob. I tried the following,
>>>
>>> latticeGrob <- function(p, ...){
>>>  grob(p=p, ..., cl="lattice")
>>> }
>>>
>>> drawDetails.lattice <- function(x, recording=FALSE){
>>>  lattice:::plot.trellis(x$p)
>>> }
>>>
>>> p1 <- xyplot(1:10 ~ 1:10)
>>> g1 <- latticeGrob(p1)
>>>
>>> grid.draw(g1) # works fine
>>>
>>> but,
>>>
>>> fg <- frameGrob(layout = grid.layout(1,1))
>>> fg <- placeGrob(fg, g1, row = 1, col = 1)
>>> grid.draw(fg)
>>>
>>> Error in UseMethod("depth") :
>>>  no applicable method for 'depth' applied to an object of class "NULL"
>>>
>>> Ideas are most welcome,
>>>
>>> Best regards,
>>>
>>> baptiste
>>>
>>>> sessionInfo()
>>> R version 2.10.1 RC (2009-12-06 r50690)
>>> i386-apple-darwin9.8.0
>>>
>>> locale:
>>> [1] en_GB.UTF-8/en_GB.UTF-8/C/C/en_GB.UTF-8/en_GB.UTF-8
>>>
>>> attached base packages:
>>> [1] grid      tools     stats     graphics  grDevices utils
>>> datasets  methods   base
>>>
>>> other attached packages:
>>> [1] ggplot2_0.8.7   digest_0.4.1    reshape_0.8.3   plyr_0.1.9
>>> proto_0.3-8     gridExtra_0.5   lattice_0.17-26 gtools_2.6.1
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>>
>> --
>> Felix Andrews / 安福立
>> Postdoctoral Fellow
>> Integrated Catchment Assessment and Management (iCAM) Centre
>> Fenner School of Environment and Society [Bldg 48a]
>> The Australian National University
>> Canberra ACT 0200 Australia
>> M: +61 410 400 963
>> T: + 61 2 6125 4670
>> E: felix.andrews at anu.edu.au
>> CRICOS Provider No. 00120C
>> --
>> http://www.neurofractal.org/felix/
>>
> 
> ______________________________________________
> 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