[R] panel.text and saving to pdf

Deepayan Sarkar deepayan.sarkar at gmail.com
Fri Jun 26 19:14:21 CEST 2009


On Fri, Jun 26, 2009 at 1:53 AM, Dieter
Menne<dieter.menne at menne-biomed.de> wrote:
> Willem Vervoort <w.vervoort <at> usyd.edu.au> writes:
>
>> I am not sure what I am doing wrong, but I have some unexplained behaviour
> when saving a lattice graph
>> including text to a pdf file. The text seems to move around. It must have
> something to do with the way
>> coordinates are set in devices other than jpg.
>>
>> today <- format(Sys.Date(),"%Y%m%d")
>>
>> x <- runif(500)
>> y <- rnorm(500)
>>
>> foo <- data.frame(x = x, y = y, z = rep(c("a","b"),250))
>> require(lattice)
>>
>> xyplot(x~y|z,data=foo)
>> panel.text(370,470,"silly graph",cex=1.2,font=2)
>
> Take this plot, resize its window: you will note that the "silly graph" moves
> relative to the graphics. The same is true for the higher-resolution pdf output.

Yes, and that this even works at all is an accident of implementation.
Once a lattice graph is drawn, the only officially supported way to
add to it is to use trellis.focus() (or it's underlying grid
equivalent downViewport()), e.g.;

xyplot(1:10 ~ 1:10)
trellis.focus("toplevel") ## has coordinate system [0,1] x [0,1]
panel.text(0.5, 0.2, "silly graph", cex = 1.2, font = 2)
trellis.unfocus()

If you know beforhand what you want to add, another option is to use
the page argument:

xyplot(1:10 ~ 1:10,
       page = function(n) {
           panel.text(0.5, 0.2, "silly graph", cex = 1.2, font = 2)
       })

-Deepayan




More information about the R-help mailing list