Skip to content

Commit

Permalink
Merge branch 'vegemite'
Browse files Browse the repository at this point in the history
  • Loading branch information
jarioksa committed Jun 19, 2024
2 parents 7f4ea80 + 1ae7c57 commit 06a5ec1
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 66 deletions.
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
* `rda` and `cca` return centroids for factor levels also when
called without formula, for instance `cca(dune, dune.env)`.

* `vegemite` and `tabasco` can now `use` a factor to show a
classification. The factor levels and sites within levels can be
reordered to give a diagonal pattern, as default in code `tabasco`
and in `vegemite` with new argument `diagonalize = TRUE` (defaults
`FALSE`). With the same argument, `vegemite` can also reorder
dendrogram (or tree) to give a diagonal pattern. If `coverscale` is
used, all internal calculations for ordering rows and columns will
be based on scaled data.

## Bug Fixes

* `plot.cca`: `biplot` and `regression` arrows were not drawn and
Expand All @@ -52,6 +61,9 @@

* `summary.ordihull` failed if input data were not two-dimensional.

* `vegemite` returned only the last page of multi-page table in its
(invisible) return object.

## Deprecated and Defunct

* Disabled use of `summary` to get ordination scores: use `scores`!
Expand Down
17 changes: 17 additions & 0 deletions R/tabasco.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@
site.ind <- order(tmp)
if (is.null(sp.ind))
sp.ind <- order(wascores(tmp, x))
} else if (is.factor(use)) {
tmp <- as.numeric(use)
if (isTRUE(Rowv)) { # reorder factor levels & sites within factors
ord <- scores(cca(x, use),
choices=1, display = c("lc","wa","sp"))
if (cor(tmp, ord$constraints, method = "spearman") < 0) {
ord$constraints <- -ord$constraints
ord$sites <- -ord$sites
ord$species <- -ord$species
}
site.ind <- order(ord$constraints, ord$sites)
sp.ind <- order(ord$species)
}
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
Expand Down
53 changes: 44 additions & 9 deletions R/vegemite.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
`vegemite` <-
function (x, use, scale, sp.ind = NULL, site.ind = NULL, zero = ".",
select, ...)
select, diagonalize = FALSE, ...)
{
if (!missing(use)) {
## derived index should be based on transformed & tabulated data
xprime <- if (missing(scale)) x
else coverscale(x, scale = scale, character = FALSE)
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))
sp.ind <- order(wascores(use, xprime))
}
else if (inherits(use, c("hclust", "twins"))) {
if (inherits(use, "twins")) {
use <- as.hclust(use)
}
if (diagonalize) {
wts <- scores(cca(xprime), choices=1, display = "wa")
use <- reorder(use, wts)
}
if (is.null(site.ind))
site.ind <- use$order
if (is.null(sp.ind))
sp.ind <- order(wascores(order(site.ind), x))
sp.ind <- order(wascores(order(site.ind), xprime))
}
else if (inherits(use, "dendrogram")) {
if (diagonalize) {
wts <- scores(cca(xprime), choices=1, display = "wa")
use <- reorder(use, wts)
}
if (is.null(site.ind)) {
site.ind <- 1:nrow(x)
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))
sp.ind <- order(wascores(order(site.ind), xprime))
}
else if (is.list(use)) {
tmp <- scores(use, choices = 1, display = "sites")
Expand All @@ -35,16 +46,35 @@
sp.ind <- try(order(scores(use, choices = 1,
display = "species")))
if (inherits(sp.ind, "try-error"))
sp.ind <- order(wascores(tmp, x))
sp.ind <- order(wascores(tmp, xprime))
}
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))
sp.ind <- order(wascores(tmp, xprime))
}
}
else if (is.factor(use)) {
tmp <- as.numeric(use)
if (diagonalize) {
ord <- scores(cca(xprime, use), choices = 1,
display = c("lc","wa","sp"))
if (cor(tmp, ord$constraints, method = "spearman") < 0) {
ord$constraints <- -ord$constraints
ord$sites <- -ord$sites
ord$species <- -ord$species
}
## order factors and sites within factor levels
site.ind <- order(ord$constraints, ord$sites)
sp.ind <- order(ord$species)
}
if (is.null(site.ind))
site.ind <- order(tmp)
if (is.null(sp.ind))
sp.ind <- order(wascores(tmp, xprime))
}
} # end of handling 'use'
if (!is.null(sp.ind) && is.logical(sp.ind))
sp.ind <- seq_len(ncol(x))[sp.ind]
if (!is.null(site.ind) && is.logical(site.ind))
Expand Down Expand Up @@ -103,8 +133,13 @@
}
dimnames(tbl) <- d
print(noquote(tbl))
## collect all pages for output table
if (exists(".tabout", inherits = FALSE))
.tabout[,2] <- paste0(.tabout[,2], tbl[,2])
else
.tabout <- tbl
}
out <- list(sites = site.ind, species = sp.ind, table = tbl)
out <- list(sites = site.ind, species = sp.ind, table = .tabout)
cat(length(out$sites), "sites,", length(out$species), "species\n")
if (!is.null(usedscale))
cat("scale: ", usedscale, "\n")
Expand Down
68 changes: 40 additions & 28 deletions man/vegemite.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
analysis.
}
\usage{
vegemite(x, use, scale, sp.ind, site.ind, zero=".", select ,...)
vegemite(x, use, scale, sp.ind, site.ind, zero=".", select,
diagonalize = FALSE, ...)
tabasco(x, use, sp.ind = NULL, site.ind = NULL, select,
Rowv = TRUE, Colv = TRUE, labRow = NULL, labCol = NULL,
scale, col = heat.colors(12), ...)
Expand All @@ -25,9 +26,9 @@ coverscale(x, scale=c("Braun.Blanquet", "Domin", "Hult", "Hill", "fix","log"),

\arguments{
\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{use}{Either a numeric vector or a classification factor, 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, site.ind}{Species and site indices. In \code{tabasco},
these can also be \code{\link{hclust}} tree,
\code{\link[cluster]{agnes}} clusterings or
Expand All @@ -38,13 +39,22 @@ coverscale(x, scale=c("Braun.Blanquet", "Domin", "Hult", "Hill", "fix","log"),
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{diagonalize}{Try to re-order \code{vegemite} table to a diagonal
pattern when using classification factor or a dendrogram.
Dendrograms are re-orded by the first axis of correspondence
analysis. Factor levels and sites within factor levels are
re-ordered by the first axis of (constrained) correspondence
analysis.}

\item{Rowv, Colv}{Re-order factors or dendrograms for the rows (sites)
or columns (species) of \code{x}. If \code{Rowv = TRUE}, factor
levels and sites within factors are re-ordered to show diagonal
pattern using CCA. If \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{labRow, labCol}{character vectors with row and column labels
used in the \code{\link{heatmap}} instead of the default. NB., the
Expand All @@ -65,9 +75,9 @@ coverscale(x, scale=c("Braun.Blanquet", "Domin", "Hult", "Hill", "fix","log"),
Data maximum in the \code{select}ed subset will be used if this is
missing.}

\item{character}{Return character codes suitable for
\item{character}{Return character codes suitable for tabulation in
\code{vegemite}. If \code{FALSE}, returns corresponding
integers.}
integers suitable for numerical analysis.}

\item{\dots}{Arguments passed to \code{coverscale} (i.e., \code{maxabund}) in
\code{vegemite} and to \code{\link{heatmap}} in \code{tabasco}.}
Expand Down Expand Up @@ -96,25 +106,27 @@ coverscale(x, scale=c("Braun.Blanquet", "Domin", "Hult", "Hill", "fix","log"),
\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}.
\code{vegemite}. In \code{tabasco} the species dendrogram can be
given in \code{sp.ind}.

If \code{use} is a vector, it is used for ordering sites. If
If \code{use} is a numeric vector, it is used for ordering sites,
and if it is a factor, it is used to order sites by classes. 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
arranged by the first axis (provided that results are available 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}} or \code{\link{reorder.hclust}}.
Please note that \code{\link{dendrogram}} and \code{\link{hclust}}
reordering can differ: unweighted means of merged branches are used in
\code{\link{dendrogram}}, but weighted means (= means of leaves of the
cluster) are used in \code{\link{reorder.hclust}}. In all cases where
species scores are missing, species are ordered by their weighted
averages (\code{\link{wascores}}) on site order.
cluster dendrogram. Function \code{tabasco} re-orders the
dendrogram to give a diagonal pattern if \code{Rowv =
TRUE}. Alternatively, if \code{Rowv} is a vector its values are used
to re-order dendrogram. With \code{diagonalize = TRUE} the
dendrogram will be re-ordered in \code{vegemite} to give a diagonal
pattern. If \code{use} is a factor, its levels and sites within
levels will be reordered to give a diagonal pattern if
\code{diagonalize = TRUE} in \code{vegemite} or \code{Rowv = TRUE}
in \code{tabasco}. In all cases where species scores are missing,
species are ordered by their weighted averages
(\code{\link{wascores}}) on site order or site value.

Species and sites can be ordered explicitly giving their indices or
names in parameters \code{sp.ind} and \code{site.ind}. If these are
Expand Down
58 changes: 29 additions & 29 deletions tests/Examples/vegan-Ex.Rout.save
Original file line number Diff line number Diff line change
Expand Up @@ -8715,49 +8715,49 @@ scale: Hult

1 111 1211221212222
431567380922149306254578
Flavniva 21.111.111....11........
Cladamau .1.1...1................
Stersp 111211111....111.1.111.1
Polypili ..111..1......111..1....
Diphcomp .1...111.1...........1..
Flavniva 21.111.111....11........
Cladphyl ..1.....11...1..........
Cladrang 344544332133111223121121
Cladcerv 1.........1..........1..
Diphcomp .1...111.1...........1..
Cladstel 454121215555213111111.1.
Cladrang 344544332133111223121121
Cladarbu 322344331111132222121111
Vacculig 11...2.1........1.1..111
Callvulg 111.1.311.11.1..11111...
Icmaeric ...1.1.......1...1......
Cladsp 1...1.11.1......111..11.
Cladcocc 1111111111...1111111.1.1
Pinusylv 1.111.1111111111111111.1
Callvulg 111.1.311.11.1..11111...
Polypili ..111..1......111..1....
Nepharct .1...1.1........1....1..
Stersp 111211111....111.1.111.1
Cladchlo .1..111.111.1.11..1.1...
Cetrisla 1.1...1.11.111...1.111.1
Cladfimb 11.11111111111111111.111
Peltapht ..1...11.......1.....11.
Cladcerv 1.........1..........1..
Cetreric 11.1111111...1..111111..
Cladgrac 11111111.111111111111111
Pohlnuta 111..11111.1111111111.11
Ptilcili .11.1.11.11.2.11.1111111
Barbhatc ......1.....1.11......1.
Cladcocc 1111111111...1111111.1.1
Cladcorn 111111111111111111111111
Vaccviti 113122132323412331223232
Cladcris 111111111111111111111111
Empenigr 111111122122312322111231
Cladbotr ......1.....1.111...1.11
Betupube ............1.....1..1..
Cladunci 111111111111131111112111
Cladfimb 11.11111111111111111.111
Cladgrac 11111111.111111111111111
Polyjuni 1.111111111.111111.112.1
Vacculig 11...2.1........1.1..111
Pinusylv 1.111.1111111111111111.1
Pohlnuta 111..11111.1111111111.11
Claddefo 11.111111111111111111111
Cladunci 111111111111131111112111
Cladsp 1...1.11.1......111..11.
Peltapht ..1...11.......1.....11.
Vaccviti 113122132323412331223232
Cetrisla 1.1...1.11.111...1.111.1
Dicrpoly ..11.1..11..1.1111.11..1
Polycomm ............111......11.
Rhodtome ......1.....1.....1...11
Polyjuni 1.111111111.111111.112.1
Empenigr 111111122122312322111231
Ptilcili .11.1.11.11.2.11.1111111
Dicrfusc 11111111111112111442.211
Barbhatc ......1.....1.11......1.
Descflex 1....1......1.1...1..111
Pleuschr 113111111111123333444455
Vaccmyrt ......1..1..311..111..13
Nepharct .1...1.1........1....1..
Cladbotr ......1.....1.111...1.11
Rhodtome ......1.....1.....1...11
Polycomm ............111......11.
Betupube ............1.....1..1..
Dicrsp ......1....11...111133.1
Descflex 1....1......1.1...1..111
Hylosple ................1....122
24 sites, 44 species
scale: Hult
Expand Down Expand Up @@ -8969,7 +8969,7 @@ Procrustes sum of squares:
> cleanEx()
> options(digits = 7L)
> base::cat("Time elapsed: ", proc.time() - base::get("ptime", pos = 'CheckExEnv'),"\n")
Time elapsed: 8.54 0.277 8.841 0 0
Time elapsed: 8.598 0.302 8.931 0 0
> grDevices::dev.off()
null device
1
Expand Down

0 comments on commit 06a5ec1

Please sign in to comment.