\HeaderA{reorder}{Reorder a dendrogram}{reorder}
\methaliasA{reorder.dendrogram}{reorder}{reorder.dendrogram}
\keyword{manip}{reorder}
\begin{Description}\relax
There are many different orderings of a dendrogram that are consistent
with the structure imposed.  This function takes a dendrogram and a
vector of values and reorders the dendrogram in the order of the
supplied vector, maintaining the constraints on the dendrogram.
\end{Description}
\begin{Usage}
\begin{verbatim}
reorder(x, ...)

## S3 method for class 'dendrogram':
reorder(x, wts, agglo.FUN = sum, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{x}] the (dendrogram) object to be reordered
\item[\code{wts}] numeric weights (arbitrary values) for reordering.
\item[\code{agglo.FUN}] a function for weights agglomeration, see below.
\item[\code{...}] additional arguments
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
Using the weights \code{wts}, the leaves of the dendrogram are
reordered so as to be in an order as consistent as possible with the
weights.  At each node, the branches are ordered in
increasing weights where the weight of a branch is defined as
\eqn{f(w_j)}{} where \eqn{f}{} is \code{agglo.FUN} and \eqn{w_j}{} is the
weight of the \eqn{j}{}-th sub branch).
\end{Details}
\begin{Value}
From \code{reorder.dendrogram}, a dendrogram where each node has a
further attribute \code{value} with its corresponding weight.
\end{Value}
\begin{Author}\relax
R. Gentleman and M. Maechler
\end{Author}
\begin{SeeAlso}\relax
\code{\LinkA{rev.dendrogram}{rev.dendrogram}} which simply reverses the nodes'
order; \code{\LinkA{heatmap}{heatmap}}, \code{\LinkA{cophenetic}{cophenetic}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
  set.seed(123)
  x <- rnorm(10)
  hc <- hclust(dist(x))
  dd <- as.dendrogram(hc)
  dd.reorder <- reorder(dd, 10:1)
  plot(dd, main = "random dendrogram `dd'")

  op <- par(mfcol = 1:2)
  plot(dd.reorder, main = "reorder(dd, 10:1)")
  plot(reorder(dd,10:1, agglo.FUN= mean),
       main = "reorder(dd, 10:1, mean)")
  par(op)
\end{ExampleCode}
\end{Examples}

