[R] Splom custom superpanels

Deepayan Sarkar deepayan.sarkar at gmail.com
Wed Aug 1 21:42:13 CEST 2007


On 8/1/07, Jonathan Williams <williams222 at llnl.gov> wrote:
> I thought one nice addition to a splom figure would be to have the
> scatterplots in the upper triangle and a color-coordinated
> correlation matrix on the bottom.  So I tried my hand at customizing
> panel.pairs(), and was rebuffed.  Many times.  Four hours of
> fruitless debugging later, I turn to you for help:
>
> panel.pairs(z=teststatfull[,6:12], pscales=0,
>         panel.subscripts=FALSE, subscripts=,
>         upper.panel=lattice.getOption("panel.splom"),
>         lower.panel=function(x1=panel.args$x, y1=panel.args$y,
>                 panel.args=trellis.panelArgs(), subscripts=1:dim(teststatfull)[1],...){
>                         panel.fill(col=brewer.pal(9,"RdBu")[round(cor(x1,y1)*4 + 5)])
>                         panel.text(mean(x1), mean(y1), round(cor(x1,y1),2),
>                         font=2)})
>
> This code is a bit above my level; I stole some tricks from examples
> I saw elsewhere, and while it looks over-clunky, it works.  Works, at
> least, in creating the superpanel: you can try it yourself by
> replacing z with a data frame or matrix of your choice and installing
> the lattice and RColorBrewer packages.  However, when I try to insert
> this into the splom function, it all goes to pot.  R scolds me for
> either missing subscripts, improper subscripts when I try to provide
> them, or missing data in the panel function, no matter how I define
> one (or don't).  Can anyone recommend a solution or show me how to
> make my superpanel function more palatable to splom?

I haven't figured out how to run your code, even after replacing 'z',
but I think what you are making things more complicated than they have
to be:

library(lattice)
library(RColorBrewer)

foo <- mtcars[c(1:6)]

splom(foo,
      upper.panel = panel.splom,
      lower.panel = function(x, y, ...) {
          panel.fill(col = brewer.pal(9, "RdBu")[ round(cor(x, y) * 4 + 5)])
          panel.text(mean(x), mean(y), round(cor(x, y),2), font=2)
      })

Of course, you can, if you really want to, make a custom superpanel
function to do this:

my.panel.pairs <- function(..., lower.panel)
{
    my.lower.panel <-
        function(x, y, ...) {
            panel.fill(col = brewer.pal(9, "RdBu")[ round(cor(x, y) * 4 + 5)])
            panel.text(mean(x), mean(y), round(cor(x, y),2), font=2)
        }
    panel.pairs(..., lower.panel = my.lower.panel)
}

splom(foo, superpanel = my.panel.pairs)

-Deepayan



More information about the R-help mailing list