[R] Adding text (coefts) to pairs panels

Liaw, Andy andy_liaw at merck.com
Wed Mar 3 16:06:32 CET 2004


You need to figure out the "user coordinates" for each panel, so you can put
the text within the range that's being plotted.  Here's an example:

panel.myfitline <- function(x, y, digits=2, prefix="", cex.cor, ...)
{
    usr <- par("usr")
    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(.95*usr[2], usr[3] +.05*(usr[4]-usr[3]), const, cex=cex)
    if(missing(cex.cor)) cex <- 0.8/strwidth(slope)
    text(usr[1] + .1*(usr[2]-usr[1]), .98*usr[4], slope, cex=cex)
}

HTH,
Andy

> From: Mark Van De Vyver
> 
> 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

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html



------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}




More information about the R-help mailing list