[Vegan-commits] r2446 - in branches/2.0: . R inst man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Feb 17 15:30:49 CET 2013
Author: jarioksa
Date: 2013-02-17 15:30:49 +0100 (Sun, 17 Feb 2013)
New Revision: 2446
Added:
branches/2.0/R/tabasco.R
Modified:
branches/2.0/NAMESPACE
branches/2.0/R/vegemite.R
branches/2.0/inst/ChangeLog
branches/2.0/man/vegemite.Rd
Log:
merge 2431,3, 2435:2442: add tabasco
Modified: branches/2.0/NAMESPACE
===================================================================
--- branches/2.0/NAMESPACE 2013-02-17 14:05:43 UTC (rev 2445)
+++ branches/2.0/NAMESPACE 2013-02-17 14:30:49 UTC (rev 2446)
@@ -25,7 +25,7 @@
rankindex, rarecurve, rarefy, raupcrick, rda, renyiaccum, renyi,
rrarefy, scores, showvarparts, simper, spandepth, spantree, specaccum,
specnumber, specpool2vect, specpool, spenvcor, stepacross, stressplot,
-swan, taxa2dist, taxondive, tolerance, treedist, treedive, treeheight,
+swan, tabasco, taxa2dist, taxondive, tolerance, treedist, treedive, treeheight,
tsallisaccum, tsallis, varpart, vectorfit, vegandocs, vegdist,
vegemite, veiledspec, wascores, wcmdscale, wisconsin)
Copied: branches/2.0/R/tabasco.R (from rev 2431, pkg/vegan/R/tabasco.R)
===================================================================
--- branches/2.0/R/tabasco.R (rev 0)
+++ branches/2.0/R/tabasco.R 2013-02-17 14:30:49 UTC (rev 2446)
@@ -0,0 +1,129 @@
+### The function displays (ordered) heatmaps of community data. It
+### copies vegemite() for handling 'use', 'sp.ind', 'site.ind' and
+### 'select', but then switches to heatmap() to display the
+### data. Unlike heatmap(), it does not insist on showing dendrograms,
+### but only uses these for sites, and only if given as 'use'.
+
+`tabasco` <-
+ function (x, use, sp.ind = NULL, site.ind = NULL,
+ select, Rowv = TRUE, Colv = TRUE, ...)
+{
+ pltree <- sptree <- NA
+ if (!missing(use)) {
+ if (!is.list(use) && is.vector(use)) {
+ if (is.null(site.ind))
+ site.ind <- order(use)
+ if (is.null(sp.ind))
+ sp.ind <- order(wascores(use, x))
+ }
+ else if (inherits(use, c("dendrogram", "hclust", "twins"))) {
+ if (inherits(use, "twins")) {
+ require(cluster) || stop("package cluster needed to handle 'use'")
+ }
+ if (!inherits(use, "dendrogram"))
+ use <- as.dendrogram(use)
+ if (!is.null(site.ind))
+ stop("'site.ind' cannot be used with dendrogram")
+ ## Reorder tree if Rowv specified
+ if (isTRUE(Rowv)) {
+ ## order by first CA axis -- decorana() is fastest
+ tmp <- decorana(x, ira = 1)
+ use <- reorder(use, scores(tmp, dis="sites", choices = 1),
+ agglo.FUN = mean)
+ } else if (length(Rowv) > 1) {
+ ## Rowv is a vector
+ if (length(Rowv) != nrow(x))
+ stop(gettextf("Rowv has length %d, but 'x' has %d rows",
+ length(Rowv), nrow(x)))
+ use <- reorder(use, Rowv, agglo.FUN = mean)
+ }
+ site.ind <- seq_len(nrow(x))
+ names(site.ind) <- rownames(x)
+ site.ind <- site.ind[labels(use)]
+ if (is.null(sp.ind))
+ sp.ind <- order(wascores(order(site.ind), x))
+ pltree <- use
+ }
+ else if (is.list(use)) {
+ tmp <- scores(use, choices = 1, display = "sites")
+ if (is.null(site.ind))
+ site.ind <- order(tmp)
+ if (is.null(sp.ind))
+ sp.ind <- try(order(scores(use, choices = 1,
+ display = "species")))
+ if (inherits(sp.ind, "try-error"))
+ sp.ind <- order(wascores(tmp, x))
+ }
+ else if (is.matrix(use)) {
+ tmp <- scores(use, choices = 1, display = "sites")
+ if (is.null(site.ind))
+ site.ind <- order(tmp)
+ if (is.null(sp.ind))
+ sp.ind <- order(wascores(tmp, x))
+ }
+ }
+ ## see if sp.ind is a dendrogram or hclust tree
+ if (inherits(sp.ind, c("hclust", "dendrogram", "twins"))) {
+ if (inherits(sp.ind, "twins"))
+ require("cluster") || stop("package cluster needed to handle 'sp.ind'")
+ if (!inherits(sp.ind, "dendrogram"))
+ sp.ind <- as.dendrogram(sp.ind)
+ sptree <- sp.ind
+ ## Consider reordering species tree
+ if (isTRUE(Colv) && !is.null(site.ind)) {
+ sptree <- reorder(sptree, wascores(order(site.ind), x),
+ agglo.FUN = mean)
+ } else if (length(Colv) > 1) {
+ if (length(Colv) != ncol(x))
+ stop(gettextf("Colv has length %d, but 'x' has %d columns",
+ length(Colv), ncol(x)))
+ sptree <- reorder(sptree, Colv, agglo.FUN = mean)
+ }
+ sp.ind <- seq_len(ncol(x))
+ names(sp.ind) <- colnames(x)
+ sp.ind <- sp.ind[labels(sptree)]
+ ## reverse: origin in the upper left corner
+ sptree <- rev(sptree)
+ }
+ if (!is.null(sp.ind) && is.logical(sp.ind))
+ sp.ind <- (1:ncol(x))[sp.ind]
+ if (!is.null(site.ind) && is.logical(site.ind))
+ site.ind <- (1:nrow(x))[site.ind]
+ if (is.null(sp.ind))
+ sp.ind <- 1:ncol(x)
+ if (is.null(site.ind))
+ site.ind <- 1:nrow(x)
+ if (!missing(select)) {
+ if (!is.na(pltree))
+ stop("sites cannot be 'select'ed with dendrograms or hclust trees")
+ if (!is.logical(select))
+ select <- sort(site.ind) %in% select
+ stake <- colSums(x[select, , drop = FALSE]) > 0
+ site.ind <- site.ind[select[site.ind]]
+ site.ind <- site.ind[!is.na(site.ind)]
+ }
+ else {
+ stake <- colSums(x[site.ind, ]) > 0
+ }
+ sp.ind <- sp.ind[stake[sp.ind]]
+ ## heatmap will reorder items by dendrogram so that we need to
+ ## give indices in the unsorted order if rows or columns have a
+ ## dendrogram
+ if (is.na(pltree[1]))
+ rind <- site.ind
+ else
+ rind <- sort(site.ind)
+ if (is.na(sptree[1]))
+ ## reverse: origin in the upper left corner
+ cind <- rev(sp.ind)
+ else
+ cind <- sort(sp.ind)
+ ## we assume t() changes data.frame to a matrix
+ x <- t(x[rind, cind])
+ sp.nam <- rownames(x)
+ sp.len <- max(nchar(sp.nam))
+ heatmap((max(x) - x), Rowv = sptree, Colv = pltree,
+ scale = "none", ...)
+ out <- list(sites = site.ind, species = sp.ind)
+ invisible(out)
+}
Modified: branches/2.0/R/vegemite.R
===================================================================
--- branches/2.0/R/vegemite.R 2013-02-17 14:05:43 UTC (rev 2445)
+++ branches/2.0/R/vegemite.R 2013-02-17 14:30:49 UTC (rev 2446)
@@ -1,4 +1,4 @@
-"vegemite" <-
+`vegemite` <-
function (x, use, scale, sp.ind = NULL, site.ind = NULL, zero = ".",
select, ...)
{
@@ -9,7 +9,11 @@
if (is.null(sp.ind))
sp.ind <- order(wascores(use, x))
}
- else if (inherits(use, "hclust")) {
+ else if (inherits(use, c("hclust", "twins"))) {
+ if (inherits(use, "twins")) {
+ require(cluster) || stop("package cluster needed for 'use'")
+ use <- as.hclust(use)
+ }
if (is.null(site.ind))
site.ind <- use$order
if (is.null(sp.ind))
Modified: branches/2.0/inst/ChangeLog
===================================================================
--- branches/2.0/inst/ChangeLog 2013-02-17 14:05:43 UTC (rev 2445)
+++ branches/2.0/inst/ChangeLog 2013-02-17 14:30:49 UTC (rev 2446)
@@ -8,6 +8,7 @@
* merge r2434: return wcmdscale object always with non-default
arguments.
* merge r2432: edit cca.Rd.
+ * merge r2431,2433,2435 thru 2442: add tabasco().
Version 2.0-6 (released February 11, 2013)
Modified: branches/2.0/man/vegemite.Rd
===================================================================
--- branches/2.0/man/vegemite.Rd 2013-02-17 14:05:43 UTC (rev 2445)
+++ branches/2.0/man/vegemite.Rd 2013-02-17 14:30:49 UTC (rev 2446)
@@ -1,71 +1,108 @@
\name{vegemite}
\alias{vegemite}
+\alias{tabasco}
\alias{coverscale}
-\title{Prints a Compact, Ordered Vegetation Table }
+\title{Display Compact Ordered Community Tables }
\description{
- The function prints a compact vegetation table, where species are
- rows, and each site takes only one column without spaces. The
- vegetation table can be ordered by explicit indexing, by environmental
- variables or results from an ordination or cluster analysis.
+ Functions \code{vegemite} and \code{tabasco} display compact
+ community tables. Function \code{vegemite} prints text tables where
+ species are rows, and each site takes only one column without
+ spaces. Function \code{tabasco} provides interface for
+ \code{\link{heatmap}} for a colour \code{\link{image}} of the
+ data. The community table can be ordered by explicit indexing, by
+ environmental variables or results from an ordination or cluster
+ analysis.
}
\usage{
vegemite(x, use, scale, sp.ind, site.ind, zero=".", select ,...)
+tabasco(x, use, sp.ind = NULL, site.ind = NULL, select,
+ Rowv = TRUE, Colv = TRUE, ...)
coverscale(x, scale=c("Braun.Blanquet", "Domin", "Hult", "Hill", "fix","log"),
maxabund)
}
\arguments{
- \item{x}{Vegetation data. }
+ \item{x}{Community data. }
\item{use}{Either a vector, or an object from \code{cca},
\code{decorana} \emph{etc.} or \code{hclust} or a
\code{\link{dendrogram}} for ordering sites and species.}
- \item{sp.ind}{Species indices. }
- \item{site.ind}{Site indices. }
+ \item{sp.ind, site.ind}{Species and site indices. In \code{tabasco},
+ these can also be \code{\link{hclust}} tree,
+ \code{\link[cluster]{agnes}} clusterings or
+ \code{\link{dendrogram}}s. }
\item{zero}{Character used for zeros. }
\item{select}{Select a subset of sites. This can be a logical vector
(\code{TRUE} for selected sites), or a vector of indices of selected
sites. The order of indices does not influence results, but you
must specify \code{use} or \code{site.ind} to reorder sites.
- }
+ }
+ \item{Rowv, Colv}{Re-order dendrograms for the rows (sites) or
+ columns (species) of \code{x}. If the \code{Rowv = TRUE}, row
+ dendrograms are ordered by the first axis of correspondence
+ analysis, and when \code{Colv = TRUE} column dendrograms by the
+ weighted average (\code{\link{wascores}}) of the row order.
+ Alternatively, the arguments can be vectors that are used to
+ reorder the dendrogram. }
\item{scale}{Cover scale used (can be abbreviated).}
\item{maxabund}{Maximum abundance used with \code{scale = "log"}.
Data maximum in the \code{select}ed subset will be used if this is
missing.}
- \item{...}{Arguments passed to \code{coverscale} (i.e., \code{maxabund}).}
+ \item{\dots}{Arguments passed to \code{coverscale} (i.e., \code{maxabund}) in
+ \code{vegemite} and to \code{\link{heatmap}} in \code{tabasco}.}
}
\details{
- The function prints a traditional vegetation table.
- Unlike in ordinary data matrices, species are used as rows and sites
- as columns. The table is printed in compact form: only one character
+ The function \code{vegemite} prints a traditional community table.
+ The display is transposed, so that species are in rows and sites in
+ columns. The table is printed in compact form: only one character
can be used for abundance, and there are no spaces between
columns. Species with no occurrences are dropped from the table.
- The parameter \code{use} can be a vector or an object from
- \code{\link{hclust}}, a \code{\link{dendrogram}} or any ordination
- result recognized by \code{\link{scores}} (all ordination methods in
- \pkg{vegan} and some of those not in \pkg{vegan}).
- If \code{use} is a vector, it is used
- for ordering sites. If \code{use} is an object from ordination, both
- sites and species are arranged by the first axis.
- When \code{use} is an
- object from \code{\link{hclust}} or a \code{\link{dendrogram}}, the
- sites are ordered similarly
- as in the cluster dendrogram.
- If ordination methods provide species scores, these are used for
- ordering species. In all cases where species scores are missing,
- species are ordered by their weighted averages (\code{\link{wascores}})
- on site scores. There is no natural, unique ordering in hierarchic
- clustering, but in some cases species are still nicely ordered (please
- note that you can \code{\link{reorder.dendrogram}} to have such a
- natural order).
- Alternatively, species and sites can be ordered explicitly giving
- their indices or names in parameters \code{sp.ind} and
- \code{site.ind}. If these are given, they take precedence over
- \code{use}. A subset of sites can be displayed using argument
- \code{select}, but this cannot be used to order sites, but you still
- must give \code{use} or \code{site.ind}.
+ Function \code{tabasco} produces a similar table as \code{vegemite}
+ using \code{\link{heatmap}}, where abundances are coded by
+ colours. The function can also display dendrograms for sites
+ (columns) or species if these are given as an argument (\code{use}
+ for sites, \code{sp.ind} for species).
+ The parameter \code{use} will be used to re-order output. The
+ \code{use} can be a vector or an object from \code{\link{hclust}} or
+ \code{\link[cluster]{agnes}}, a \code{\link{dendrogram}} or any
+ ordination result recognized by \code{\link{scores}} (all ordination
+ methods in \pkg{vegan} and some of those not in \pkg{vegan}). The
+ \code{\link{hclust}}, \code{\link[cluster]{agnes}} and
+ \code{\link{dendrogram}} must be for sites. The dendrogram is
+ displayed above the sites in \code{tabasco}, but is not shown in
+ \code{vegemite}. No dendrogram for species is displayed, except
+ when given in \code{sp.ind}.
+
+ If \code{use} is a vector, it is used for ordering sites. If
+ \code{use} is an object from ordination, both sites and species are
+ arranged by the first axis (provided that results are available both
+ also for species). When \code{use} is an object from
+ \code{\link{hclust}}, \code{\link[cluster]{agnes}} or a
+ \code{\link{dendrogram}}, the sites are ordered similarly as in the
+ cluster dendrogram. Function \code{tabasco} re-orders the
+ dendrogram if \code{Rowv = TRUE} or \code{Rowv} is a vector. Such
+ re-ordering is not available for \code{vegemite}, but it can be done
+ by hand using \code{\link{reorder.dendrogram}}. In all cases where
+ species scores are missing, species are ordered by their weighted
+ averages (\code{\link{wascores}}) on site order.
+
+ Species and sites can be ordered explicitly giving their indices or
+ names in parameters \code{sp.ind} and \code{site.ind}. If these are
+ given, they take precedence over \code{use}. A subset of sites can
+ be displayed using argument \code{select}, but this cannot be used
+ to order sites, but you still must give \code{use} or
+ \code{site.ind}. However, \code{tabasco} makes two exceptions:
+ \code{site.ind} and \code{select} cannot be used when \code{use} is
+ a dendrogram (clustering result). In addition, the \code{sp.ind} can
+ be an \code{\link{hclust}} tree, \code{\link[cluster]{agnes}}
+ clustering or a \code{\link{dendrogram}}, and in that case the
+ dendrogram is plotted on the left side of the
+ \code{\link{heatmap}}. Phylogenetic trees cannot be directly used,
+ \code{\link[ape]{as.hclust.phylo}} (package \pkg{ape}) can transform
+ these to \code{\link{hclust}} trees.
+
If \code{scale} is given, \code{vegemite} calls
\code{coverscale} to transform percent cover
scale or some other scales into traditional class scales used in
@@ -88,13 +125,14 @@
10 with \code{X} and positive values below 1 with \code{+}.
}
\value{
- The function is used mainly to print a table, but it returns
+ The functions are used mainly to display a table, but they return
(invisibly) a list
with items:
\item{species}{Ordered species indices}
\item{sites}{Ordered site indices}
+
These items can be used as arguments \code{sp.ind} and \code{site.ind}
- to reproduce the table. In addition to the proper table, the function
+ to reproduce the table. In addition to the proper table, \code{vegemite}
prints the numbers of species and sites and the name of the used cover
scale at the end.
}
@@ -106,15 +144,19 @@
}
\author{Jari Oksanen}
-\seealso{\code{\link{cut}} and \code{\link{approx}} for making your own
- `cover scales', \code{\link{wascores}} for weighted averages.
- }
+\seealso{\code{\link{cut}} and \code{\link{approx}} for making your
+ own \sQuote{cover scales} for \code{vegemite}. Function
+ \code{tabasco} is based on \code{\link{heatmap}} which in turn is
+ based on \code{\link{image}}. Both functions order species with
+ weighted averages using \code{\link{wascores}}. }
-\note{ This function was called \code{vegetab} in older versions of
- \code{vegan}. The new name was chosen because the output is so
- compact (and to avoid confusion with the \code{vegtab} function in the
- \pkg{labdsv} package).
- }
+\note{
+
+ The name \code{vegemite} was chosen because the output is so
+ compact, and the \code{tabasco} because it is just as compact, but
+ uses heat colours.
+
+}
\examples{
data(varespec)
## Print only more common species
@@ -127,9 +169,31 @@
clus <- hclust(vegdist(varespec))
cl <- cutree(clus, 3)
sel <- vegemite(varespec, use=dca, select = cl == 3, scale="Br")
-# Re-create previous
+## Re-create previous
vegemite(varespec, sp=sel$sp, site=sel$site, scale="Hult")
+## Re-order clusters by ordination
+clus <- as.dendrogram(clus)
+clus <- reorder(clus, scores(dca, choices=1, display="sites"), agglo.FUN = mean)
+vegemite(varespec, clus, scale = "Hult")
+
+## Abundance values have such a wide range that they must be rescaled
+## or all abundances will not be shown in tabasco
+tabasco(decostand(varespec, "log"), dca)
+
+## Classification trees for species
+data(dune, dune.taxon)
+taxontree <- hclust(taxa2dist(dune.taxon))
+plotree <- hclust(vegdist(dune), "average")
+## Automatic reordering of clusters
+tabasco(dune, plotree, sp.ind = taxontree)
+## No reordering of taxonomy
+tabasco(dune, plotree, sp.ind = taxontree, Colv = FALSE)
+## Species cluster: most dissimilarity indices do a bad job when
+## comparing rare and common species, but Raup-Crick makes sense
+sptree <- hclust(vegdist(t(dune), "raup"), "average")
+tabasco(dune, plotree, sptree)
}
-\keyword{ print }
-\keyword{ manip }
+\keyword{print}
+\keyword{manip}
+\keyword{hplot}
More information about the Vegan-commits
mailing list