[R] Re creating Procrustes Plot in Lattice

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Jul 30 21:14:15 CEST 2008


On Wed, 2008-07-30 at 17:37 +0100, Gavin Simpson wrote:
> On Wed, 2008-07-30 at 04:59 -0700, Andrewjohnclose wrote:
> > Hi, I have been trying to create a function to generate a Procrustes plot,
> > generated from package "vegan"
> > in lattice.
<snip />
> > 
> > I can generate an empty plot, but not much else - hope someone can help and
> > point out the obvious!
> > 
> > Kind regards
> > 
> > Andrew
> 
> The obvious thing is that whole thing is wrong! ;-)
> 
> What's wrong with the standard plot?
<snip />

OK, the above question aside, how does the following set of functions
suit?

xyplot.procrustes <- function(object, choices = 1:2, xlab, ylab, ...) {
    dat <- data.frame(x = object$Yrot[, choices[1]],
                      y = object$Yrot[, choices[2]])
    if(missing(xlab))
        xlab <- paste("Dimension", choices[1])
    if(missing(ylab))
        ylab <- paste("Dimension", choices[2])
    xyplot(y ~ x, data = dat, type = "p",
           rotation = object$rotation,
           X = object$X,
           choices = choices,
           aspect = "iso",
           xlab = xlab, ylab = ylab,
           panel = panel.procrustes,
           prepanel = prepanel.procrustes, ...)
}

panel.procrustes <- function(x, y, rotation, X, choices,
                             ar.col = "blue", length = 0.05, ...) {
    panel.abline(h = 0, lty = "dashed")
    panel.abline(v = 0, lty = "dashed")
    rotation[1,1] <- min(rotation[1,1], 1)
    panel.abline(0, tan(acos(rotation[1, 1])), lty = "solid")
    panel.abline(0, 1/tan(acos(-rotation[1, 1])), lty = "solid")
    panel.points(x, y, ...)
    panel.arrows(x0 = x, y0 = y, x1 = X[, choices[1]], y1 = X[,choices[2]],
                 length = length, col = ar.col)
}

prepanel.procrustes <- function(x, y, X, choices, ...) {
    xlim <- range(x, X[, choices[1]])
    ylim <- range(y, X[, choices[2]])
    list(ylim = ylim, xlim = xlim)
}

## and an example
require(vegan)
require(lattice)
example(protest)

xyplot(vare.proc)

Note that this only reproduces the which = 1 plot from plot.procrustes.
And it is far from bulletproof as I haven't put in any checks, and I
probably haven't got the passing of graphics parameters sorted out or
even allow the level of customisation that you can get with
plot.procrustes, but it does reproduce the output of that function for
which = 1. (but all that despite the fact that this has only taken 20
minutes to cook up, plus a tube journey to consult Deepayan's Lattice
book for inspiration!)

I'll try to tart this up a bit and see about reproducing the
functionality of plot.procrustes which = 2 and then perhaps stick it in
vegan when I get a minute and can talk to Jari about making the function
fit in with the other ordination lattice functions currently in vegan.

HTH

G

> 
> G



More information about the R-help mailing list