[R] Adding text (coefts) to pairs panels

Mark Van De Vyver mvdv at spamcop.net
Wed Mar 3 04:10:20 CET 2004


Hi,
First of all, thanks for the efforts of all the developers and contributors
- I'm very new to R and at the moment am just using it to create some
graphics, but it seems to be quite powerful.
I've googled the archives and wasn't able to find anyhting that dealt with
this problem, so would appreciate any suggestions/tips.

In a pairs plot I'd like to plot a linear regression line in each panel and,
also in each panel, I'd like to have some text in the panel showing the
coefts, etc.
I'm able to get the regressions line plotted, but not the text/coefficients.
Using one of the R examples, example(pairs), I have got this far:

panel.myfitline<-function(x, y, digits=2, prefix="", cex.cor, ...)
{
    res<-panel.smooth(x,y, col.smooth="blue", ...)
    reg <- coef(lm(y ~ x))
    abline(coef=reg,untf=F)
    const<-format(reg[1], trim = FALSE, digits = NULL, nsmall = 0, justify =
"left", ...)
    const<-paste(prefix, const, sep="")
    slope<-format(reg[2], trim = FALSE, digits = NULL, nsmall = 0, justify =
"left", ...)
    slope<-paste(prefix, slope, sep="")
    if(missing(cex.cor)) cex <- 0.8/strwidth(const)
    text(0.1, 0.5, const, cex=cex)
    if(missing(cex.cor)) cex <- 0.8/strwidth(slope)
    text(0.5, 0.1, slope, cex=cex)
}
pairs(USJudgeRatings[1:5], lower.panel=panel.smooth, diag.panel=panel.hist,
upper.panel=panel.myfitline)

Mark Van De Vyver
PhD
Lecturer
Finance Discipline
School of Business
Faculty of Economics and Business
Economics & Business Building H69
The University of Sydney  
Sydney  NSW  2006  Australia
Telephone: +61 2 9351-6452
Fax: +61 2 9351-6461
Mobile: 0428 281407

mailto:mvdv at spamcop.net
http:\\www.econ.usyd.edu.au




More information about the R-help mailing list