Skip to content

plotting issues with PCA

2 messages · Andrew Halford, Gavin Simpson

#
On Mon, 2011-10-17 at 21:02 +1000, Andrew Halford wrote:
ordiellipse() doesn't work the way you think it does - it can only take
a single colour at a time. Therefore you need to do /n/ calls to
ordiellipse() to draw /n/ ellipses.

Here is a *reproducible* example, taken from ?ordiellipse:

require(vegan)
data(dune)
data(dune.env)
mod <- cca(dune ~ Management, dune.env)
plot(mod, type="n", display = "sites")
text(mod, display="sites", labels = as.character(Management))

## vector of colours
cols <- c("blue","red","darkgreen","grey70")

## add ellipses
with(dune.env, ordiellipse(mod, Management, kind="se", conf=0.95,
                           lwd=2, col=cols[1], show.groups = "BF"))
with(dune.env, ordiellipse(mod, Management, kind="se", conf=0.95,
                           lwd=2, col=cols[2], show.groups = "HF"))
with(dune.env, ordiellipse(mod, Management, kind="se", conf=0.95,
                           lwd=2, col=cols[3], show.groups = "NM"))
with(dune.env, ordiellipse(mod, Management, kind="se", conf=0.95,
                           lwd=2, col=cols[4], show.groups = "SF"))

We could automate this a bit:

## set up plotting region
plot(mod, type="n", display = "sites")
text(mod, display="sites", labels = as.character(Management))

## get the levels of the factor for plotting groups
lev <- with(dune.env, levels(Management))
## vector of colours
cols <- c("blue","red","darkgreen","grey70")

## loop to draw each group
for (i in seq_along(lev)) {
with(dune.env, ordiellipse(mod, Management, kind="se", conf=0.95,
                           lwd=2,
                           col=cols[i], ## ith colour
                           show.groups = lev[i])) ## for ith group
}

This works with rda() too.

HTH

G