[R] Lattice: how to draw some text outside the panel?

Paul Murrell p.murrell at auckland.ac.nz
Tue Jul 22 04:38:46 CEST 2003


Wladimir Eremeev wrote:
> Dear r-help
>    I draw plots with xyplot() function.
>    Each plot contains also a line of regression.
>    I want to write the trend value and its significance
>    (obtained with lm()) below each panel.
>    I use ltext() for this.
>    But the text is cut, when it comes outside a panel.
>    Moreover (obviously), it doesn't appear at all when its
>    coordinates are outside a panel.
>    Could you, please, be so kind to give me a direction to walk
>    in order to have a text written below a panel.

Lattice clips to the panel by default.  I think there is a plan to add a 
lattice argument to allow you to turn this clipping off in future versions.

In the meantime, there is a workaround.  The idea is to push a viewport 
bigger than the panel and set the clipping region using that larger 
viewport, then push another viewport which is the same as the original 
panel (but do not clip to that).  The following example shows what to do 
   (if you remove the push.viewport and pop.viewport bits the text is 
clipped to the panel):

      Depth <- equal.count(quakes$depth, number=8, overlap=.1)
      xyplot(lat ~ long | Depth, data = quakes,
             panel=function(x, y, ...) {
               panel.xyplot(x, y)

               # Set the clipping region four times larger
               xscale <- current.viewport()$xscale
               yscale <- current.viewport()$yscale
               push.viewport(viewport(width=2, height=2, clip=TRUE))
               push.viewport(viewport(width=.5, height=.5,
                                      xscale=xscale, yscale=yscale))

               ltext(rep(165, 4), seq(-10, -40, -10),
                     c("one", "two", "three", "four"))

               # Pop the extra viewports (VERY IMPORTANT!)

Hope that helps

Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz

More information about the R-help mailing list