[R] Gridbase basic question

Sean Davis sdavis2 at mail.nih.gov
Tue Sep 28 12:13:03 CEST 2004


Paul,

Thanks for the extensive and clear explanation.  The reason I started  
with grid is that I am hoping to use a combination of segments,  
rectangles, and text to describe relatively complex (genes) objects  
that relate to the x-axis in a plot.  I do appreciate the insight from  
you and others that the added complexity of grid may not be necessary.

Sean

On Sep 27, 2004, at 5:57 PM, Paul Murrell wrote:

> Hi
>
>
> Sean Davis wrote:
>> All,
>> I have a simple plot(x,y) and I would like to then insert rectangles  
>> of  some length (in native coordinates) and height fixed to 0.5 in  
>> native  coordinates.  I can't quite get the code right to do this.   
>> Can anyone  give me a quick example of how to do this?  I looked the  
>> gridBase index  and the tutorial (from R-news?) but just haven't  
>> gotten it down yet.
>>  > plot(1:10,1:10)
>>  > par(new=T);vps <- baseViewports()
>>  > pushViewport(vps$inner,vps$figure,vps$plot)
>> viewport[GRID.VP.28]
>
>
> At this point you are within a viewport which has x- and y-scales  
> corresponding to the plot(1:10, 1:10).
>
>
>>  > pushViewport(viewport(x=unit(1,"native"),y=unit(2,"native")))
>> viewport[GRID.VP.29]
>
>
> You have just created a new viewport at location (1, 2) in the plot,  
> but  the scales on this new viewport are the default (0, 1).  i.e.,  
> you are now in a completely different coordinate system.  Also, this  
> new viewport is as wide and as high as the plot region -- for example,  
> it extends well beyong the left edge of the window/page.
>
>
>> grid.rect(height=unit(0.5,"native"),width=unit(1.5,"native"),just='bot 
>> to m')
>
>
> This draws a rectangle half as high as the current viewport and 1.5  
> times as wide (the native scale in the current viewport is (0, 1) in  
> both dimensions).  Importantly, the "native" coordinate systems you  
> are referring to no longer correspond to the scales on the plot.
>
>
>> This draws a very large rectangle going from 2 to 7 (y) and to 8 (x).
>
>
> Three things:
>
> (i) If drawing rectangles relative to the current "native" (or user)  
> coordinates is all you want to do then you could just use rect() and  
> ignore gridBase altogether.  For example, ...
>
> x <- sample(1:10, 10)
> y <- 1:10
> w <- runif(10)
> h <- 0.5
>
> plot(1:10,1:10)
> rect(x - w/2, y - h/2, x + w/2, y + h/2)
>
>
> (ii) Using grid and gridBase, the above example becomes ...
>
> plot(1:10,1:10)
> par(new=T);vps <- baseViewports()
> pushViewport(vps$inner,vps$figure,vps$plot)
> grid.rect(x=x, y=y, width=w, height=h, default.units="native")
> popViewport(3)
>
> ... but as mentioned, this is like using a sledge hammer to kill a cat  
> or whatever the expression is.
>
> (iii) There would be justification in using grid and gridBase if you  
> want to draw more than just a rectangle, especially if you want to use  
> coordinates other than native.  Here's a trivial example (adds fixed  
> size "whiskers" to the corners of the rectangles) ...
>
> plot(1:10,1:10)
> par(new=T);vps <- baseViewports()
> pushViewport(vps$inner,vps$figure,vps$plot)
> for (i in 1:10) {
>   pushViewport(viewport(x=x[i], y=y[i], width=w[i], height=h,
>                         default.units="native"))
>   grid.rect()
>   grid.segments(0, 0, unit(-1, "mm"), unit(-1, "mm"))
>   grid.segments(0, 1, unit(-1, "mm"),
>                 unit(1, "npc") + unit(1, "mm"))
>   grid.segments(1, 1,
>                 unit(1, "npc") + unit(1, "mm"),
>                 unit(1, "npc") + unit(1, "mm"))
>   grid.segments(1, 0,
>                 unit(1, "npc") + unit(1, "mm"),
>                 unit(-1, "mm"))
>   popViewport()
> }
>
> ... (but pushing a viewport per data point like this is a LOT slower).
>
> Hope that helps
>
> 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