[R] Writing text in lattice graphics

Deepayan Sarkar deepayansarkar at yahoo.com
Tue Apr 23 18:33:34 CEST 2002


--- Luis Torgo <ltorgo at liacc.up.pt> wrote:
> I'm trying to obtain a set of regression lines obtained for different values 
> of a factor using lattice. I would like to add a string to each panel showing
> 
> the R^2 value of the respective line, but I'm having difficulties positioning
> 
> the text (namely on which coordinates to give to the "ltext" function).
> Here is what I'm using (I've used ?? for the things I would like you to be so
> 
> kind to help me on how to fill in).
> 
> >  xyplot(O2 ~ Am | Site, data = mydata, scales=list(x='free'),
>  panel=function(x,y,...) {
>   panel.xyplot(x,y,...)
>   panel.lmline(x,y,...)
>   ltext(??, ??, round(summary(lm(y ~ x))$r.squared,3))
>  })
> 
> I would like the R^2 values to appear in the top left corner of each of the 
> panels I get.
> I've tried fixed values for the "x", "y" parameters of "ltext", but as I'm 
> using different X scales for each panel things get messy...


This is where you can use the flexibility of grid. ltext calls grid.text with
the units fixed at the native x and y scales, but you can get all sorts of 
other coordinate systems if you use grid.text directly. For example,

grid.text(round(summary(lm(y ~ x))$r.squared,3), 0.1, 0.9)

should give you approximately what you want. See ?grid.text for details on 
how to customize this further.



> I'm using R 1.4.1 on a Windows 2000 machine.
> 
> Thank you in advance.
> 
> -- 
> Luis Torgo
>     FEP/LIACC, University of Porto   Phone : (+351) 22 607 88 30
>     Machine Learning Group           Fax   : (+351) 22 600 36 54
>     R. Campo Alegre, 823             email : ltorgo at liacc.up.pt
>     4150 PORTO   -  PORTUGAL         WWW   : http://www.liacc.up.pt/~ltorgo
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

__________________________________________________

Yahoo! Games - play chess, backgammon, pool and more

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list