[R] navigating ggplot viewports

Paul Murrell p.murrell at auckland.ac.nz
Thu Jan 24 01:26:17 CET 2008


Hi


hadley wickham wrote:
> Hi Felix,
> 
> I'm a bit stumped too - it's been a while since I've played around
> with viewports in ggplot.  I have a feeling that this might be due to
> an old bug in grid that I thought had been fixed - you can't
> downViewport to a viewport underneath a frame grob.  I've cc'd Paul in
> the hope that he might have a suggestion.


This is a problem with frames;  the viewports that are created to 
position the cells within a frame get popped, so they *and any viewports 
below them* are not left hanging around for you to downViewport() to 
after the frame is drawn.

I am working on a fix to frames that would allow your code to run unchanged.

In the meantime, you may be able to use a workaround that makes use of a 
graphical-object approach.  grid.ls() shows that (in my session) the 
main plot is a grob (graphical object) called "plot.gTree.78" ...

 > grid.ls()
plot-surrounds
   GRID.cellGrob.83
     background
   GRID.cellGrob.84
     plot.gTree.78
       background
       guide.gTree.63
         background.rect.53
         minor-horizontal.segments.55
         minor-vertical.segments.57

<output clipped>

... and this is the guy that sets up the "panel_1_1" you were looking 
for ...

 > grid.ls(grid.get("plot.gTree.78"), view=TRUE, grob=FALSE)
layout
   strip_h_1_1
     1
   strip_v_1_1
     1
   axis_h_1_1
     1
   axis_v_1_1
     1
   panel_1_1
     2
layout
   panel_1_1
     2

<output clipped>

... so here's a trivial example of how to add another grob to that plot 
grob that will be drawn within the desired viewport ...

grid.add("plot.gTree.78",
          rectGrob(gp=gpar(col="red"), name="myBorder",
                   vp=vpPath("layout", "panel_1_1")))

... (that draws a red border around the plot region).  NOTE the vpPath 
to get the grob drawing in the right viewport, plus it is useful to give 
the new grob a name so that when things go haywire you can rip it back 
out with something like ...

grid.remove("myBorder")

If that doesn't help, look out for a fix in the next R version.

Paul


> Hadley
> 
> 2008/1/14 Felix Andrews <felix at nfrac.org>:
>> list(...)
>>
>> I can not get at the grid viewports in a ggplot2 plot.
>> I know there is supposed to be a viewport called "panel_1_1" but
>> downViewport() can't find it. Has the viewport been popped?
>> I had a quick look at the functions involved (eg ggplot_plot)
>> but there is no obvious problem there.
>>
>>> library(ggplot2)
>>> qplot(1:10, 1:10)
>>> current.viewport()
>> viewport[ROOT]
>>> downViewport("panel_1_1")
>> Error in downViewport.vpPath(vpPathDirect(name), strict, recording =
>> recording) :
>>   Viewport 'panel_1_1' was not found
>>> current.vpTree()
>> viewport[ROOT]->(viewport[GRID.VP.1])
>>> grid.ls(viewports=T, grob=F)
>> ROOT
>>   GRID.VP.2
>>     1
>>   GRID.VP.3
>>     layout
>>       strip_h_1_1
>>         1
>>       strip_v_1_1
>>         1
>>       axis_h_1_1
>>         1
>>       axis_v_1_1
>>         1
>>       panel_1_1
>>         2
>>     layout
>>       panel_1_1
>>         2
>>     layout
>>       axis_v_1_1
>>         left_axis
>>           labels
>>             1
>>           ticks
>>             2
>>         left_axis
>>           ticks
>>             2
>>         left_axis
>>           labels
>>             2
>>         2
>>     layout
>>       axis_h_1_1
>>         bottom_axis
>>           labels
>>             1
>>           ticks
>>             2
>>         bottom_axis
>>           ticks
>>             2
>>         bottom_axis
>>           labels
>>             2
>>         2
>>     layout
>>       panel_1_1
>>         2
>>     layout
>>       panel_1_1
>>         2
>>     layout
>>       panel_1_1
>>         2
>>     1
>>   GRID.VP.4
>>     1
>>   GRID.VP.5
>>     1
>>   GRID.VP.6
>>     1
>>
>>> sessionInfo()
>> R version 2.6.1 (2007-11-26)
>> i386-pc-mingw32
>>
>> locale:
>> LC_COLLATE=English_Australia.1252;LC_CTYPE=English_Australia.1252;LC_MONETARY=English_Australia.1252;LC_NUMERIC=C;LC_TIME=English_Australia.1252
>>
>> attached base packages:
>> [1] splines   grid      stats     graphics  grDevices utils     datasets
>> [8] methods   base
>>
>> other attached packages:
>> [1] ggplot2_0.5.7      colorspace_0.95    RColorBrewer_1.0-2 MASS_7.2-38
>> [5] proto_0.3-8        reshape_0.8.0
>>
>> loaded via a namespace (and not attached):
>> [1] tools_2.6.1
>>
>>
>> --
>> Felix Andrews / ???
>> PhD candidate
>> Integrated Catchment Assessment and Management Centre
>> The Fenner School of Environment and Society
>> The Australian National University (Building 48A), ACT 0200
>> Beijing Bag, Locked Bag 40, Kingston ACT 2604
>> http://www.neurofractal.org/felix/
>> 3358 543D AAC6 22C2 D336  80D9 360B 72DD 3E4C F5D8
>>
>>
>> ______________________________________________
>> 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