[R] splom pairs and groups argument

deepayan.sarkar at gmail.com deepayan.sarkar at gmail.com
Tue Oct 2 12:58:23 CEST 2007


On 10/2/07, GOUACHE David <D.GOUACHE at arvalisinstitutduvegetal.fr> wrote:
> Hello,
>
> I'm trying to pull off a certain graph using splom, and can't quite get my
> panel functions right.
> Basically, the equivalent using pairs would be something like this (using
> iris data set as an example):
>
> panel.corval <- function(x, y, digits=2, prefix="", cex.cor,col,pch)
>      {
>          usr <- par("usr"); on.exit(par(usr))
>          par(usr = c(0, 1, 0, 1))
>          r <- abs(cor(x, y,use="complete.obs"))
>          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 )
>      }
> pairs(iris[,1:4], lower.panel=panel.smooth,
> upper.panel=panel.corval,col=rainbow(nlevels(iris$Species))[iris$Species],pch=19)

Here's a more or less direct translation:


panel.corval2 <- function(x, y, digits=2, prefix="", cex.cor, ...)
{
    require(grid)
    r <- abs(cor(x, y, use = "complete.obs"))
    txt <- format(c(r, 0.123456789), digits=digits)[1]
    txt <- paste(prefix, txt, sep="")
    if (missing(cex.cor)) cex.cor <- 10 / nchar(txt)
    grid.text(txt, 0.5, 0.5, gp = gpar(cex = cex.cor))
}


splom(iris[1:4], groups = iris$Species, pch = 16,
      lower.panel = function(...) {
          panel.xyplot(...)
          panel.loess(..., col = 1, lwd = 3)
      },
      upper.panel = panel.corval2)


> My goals are:
> 1) to have in one panel (above or below the diagonal) the points themselves
> with a smoothing line over all the points and in the other the (absolute)
> value of the correlation coefficient, so as to get an overall idea of my
> correlations
> 2) to be able to identify points depending on levels of one or more factors
> (hence the groups argument that is giving me a hard time in writing a
> succesfull panel function)
> 3) I want to do this in splom as I want to build several scatterplot
> matrices subsetting my data over the levels of another factor...
>
> Could anyone please help me out with this ?
>
> Also, while trying to set up an example, I landed upon this behavior I
> couldn't quite make sense of:
>
> panel.corval <- function(x, y, digits=2, prefix="", cex.cor)
>      {
>          usr <- par("usr"); on.exit(par(usr))
>          par(usr = c(0, 1, 0, 1))
>          r <- abs(cor(x, y,use="complete.obs"))
>          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 )
>      }
> pairs(iris[,1:4], lower.panel=panel.smooth,
> upper.panel=panel.corval,col=rainbow(nlevels(iris$Species))[iris$Species],pch=19)
>
> Erreur dans lower.panel(...) : unused argument(s) (col = c("#FF0000",
> "#FF0000", "#FF0000",
>
> I can't understand why not specifying col and pch arguments in the function
> I use for upper.panel (which dosen't at all need them in this case) is a
> problem.

upper.panel will be called with pch, col, etc., even if they don't
``need'' them. Having a ... argument should be enough to capture
these.

> It's all the more confusing that the error message says the problem
> is with lower.panel... If anyone has a clue as to what's happening, please
> tell.

pairs.default is renaming things internally; hint: try

pairs(iris[,1:4], upper.panel=panel.corval,pch=19, row1attop=FALSE)

-Deepayan



More information about the R-help mailing list