Skip to content
Prev 277011 / 398506 Next

grid.pack and grid.frame bewilder me

Hi
On 11/11/2011 7:00 a.m., cheung zihong wrote:
The problem is that the "cell" that you are packing the rectangle into 
is getting its own size from the rectangle.  That makes sense if the 
rectangle has an absolute size (e.g., unit(1, "inches")), but if the 
size of the rectangle is relative it creates a bit of havoc.  The 
conversation between the cell and the frame goes a bit like this ...

frame to cell:  how big are you?
cell to frame:  I dunno, I'll ask my rectangle
cell to rectangle:  how big are you?
rectangle to cell:  I'm half as big as my parent
cell to frame:  I'm half as big as my parent

Now the cell is half as big as the frame and the rectangle is half as 
big as the cell!

You can get what I think you want like this (tell the cell how big it is 
within the frame, rather than leaving it to ask its rectangle) ...

    library(grid)
    grid.frame(name="frame1")
    grid.pack("frame1",
              width=unit(1, "null"),
              height=unit(1, "null"),
              rectGrob(width=unit(0.5,"npc"),
                       height=unit(0.5,"npc"), gp=gpar(col="red")))

No, that's not the greatest software design in history, but judging from 
your next question I'm not sure that you need to use frames and packing. 
  It is more often the case that you can get what you want by using 
viewports and layouts directly.

Hope that helps.

Paul