[R] Linear line on pairs plot

Frede Aakmann Tøgersen frtog at vestas.com
Fri Apr 25 12:57:50 CEST 2014


Hi

Have a look on how panel.smooth is defined:

panel.smooth
function (x, y, col = par("col"), bg = NA, pch = par("pch"), 
    cex = 1, col.smooth = "red", span = 2/3, iter = 3, ...) 
{
    points(x, y, pch = pch, col = col, bg = bg, cex = cex)
    ok <- is.finite(x) & is.finite(y)
    if (any(ok)) 
        lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), 
            col = col.smooth, ...)
}
<bytecode: 0x000000000bfb2ce0>
<environment: namespace:graphics>


And change that to something like this:


panel.regression <- function (x, y, col = par("col"), bg = NA, pch = par("pch"), 
    cex = 1, col.regres = "red", ...) 
{
    points(x, y, pch = pch, col = col, bg = bg, cex = cex)
    ok <- is.finite(x) & is.finite(y)
    if (any(ok)) 
        abline(stats::lm(y[ok] ~ x[ok]), col = col.regres, ...)
}


Substitute panel.smooth with panel.regression in your pairs.panel() function and you have it.



Yours sincerely / Med venlig hilsen


Frede Aakmann Tøgersen
Specialist, M.Sc., Ph.D.
Plant Performance & Modeling

Technology & Service Solutions
T +45 9730 5135
M +45 2547 6050
frtog at vestas.com
http://www.vestas.com

Company reg. name: Vestas Wind Systems A/S
This e-mail is subject to our e-mail disclaimer statement.
Please refer to www.vestas.com/legal/notice
If you have received this e-mail in error please contact the sender. 

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Shane Carey
> Sent: 25. april 2014 12:26
> To: r-help at r-project.org
> Subject: [R] Linear line on pairs plot
> 
> Hi,
> 
> Im trying to plot a linear line on the scatter plot using the pairs()
> function. At the moment the line is non linear. However, I want a linear
> line and the associated R value.
> 
> Here is my current code:
> 
> panel.cor.scale <- function(x, y, digits=2, prefix="", cex.cor)
> {
>   usr <- par("usr"); on.exit(par(usr))
>   par(usr = c(0, 1, 0, 1))
>   r = (cor(x, y,use="pairwise"))
>   txt <- format(c(r, 0.123456789), digits=digits)[1]
>   txt <- paste(prefix, txt, sep="")
>   if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
>   text(0.5, 0.5, txt, cex = cex * abs(r))
> }
> 
> 
> panel.cor <- function(x, y, digits=2, prefix="", cex.cor)
> {
>   usr <- par("usr"); on.exit(par(usr))
>   par(usr = c(0, 1, 0, 1))
>   r = (cor(x, y,use="pairwise"))
>   txt <- format(c(r, 0.123456789), digits=digits)[1]
>   txt <- paste(prefix, txt, sep="")
>   if(missing(cex.cor)) cex <- 0.8/strwidth(txt)
>   text(0.5, 0.5, txt, cex = cex )
> }
> 
> 
> panel.hist <- function(x, ...)
> {
>   usr <- par("usr"); on.exit(par(usr))
>   par(usr = c(usr[1:2], 0, 1.5) )
>   h <- hist(x, plot = FALSE)
>   breaks <- h$breaks; nB <- length(breaks)
>   y <- h$counts; y <- y/max(y)
>   rect(breaks[-nB], 0, breaks[-1], y, col="cyan", ...)
> }
> 
> 
> pairs.panels <- function (x,y,smooth=TRUE,scale=FALSE)
> {if (smooth ){
>   if (scale) {
> 
> pairs(x,diag.panel=panel.hist,upper.panel=panel.cor.scale,lower.panel=pane
> l.smooth)
>   }
>   else
> {pairs(x,diag.panel=panel.hist,upper.panel=panel.cor,lower.panel=panel.sm
> ooth)
>   } #else
> {pairs(x,diag.panel=panel.hist,upper.panel=panel.cor,lower.panel=panel.sm
> ooth)
> }
>  else #smooth is not true
>  { if (scale) {pairs(x,diag.panel=panel.hist,upper.panel=panel.cor.scale)
>  } else {pairs(x,diag.panel=panel.hist,upper.panel=panel.cor) }
>  } #end of else (smooth)
> } #end of function
> 
> pairs.panels(iris[1:4])
> 
> Thanks
> 
> --
> Shane
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list