% -*- mode: noweb; noweb-default-code-mode: R-mode; -*-
%\VignetteIndexEntry{Introduction to ordination in vegan}
\documentclass[a4paper,10pt]{article}
\usepackage{vegan} % vegan settings

\title{Vegan: an introduction to ordination}
\author{Jari Oksanen}

\date{\footnotesize{
  processed with vegan
\Sexpr{packageDescription("vegan", field="Version")}
in \Sexpr{R.version.string} on \today}}

%% need no \usepackage{Sweave}
\begin{document}

\SweaveOpts{strip.white=true}
<<echo=false>>=
par(mfrow=c(1,1))
options(width=72)
figset <- function() par(mar=c(4,4,1,1)+.1)
options(SweaveHooks = list(fig = figset))
options("prompt" = "> ", "continue" = "  ")
@

\maketitle
\begin{abstract}
  The document describes typical, simple work pathways of
  vegetation ordination. Unconstrained ordination uses as examples
  detrended correspondence analysis and non-metric multidimensional
  scaling, and shows how to interpret their results by fitting
  environmental vectors and factors or smooth environmental surfaces
  to the graph. The basic plotting command, and more advanced plotting
  commands for congested plots are also discussed, as well as adding
  items such as ellipses, convex hulls, and other items for
  classes. The constrained ordination uses constrained (canonical)
  correspondence analysis as an example. It is first shown how a model
  is defined, then the document discusses model building and
  significance tests of the whole analysis, single constraints and
  axes.
\end{abstract}
\tableofcontents

\vspace{3ex}
\noindent \pkg{Vegan} is a package for community ecologists.  This
documents explains how the commonly used ordination methods can be
performed in \pkg{vegan}.  The document only is a very basic
introduction.
%Another document (\emph{vegan tutorial})
%(\url{http://cc.oulu.fi/~jarioksa/opetus/method/vegantutor.pdf}) gives
%a longer and more detailed introduction to ordination.
The
current document only describes a small part of all \pkg{vegan}
functions.  For most functions, the canonical references are the
\pkg{vegan} help pages.

\section{Ordination}

The \pkg{vegan} package contains all common ordination methods:
Principal component analysis (functions \code{pca} and \code{rda}, or
\code{prcomp} in the base \proglang{R}), correspondence analysis
(\code{ca}, \code{cca}), detrended correspondence analysis
(\code{decorana}), metric scaling, also known as principal coordinate
analysis (\code{pco}, \code{wcmdscale}, or \code{cmdscale} in base
\proglang{R}), non-metric multidimensional scaling (\code{monoMDS})
with wrapper for common pipeline of use (\code{metaMDS}).  Functions
\code{rda} and \code{cca} mainly are designed for constrained
ordination, and will be discussed later.  In this chapter I describe
functions \code{decorana} and \code{metaMDS}.

\subsection{Detrended correspondence analysis}

Detrended correspondence analysis (\textsc{dca}) is done like this:
<<>>=
library(vegan)
data(dune)
ord <- decorana(dune)
@
This saves ordination results in \code{ord}:
<<>>=
ord
@

The display of results is very brief: only eigenvalues and used
options are listed.  Actual ordination results are not shown, but you
can extract them with command \code{scores(ord)}.  The \code{plot}
function also automatically knows how to access the scores.

\subsection{Non-metric multidimensional scaling}


Function \code{metaMDS} is a bit special case.  The actual ordination
is performed by \pkg{vegan} function \code{monoMDS}.  Function
\code{metaMDS} is a wrapper to perform non-metric multidimensional
scaling (\textsc{nmds}) like recommended in community ordination: it
uses adequate dissimilarity measures (function \code{vegdist}), then
it runs \textsc{nmds} several times with random starting
configurations, compares results (function \code{procrustes}), and
stops after finding twice a similar minimum stress solution.  Finally
it scales and rotates the solution, and adds species scores to the
configuration as weighted averages (function \code{wascores}):
<<>>=
ord <- metaMDS(dune, trace = FALSE)
ord
@

\section{Ordination graphics}

Ordination is nothing but a way of drawing graphs, and it is best to
inspect ordinations only graphically (which also implies that they
should not be taken too seriously).

All ordination results of \pkg{vegan} can be displayed with a
\code{plot} command (Fig. \ref{fig:plot}):
<<a>>=
plot(ord)
@
\begin{figure}
<<fig=true,echo=false>>=
<<a>>
@
\caption{Default ordination plot.}
\label{fig:plot}
\end{figure}
Default \code{plot} command uses either black circles for sites and
red pluses for species, or black and red text for sites and species,
resp.  The choices depend on the number of items in the plot and
ordination method.  You can override the default choice by setting
\code{type = "p"} for points, or \code{type = "t"} for text.  For
a better control of ordination graphics you can first draw an empty
plot (\code{type = "n"}) and then add species and sites separately
using \code{points} or \code{text} functions.  In this way you can
combine points and text, and you can select colours and character
sizes freely (Fig. \ref{fig:plot.args}). The easiest way is to build
the plot by layers using pipe (\code{|>}).
<<a>>=
plot(ord, type = "n") |>
    points("sites", cex = 0.8, pch=21, col="red", bg="yellow") |>
    text("species", cex=0.7, col="blue")
@ 

\begin{figure}
<<fig=true, echo=false>>=
<<a>>
@ 
 \caption{A more colourful ordination plot where sites are points and
   species are text.}
 \label{fig:plot.args}
 \end{figure}

 All \pkg{vegan} ordination methods have a specific \code{plot}
 function.  In addition, \pkg{vegan} has an alternative plotting
 function \code{ordiplot} that also knows many non-\pkg{vegan}
 ordination methods, such as \code{prcomp} and \code{cmdscale}.  All
 \pkg{vegan} plot functions return invisibly an \code{ordiplot}
 object, so that you can use \code{ordiplot} support functions with
 the results (\code{points}, \code{text}, \code{identify}).

Alternative plotting methods are available in two packages:

\begin{itemize}

  \item \pkg{vegan3d} on CRAN provides static 3D plots
      (\code{ordiplot3d}), dynamic 3D plots that can be spinned around
      or zoomed (\code{ordirgl}) and interactive 2D plots that can be
      edited moving point labels to better position (\code{orditkplot}).

    \item \pkg{ggvegan} on CRAN provides \pkg{ggplot2} graphics for
      most \pkg{vegan} objects. The \pkg{vegan} \code{scores}
      functions for ordination objects (and some others) recognize
      argument \code{tidy} which can be used to extract scores for
      \pkg{ggplot2} graphics.  (There are some \pkg{lattice} graphics
      functions in \pkg{vegan}, but the plan is to phase out these in
      favour of \pkg{ggvegan} graphics.)

\end{itemize}

\subsection{Cluttered plots}

Ordination plots are often congested: there is a large number of sites
and species, and it may be impossible to display all clearly.  In
particular, two or more species may have identical scores and are
plotted over each other.  Here some methods you can try:
\begin{itemize}
  
  \item Use only \code{points} if you do not need to identify the item
    (and function \code{identify} can be used to add labels some of
    the points).
    
  \item Most \pkg{vegan} \code{plot} and \code{text} functions know
    arguments \code{optimize} and \code{bg}.  With
    \code{optimize=TRUE} the exact scores are shown with labelled
    points, and the labels are positioned to minimize
    over-plotting. Argument \code{bg} gives the background colour of
    labels. With background you cannot see the text or points below
    the label, but you can read at least the uppermost text. These
    arguments can be used together. These two arguments often help
    with moderate cluttering.
  
  \item Zoom into graph setting axis limits \code{xlim} and
    \code{ylim}.  You must typically set both, because \pkg{vegan}
    will maintain equal aspect ratio of axes.

  \item Use points and add label only to some points with
    \code{identify} command.

  \item Use \code{select} argument in ordination \code{text} and
    \code{points} functions to only show the specified items, possibly
    combined with argument \code{labels} for shorter names.

  \item Use automatic \code{orditorp} function that uses text only if
    this can be done without overwriting previous labels, but points
    in other cases.

  \item Use interactive \code{orditkplot} function in \pkg{vegan3d}
    that draws both points and labels for ordination scores, and
    allows you to drag labels to better positions. You can export the
    edited graph in several graphical formats, or return the edited
    positions to \proglang{R} for further processing.

\end{itemize}

\subsection{Adding items to ordination plots}

\pkg{Vegan} has a group of functions for adding information about
classification or grouping of points onto ordination diagrams.
Function \code{ordihull} adds convex hulls, \code{ordiellipse} adds
ellipses enclosing all points in the group (ellipsoid hulls) or
ellipses of standard deviation, standard error or confidence areas,
\code{ordibar} draws a cross corresponding to the principal axes of
ellipses, and \code{ordispider} combines items to their centroid
(Fig. \ref{fig:ordihull}):
<<>>=
data(dune.env)
attach(dune.env)
@
<<a>>=
plot(ord, disp="sites", type="n")
ordihull(ord, Management, col=1:4, lwd=3)
ordiellipse(ord, Management, col=1:4, kind = "ehull", lwd=3)
ordiellipse(ord, Management, col=1:4, draw="polygon")
ordispider(ord, Management, col=1:4, label = TRUE)
points(ord, disp="sites", pch=21, col="red", bg="yellow", cex=1.3)
@
\begin{figure}
<<fig=true,echo=false>>=
<<a>>
@
\caption{Convex hull, ellipsoid hull, standard error ellipse and a spider web diagram
  for Management levels in ordination.}
\label{fig:ordihull}
\end{figure}
In addition, you can overlay a cluster dendrogram from \code{hclust}
using \code{ordicluster} or a minimum spanning tree from
\code{spantree} with its \code{lines} function.  Segmented arrows
can be added with \code{ordiarrows}, lines with
\code{ordisegments} and regular grids with \code{ordigrid}.

\section{Fitting environmental variables}

\pkg{Vegan} provides two functions for fitting environmental
variables onto ordination:
\begin{itemize}
\item \code{envfit} fits vectors of continuous variables and centroids
  of levels of class variables (defined as \code{factor} in
  \proglang{R}).  The arrow shows the direction of the (increasing)
  gradient, and the length of the arrow is proportional to the
  correlation between the variable and the ordination.
\item \code{ordisurf} (which requires package \pkg{mgcv}) fits
  smooth surfaces for continuous variables onto ordination using
  thinplate splines with cross-validatory selection of smoothness.
\end{itemize}

Function \code{envfit} can be called with a \code{formula}
interface, and it optionally can assess the ``significance'' of the
variables using permutation tests:
<<>>=
ord.fit <- envfit(ord ~ A1 + Management, data=dune.env, perm=999)
ord.fit
@
The result can be drawn directly or added to an ordination diagram
(Fig. \ref{fig:envfit}):
<<a>>=
plot(ord, dis="site")
plot(ord.fit, bg = "yellow")
@

Function \code{ordisurf} directly adds a fitted surface onto
ordination, and it returns the result of the fitted \code{gam}
(Fig. \ref{fig:envfit}):
<<b,fig=false>>=
ordisurf(ord, A1, add=TRUE)
@
\begin{figure}
<<fig=true,echo=false,results=hide>>=
<<a>>
<<b>>
@
\caption{Fitted vector and smooth surface for the thickness of A1
  horizon (\code{A1}, in cm), and centroids of Management levels.}
\label{fig:envfit}
\end{figure}

\section{Constrained ordination}

\pkg{Vegan} has three methods of constrained ordination:
constrained or ``canonical'' correspondence analysis (function
\code{cca}), redundancy analysis (function \code{rda}) and
distance-based redundancy analysis (function \code{dbrda}).  All
these functions can have a conditioning term that is ``partialled
out''.  I only demonstrate \code{cca}, but all functions accept
similar commands and can be used in the same way.

The preferred way is to use \code{formula} interface, where the left
hand side gives the community data frame and the right hand side lists
the constraining variables:
<<>>=
ord <- cca(dune ~ A1 + Management, data=dune.env)
ord
@
The results can be plotted with (Fig. \ref{fig:cca}):
<<a>>=
plot(ord, spe.par = list(optimize = TRUE), sit.par = list(type="p"))
@
\begin{figure}
<<fig=true,echo=false>>=
<<a>>
@
\caption{Plot of constrained correspondence analysis showing sites as
  points and with optimized location for species labels.}
\label{fig:cca}
\end{figure}
There are three groups of items: sites, species and centroids (and
biplot arrows) of environmental variables.  All these can be added
individually to an empty plot with pipes, and all previously explained tricks of
controlling graphics still apply. If only small changes are wanted to
default settings, it is easier to change those parameters with a list
of new argument values like in the example.

It is not recommended to perform constrained ordination with all
available environmental variables: adding the number of
constraints means slacker constraint, and you finally end up with
solution similar to unconstrained ordination. In that case it is
better to use unconstrained ordination with environmental fitting.
However, if you really want to do so, it is possible with the
following shortcut in \code{formula}:
<<>>=
cca(dune ~ ., data=dune.env)
@

The model gave a message that some constraints were aliased because
they were redundant. This means that the variable did not have unique
explanatory power, but it can be expressed with the help of other
variables. Such redundant variables are not shown in ordination. In
this case fourth degree polynomial of \code{Manure} (an ordered
factor) was redundant ana aliased. There is one \code{Manure} level
(0) which only occurs in \code{Management} level \code{NM} (natural
management), and we know that \code{Manure} level once we know the
\code{Management}.

\subsection{Significance tests}

\pkg{vegan} provides permutation tests for the significance of
constraints.  The test mimics standard analysis of variance function
(\code{anova}), and the default test analyses all constraints
simultaneously:
<<>>=
anova(ord)
@
The function actually used was \code{anova.cca}, but you do not need
to give its name in full, because \proglang{R} automatically chooses the
correct \code{anova} variant for the result of constrained
ordination.

It is also possible to analyse terms separately:
<<>>=
anova(ord, by="term")
@
This test is sequential: the terms are analysed
in the order they happen to be in the model. You can also analyse
significances of marginal effects (``Type III effects''):
<<>>=
anova(ord, by="margin")
@

\subsection{Conditioned or partial ordination}

All constrained ordination methods can have terms that are partialled
out from the analysis before constraints:
<<>>=
ord <- cca(dune ~ A1 + Management + Condition(Moisture), data=dune.env)
ord
@
This partials out the effect of \code{Moisture} before analysing the
effects of \code{A1} and \code{Management}.  This also influences
the significances of the terms:
<<>>=
anova(ord, by="term")
@
If we had a designed experiment, we may wish to restrict the
permutations so that the observations only are permuted within levels
of \code{Moisture}. Restricted permutation is based on the powerful
\pkg{permute} package. Function \code{how()} can be used to define
permutation schemes. In the following, we set the levels with
\code{plots} argument:
<<>>=
how <- how(nperm=999, plots = Plots(strata=dune.env$Moisture))
anova(ord, by="term", permutations = how)
@

%%%%%%%%%%%%%%%%%%%
<<echo=false>>=
detach(dune.env)
@

\end{document}
