| 42333 |
ripley |
1 |
% File src/library/stats/man/dendrogram.Rd
|
|
|
2 |
% Part of the R package, http://www.R-project.org
|
| 59039 |
ripley |
3 |
% Copyright 1995-2011 R Core Team
|
| 54147 |
maechler |
4 |
% Copyright 2002-2011 The R Foundation
|
| 42333 |
ripley |
5 |
% Distributed under GPL 2 or later
|
|
|
6 |
|
| 27474 |
ripley |
7 |
\name{dendrogram}
|
|
|
8 |
\title{General Tree Structures}
|
| 56186 |
murdoch |
9 |
\alias{dendrogram}% the class
|
| 27474 |
ripley |
10 |
\alias{as.dendrogram}
|
| 41244 |
ripley |
11 |
\alias{as.dendrogram.dendrogram}
|
| 29701 |
maechler |
12 |
\alias{as.dendrogram.hclust}
|
| 53892 |
maechler |
13 |
\alias{as.hclust.dendrogram}
|
| 27474 |
ripley |
14 |
\alias{cut.dendrogram}
|
|
|
15 |
\alias{[[.dendrogram}
|
| 53892 |
maechler |
16 |
\alias{merge.dendrogram}
|
|
|
17 |
\alias{plot.dendrogram}
|
| 27474 |
ripley |
18 |
\alias{print.dendrogram}
|
|
|
19 |
\alias{rev.dendrogram}
|
|
|
20 |
\alias{str.dendrogram}
|
| 30228 |
maechler |
21 |
\alias{is.leaf}
|
| 27474 |
ripley |
22 |
\description{
|
|
|
23 |
Class \code{"dendrogram"} provides general functions for handling
|
|
|
24 |
tree-like structures. It is intended as a replacement for similar
|
|
|
25 |
functions in hierarchical clustering and classification/regression
|
|
|
26 |
trees, such that all of these can use the same engine for plotting or
|
|
|
27 |
cutting trees.
|
|
|
28 |
}
|
|
|
29 |
\usage{
|
|
|
30 |
as.dendrogram(object, \dots)
|
| 29701 |
maechler |
31 |
\method{as.dendrogram}{hclust}(object, hang = -1, \dots)
|
| 27474 |
ripley |
32 |
|
| 53892 |
maechler |
33 |
\method{as.hclust}{dendrogram}(x, \dots)
|
|
|
34 |
|
| 27474 |
ripley |
35 |
\method{plot}{dendrogram}(x, type = c("rectangle", "triangle"),
|
| 30912 |
ripley |
36 |
center = FALSE,
|
|
|
37 |
edge.root = is.leaf(x) || !is.null(attr(x,"edgetext")),
|
| 30884 |
maechler |
38 |
nodePar = NULL, edgePar = list(),
|
| 30912 |
ripley |
39 |
leaflab = c("perpendicular", "textlike", "none"),
|
|
|
40 |
dLeaf = NULL, xlab = "", ylab = "", xaxt = "n", yaxt = "s",
|
| 46113 |
maechler |
41 |
horiz = FALSE, frame.plot = FALSE, xlim, ylim, \dots)
|
| 27474 |
ripley |
42 |
|
|
|
43 |
\method{cut}{dendrogram}(x, h, \dots)
|
|
|
44 |
|
| 53892 |
maechler |
45 |
\method{merge}{dendrogram}(x, y, \dots, height)
|
|
|
46 |
|
| 27474 |
ripley |
47 |
\method{print}{dendrogram}(x, digits, \dots)
|
|
|
48 |
|
|
|
49 |
\method{rev}{dendrogram}(x)
|
|
|
50 |
|
| 40832 |
maechler |
51 |
\method{str}{dendrogram}(object, max.level = NA, digits.d = 3,
|
| 27474 |
ripley |
52 |
give.attr = FALSE, wid = getOption("width"),
|
| 57963 |
maechler |
53 |
nest.lev = 0, indent.str = "",
|
|
|
54 |
last.str = getOption("str.dendrogram.last"), stem = "--",
|
| 56287 |
ripley |
55 |
\dots)
|
| 30228 |
maechler |
56 |
|
|
|
57 |
is.leaf(object)
|
| 27474 |
ripley |
58 |
}
|
|
|
59 |
\arguments{
|
|
|
60 |
\item{object}{any \R object that can be made into one of class
|
|
|
61 |
\code{"dendrogram"}.}
|
| 60892 |
ripley |
62 |
\item{x, y}{object(s) of class \code{"dendrogram"}.}
|
| 29701 |
maechler |
63 |
\item{hang}{numeric scalar indicating how the \emph{height} of leaves
|
|
|
64 |
should be computed from the heights of their parents; see
|
|
|
65 |
\code{\link{plot.hclust}}.}
|
| 27474 |
ripley |
66 |
\item{type}{type of plot.}
|
|
|
67 |
\item{center}{logical; if \code{TRUE}, nodes are plotted centered with
|
|
|
68 |
respect to the leaves in the branch. Otherwise (default), plot them
|
|
|
69 |
in the middle of all direct child nodes.}
|
|
|
70 |
\item{edge.root}{logical; if true, draw an edge to the root node.}
|
|
|
71 |
\item{nodePar}{a \code{list} of plotting parameters to use for the
|
|
|
72 |
nodes (see \code{\link{points}}) or \code{NULL} by default which
|
|
|
73 |
does not draw symbols at the nodes. The list may contain components
|
| 54147 |
maechler |
74 |
named \code{pch}, \code{cex}, \code{col}, \code{xpd},
|
|
|
75 |
and/or \code{bg} each of
|
| 27474 |
ripley |
76 |
which can have length two for specifying separate attributes for
|
| 54147 |
maechler |
77 |
\emph{inner} nodes and \emph{leaves}. Note that the default of
|
|
|
78 |
\code{pch} is \code{1:2}, so you may want to use \code{pch = NA} if
|
|
|
79 |
you specify \code{nodePar}.}
|
| 27474 |
ripley |
80 |
\item{edgePar}{a \code{list} of plotting parameters to use for the
|
| 30228 |
maechler |
81 |
edge \code{\link{segments}} and labels (if there's an
|
|
|
82 |
\code{edgetext}). The list may contain components
|
|
|
83 |
named \code{col}, \code{lty} and \code{lwd} (for the segments),
|
|
|
84 |
\code{p.col}, \code{p.lwd}, and \code{p.lty} (for the
|
|
|
85 |
\code{\link{polygon}} around the text) and \code{t.col} for the text
|
|
|
86 |
color. As with \code{nodePar}, each can have length two for
|
|
|
87 |
differentiating leaves and inner nodes.
|
|
|
88 |
}
|
| 30884 |
maechler |
89 |
\item{leaflab}{a string specifying how leaves are labeled. The
|
|
|
90 |
default \code{"perpendicular"} write text vertically (by default).\cr
|
|
|
91 |
\code{"textlike"} writes text horizontally (in a rectangle), and \cr
|
|
|
92 |
\code{"none"} suppresses leaf labels.}
|
|
|
93 |
\item{dLeaf}{a number specifying the \bold{d}istance in user
|
|
|
94 |
coordinates between the tip of a leaf and its label. If \code{NULL}
|
|
|
95 |
as per default, 3/4 of a letter width or height is used.}
|
|
|
96 |
\item{horiz}{logical indicating if the dendrogram should be drawn
|
| 27474 |
ripley |
97 |
\emph{horizontally} or not.}
|
|
|
98 |
\item{frame.plot}{logical indicating if a box around the plot should
|
|
|
99 |
be drawn, see \code{\link{plot.default}}.}
|
|
|
100 |
\item{h}{height at which the tree is cut.}
|
| 53892 |
maechler |
101 |
\item{height}{height at which the two dendrogram should be merged. If not
|
|
|
102 |
specified (or \code{NULL}), the default is ten percent larger than
|
|
|
103 |
the (larger of the) two component heights.}
|
| 46113 |
maechler |
104 |
\item{xlim, ylim}{optional x- and y-limits of the plot, passed to
|
|
|
105 |
\code{\link{plot.default}}. The defaults for these show the full
|
|
|
106 |
dendrogram.}
|
| 30884 |
maechler |
107 |
\item{\dots, xlab, ylab, xaxt, yaxt}{graphical parameters, or arguments for
|
| 27474 |
ripley |
108 |
other methods.}
|
| 40281 |
ripley |
109 |
\item{digits}{integer specifying the precision for printing, see
|
| 27474 |
ripley |
110 |
\code{\link{print.default}}.}
|
| 50092 |
ripley |
111 |
\item{max.level, digits.d, give.attr, wid, nest.lev, indent.str}{arguments
|
|
|
112 |
to \code{str}, see \code{\link{str.default}()}. Note that
|
| 30884 |
maechler |
113 |
\code{give.attr = FALSE} still shows \code{height} and \code{members}
|
|
|
114 |
attributes for each node.}
|
| 53892 |
maechler |
115 |
\item{last.str, stem}{strings used for \code{str()} specifying how the
|
|
|
116 |
last branch (at each level) should start and the \emph{stem}
|
| 57963 |
maechler |
117 |
to use for each dendrogram branch. In some environments, using
|
|
|
118 |
\code{last.str = "'"} will provide much nicer looking output, than
|
|
|
119 |
the historical default \code{last.str = "`"}.}
|
| 27474 |
ripley |
120 |
}
|
|
|
121 |
\details{
|
|
|
122 |
The dendrogram is directly represented as a nested list where each
|
|
|
123 |
component corresponds to a branch of the tree. Hence, the first
|
|
|
124 |
branch of tree \code{z} is \code{z[[1]]}, the second branch of the
|
| 53892 |
maechler |
125 |
corresponding subtree is \code{z[[1]][[2]]}, or shorter
|
|
|
126 |
\code{z[[c(1,2)]]}, etc.. Each node of the tree
|
| 27474 |
ripley |
127 |
carries some information needed for efficient plotting or cutting as
|
| 30228 |
maechler |
128 |
attributes, of which only \code{members}, \code{height} and
|
|
|
129 |
\code{leaf} for leaves are compulsory:
|
| 27474 |
ripley |
130 |
\describe{
|
|
|
131 |
\item{\code{members}}{total number of leaves in the branch}
|
|
|
132 |
\item{\code{height}}{numeric non-negative height at which the node
|
|
|
133 |
is plotted.}
|
|
|
134 |
\item{\code{midpoint}}{numeric horizontal distance of the node from
|
| 29796 |
maechler |
135 |
the left border (the leftmost leaf) of the branch (unit 1 between
|
| 61160 |
ripley |
136 |
all leaves). This is used for \code{plot(*, center = FALSE)}.}
|
| 27474 |
ripley |
137 |
\item{\code{label}}{character; the label of the node}
|
| 30228 |
maechler |
138 |
\item{\code{x.member}}{for \code{cut()$upper},
|
|
|
139 |
the number of \emph{former} members; more generally a substitute
|
| 42961 |
ripley |
140 |
for the \code{members} component used for \sQuote{horizontal}
|
|
|
141 |
(when \code{horiz = FALSE}, else \sQuote{vertical}) alignment.}
|
| 27474 |
ripley |
142 |
\item{\code{edgetext}}{character; the label for the edge leading to
|
|
|
143 |
the node}
|
| 30228 |
maechler |
144 |
\item{\code{nodePar}}{a named list (of length-1 components)
|
|
|
145 |
specifying node-specific attributes for \code{\link{points}}
|
|
|
146 |
plotting, see the \code{nodePar} argument above.}
|
|
|
147 |
\item{\code{edgePar}}{a named list (of length-1 components)
|
|
|
148 |
specifying attributes for \code{\link{segments}} plotting of the
|
|
|
149 |
edge leading to the node, and drawing of the \code{edgetext} if
|
|
|
150 |
available, see the \code{edgePar} argument above.}
|
| 27474 |
ripley |
151 |
\item{\code{leaf}}{logical, if \code{TRUE}, the node is a leaf of
|
|
|
152 |
the tree.}% This will often be a \code{\link{character}} which can
|
|
|
153 |
% be used for plotting instead of the \code{text} attribute.}
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
\code{cut.dendrogram()} returns a list with components \code{$upper}
|
|
|
157 |
and \code{$lower}, the first is a truncated version of the original
|
|
|
158 |
tree, also of class \code{dendrogram}, the latter a list with the
|
|
|
159 |
branches obtained from cutting the tree, each a \code{dendrogram}.
|
|
|
160 |
|
|
|
161 |
There are \code{\link{[[}}, \code{\link{print}}, and \code{\link{str}}
|
|
|
162 |
methods for \code{"dendrogram"} objects where the first one
|
|
|
163 |
(extraction) ensures that selecting sub-branches keeps the class.
|
|
|
164 |
|
|
|
165 |
Objects of class \code{"hclust"} can be converted to class
|
| 53892 |
maechler |
166 |
\code{"dendrogram"} using method \code{as.dendrogram()}, and since R
|
|
|
167 |
2.13.0, there is also a \code{\link{as.hclust}()} method as an inverse.
|
| 27474 |
ripley |
168 |
|
| 29799 |
maechler |
169 |
\code{rev.dendrogram} simply returns the dendrogram \code{x} with
|
|
|
170 |
reversed nodes, see also \code{\link{reorder.dendrogram}}.
|
|
|
171 |
|
| 60892 |
ripley |
172 |
The \code{\link{merge}(x, y, ...)} method which merges two or more
|
| 53892 |
maechler |
173 |
dendrograms into a new one which has \code{x} and \code{y} (and
|
|
|
174 |
optional further arguments) as branches.
|
|
|
175 |
|
| 60892 |
ripley |
176 |
\code{is.leaf(object)} returns logical indicating if \code{object} is a
|
| 30228 |
maechler |
177 |
leaf (the most simple dendrogram).
|
| 60892 |
ripley |
178 |
|
| 30228 |
maechler |
179 |
\code{plotNode()} and \code{plotNodeLimit()} are helper functions.
|
| 27474 |
ripley |
180 |
}
|
|
|
181 |
\note{
|
| 53892 |
maechler |
182 |
\describe{
|
|
|
183 |
\item{\code{plot()}:}{When using \code{type = "triangle"},
|
|
|
184 |
\code{center = TRUE} often looks better.}
|
|
|
185 |
\item{\code{str(d)}:}{If you really want to see the \emph{internal}
|
|
|
186 |
structure, use \code{str(unclass(d))} instead.}
|
|
|
187 |
}
|
| 27474 |
ripley |
188 |
}
|
| 35940 |
ripley |
189 |
\section{Warning}{
|
|
|
190 |
Some operations on dendrograms (including plotting) make use of
|
|
|
191 |
recursion. For very deep trees It may be necessary to increase
|
|
|
192 |
\code{\link{options}("expressions")}: if you do you are likely to need
|
| 60892 |
ripley |
193 |
to set the C stack size larger than the default where possible.
|
| 35940 |
ripley |
194 |
}
|
| 30228 |
maechler |
195 |
\seealso{
|
| 53892 |
maechler |
196 |
\code{\link{dendrapply}} for applying a function to \emph{each} node.
|
|
|
197 |
\code{\link{order.dendrogram}} and \code{\link{reorder.dendrogram}};
|
|
|
198 |
further, the \code{\link{labels}} method.
|
| 30228 |
maechler |
199 |
}
|
| 27474 |
ripley |
200 |
\examples{
|
| 41508 |
ripley |
201 |
require(graphics); require(utils)
|
|
|
202 |
|
| 27474 |
ripley |
203 |
hc <- hclust(dist(USArrests), "ave")
|
|
|
204 |
(dend1 <- as.dendrogram(hc)) # "print()" method
|
| 30884 |
maechler |
205 |
str(dend1) # "str()" method
|
| 61160 |
ripley |
206 |
str(dend1, max = 2, last.str = "'") # only the first two sub-levels
|
| 57963 |
maechler |
207 |
oo <- options(str.dendrogram.last = "\\\\") # yet another possibility
|
|
|
208 |
str(dend1, max = 2) # only the first two sub-levels
|
| 61168 |
ripley |
209 |
options(oo) # .. resetting them
|
| 27474 |
ripley |
210 |
|
| 61160 |
ripley |
211 |
op <- par(mfrow = c(2,2), mar = c(5,2,1,4))
|
| 27474 |
ripley |
212 |
plot(dend1)
|
|
|
213 |
## "triangle" type and show inner nodes:
|
| 61160 |
ripley |
214 |
plot(dend1, nodePar = list(pch = c(1,NA), cex = 0.8, lab.cex = 0.8),
|
|
|
215 |
type = "t", center = TRUE)
|
|
|
216 |
plot(dend1, edgePar = list(col = 1:2, lty = 2:3),
|
|
|
217 |
dLeaf = 1, edge.root = TRUE)
|
|
|
218 |
plot(dend1, nodePar = list(pch = 2:1, cex = .4*2:1, col = 2:3),
|
|
|
219 |
horiz = TRUE)
|
| 27474 |
ripley |
220 |
|
| 54147 |
maechler |
221 |
## simple test for as.hclust() as the inverse of as.dendrogram():
|
| 53892 |
maechler |
222 |
stopifnot(identical(as.hclust(dend1)[1:4], hc[1:4]))
|
|
|
223 |
|
| 61160 |
ripley |
224 |
dend2 <- cut(dend1, h = 70)
|
| 27474 |
ripley |
225 |
plot(dend2$upper)
|
| 49649 |
ripley |
226 |
## leaves are wrong horizontally:
|
| 61160 |
ripley |
227 |
plot(dend2$upper, nodePar = list(pch = c(1,7), col = 2:1))
|
| 27474 |
ripley |
228 |
## dend2$lower is *NOT* a dendrogram, but a list of .. :
|
| 61160 |
ripley |
229 |
plot(dend2$lower[[3]], nodePar = list(col = 4), horiz = TRUE, type = "tr")
|
| 27474 |
ripley |
230 |
## "inner" and "leaf" edges in different type & color :
|
| 61168 |
ripley |
231 |
plot(dend2$lower[[2]], nodePar = list(col = 1), # non empty list
|
| 61160 |
ripley |
232 |
edgePar = list(lty = 1:2, col = 2:1), edge.root = TRUE)
|
| 27474 |
ripley |
233 |
par(op)
|
| 53892 |
maechler |
234 |
d3 <- dend2$lower[[2]][[2]][[1]]
|
|
|
235 |
stopifnot(identical(d3, dend2$lower[[2]][[c(2,1)]]))
|
| 61160 |
ripley |
236 |
str(d3, last.str = "'")
|
| 30228 |
maechler |
237 |
|
| 53892 |
maechler |
238 |
## merge() to join dendrograms:
|
|
|
239 |
(d13 <- merge(dend2$lower[[1]], dend2$lower[[3]]))
|
|
|
240 |
## merge() all parts back (using default 'height' instead of original one):
|
|
|
241 |
den.1 <- Reduce(merge, dend2$lower)
|
|
|
242 |
## or merge() all four parts at same height --> 4 branches (!)
|
| 56287 |
ripley |
243 |
d. <- merge(dend2$lower[[1]], dend2$lower[[2]], dend2$lower[[3]],
|
|
|
244 |
dend2$lower[[4]])
|
| 53892 |
maechler |
245 |
## (with a warning) or the same using do.call :
|
|
|
246 |
stopifnot(identical(d., do.call(merge, dend2$lower)))
|
| 61160 |
ripley |
247 |
plot(d., main = "merge(d1, d2, d3, d4) |-> dendrogram with a 4-split")
|
| 53892 |
maechler |
248 |
|
| 46113 |
maechler |
249 |
## "Zoom" in to the first dendrogram :
|
|
|
250 |
plot(dend1, xlim = c(1,20), ylim = c(1,50))
|
|
|
251 |
|
| 61160 |
ripley |
252 |
nP <- list(col = 3:2, cex = c(2.0, 0.75), pch = 21:22,
|
|
|
253 |
bg = c("light blue", "pink"),
|
| 30228 |
maechler |
254 |
lab.cex = 0.75, lab.col = "tomato")
|
| 61160 |
ripley |
255 |
plot(d3, nodePar= nP, edgePar = list(col = "gray", lwd = 2), horiz = TRUE)
|
| 30228 |
maechler |
256 |
%% now add some "edgetext" :
|
|
|
257 |
addE <- function(n) {
|
|
|
258 |
if(!is.leaf(n)) {
|
| 61160 |
ripley |
259 |
attr(n, "edgePar") <- list(p.col = "plum")
|
| 30228 |
maechler |
260 |
attr(n, "edgetext") <- paste(attr(n,"members"),"members")
|
|
|
261 |
}
|
|
|
262 |
n
|
| 27474 |
ripley |
263 |
}
|
| 30228 |
maechler |
264 |
d3e <- dendrapply(d3, addE)
|
| 61160 |
ripley |
265 |
plot(d3e, nodePar = nP)
|
|
|
266 |
plot(d3e, nodePar = nP, leaflab = "textlike")
|
| 30228 |
maechler |
267 |
|
| 61160 |
ripley |
268 |
%% BUG: edge labeling *and* leaflab = "textlike" both fail with horiz = TRUE:
|
|
|
269 |
%% BUG plot(d3e, nodePar = nP, leaflab = "textlike", horiz = TRUE)
|
| 30228 |
maechler |
270 |
}
|
| 27474 |
ripley |
271 |
\keyword{multivariate}
|
|
|
272 |
\keyword{tree}% FIXME: want as.dendrogram.tree() etc!
|
|
|
273 |
\keyword{hplot}% only for plot.()
|