# [R] NMDS Ordination Graphics Problem

Gavin Simpson gavin.simpson at ucl.ac.uk
Wed Apr 14 21:07:35 CEST 2010

```On Mon, 2010-04-05 at 09:58 -0800, Trey wrote:
> Dr. Stevens,

Hmm, did you get the wrong address there ;-)

As Michael Denslow has mentioned, the way to handle this sort of
customised plotting at the moment in vegan is to build the plot up by
hand. Michael's response earlier showed you how to do it with the
in-built data set. Here's how I'd do it (essentially the same thing),
using Stephen Sefick's conveniently dput()ed version of your example
data:

## something to hold the data
dat <- (structure(list(a = c(1L, 12L, 2L, 0L, 0L, 13L), b = c(4L, 17L,
5L, 2L, 1L, 15L), c = c(7L, 6L, 8L, 4L, 4L, 19L), d = c(9L, 2L,
1L, 7L, 6L, 10L), e = c(2L, 3L, 3L, 2L, 9L, 8L), f = c(5L, 7L,
2L, 1L, 10L, 9L)), .Names = c("a", "b", "c", "d", "e", "f"), class =
"data.frame", row.names = c("1i",
"2i", "3i", "1c", "2c", "3c")))

## to hold the factor for whether a site is infested or control
tlabs <- c("infested","control")
treat <- factor(rep(tlabs, each = 3), levels = tlabs)
## setting the levels explicitly insures R doesn't reorder them
alphabetically

## sanity check:
all.equal(length(treat), nrow(dat))

## do the analysis
p <- metaMDS(x)

## set-up the plotting region:
plot(p, display = "sites", type = "n")

## now add the points, coding the infested/control by colour and pch
## this is where I differ slightly from the example Michael gave
## as here the user can specify the cols, pchs etc required
cols <- c("red","navy") ## chose what you want
pchs <- c(3,16) ## ditto choose what you want
points(p, display = "sites", pch = pchs[treat], col = cols[treat])
## add a legend:
legend("topleft", legend = levels(treat), pch = pchs, col = cols, bty =
"n")

Notice how I've done the indexing there. Remember treat is a factor with
more than two observations in it. In the points call above we rely on
the factor being coded internally as 1,2,3,...,k for the k levels in the
factor. To see what is actually being passed to col in the above call,
do:

cols[treat]

So we've only specified our two colours and then used the treatment
variable created earlier to do the right thing for us.

If you have more toruble with vegan, try posting on the R SIG Ecology
list (as that's where Jari Oksanen tends to hang out rather than R-Help)
or on the R-Forge forum for Vegan:

http://r-forge.r-project.org/forum/forum.php?forum_id=194

HTH

G

>
> Hi, my name is Trey Scott, and I'm a grad student of Brian McCarthy's.  He
> referred me to you because of your expertise in handling complex R problems.
> We were hoping you could help us solve a nagging problem that is prohibiting
> me from producing graphicl output.
>
> Here is a simple mock-up of the matrix I'm using
>
>         a     b     c     d     e     f
> 1i      1     4     7     9     2     5
> 2i      12   17    6     2     3     7
> 3i       2    5     8     1     3     2
> 1c      0    2     4     7     2     1
> 2c      0    1     4     6     9     10
> 3c      13   15   19   10    8     9
>
> Where:  1i-3i are "infested" sites, and 1c-3c are "control sites".  A-F are
> species found at each site.  I have several of these ordinations to perform
> on different variables (BA, density, RIV, cover, etc..., all in different
> matrices).  I'm running NMDS (metaMDS) ordinations on each matrices, and
> producing ordination graphs for each cloud of points.  The problem I have is
> that I cannot devise a way to split the cloud of points into infested and
> control so that I can deduce any significant groupings.  A simple difference
> in symbols/color (Ex. gray triangles for infested, black circles for
> control) would do.  Also, I understand the use of pch/col/cex, I just need
> to apply them to the "split".
>
> So:
>
> +   How would I split these out in R after I run the metaMDS in vegan?
> +   What code would be necessary to bring this about?
>
> McCarthy and I are at the end of our preoverbial rope on this; nothing has
> worked.

--
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Dr. Gavin Simpson             [t] +44 (0)20 7679 0522
ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%

```