dendrogram package:stats R Documentation _G_e_n_e_r_a_l _T_r_e_e _S_t_r_u_c_t_u_r_e_s _D_e_s_c_r_i_p_t_i_o_n: Class '"dendrogram"' provides general functions for handling tree-like structures. It is intended as a replacement for similar functions in hierarchical clustering and classification/regression trees, such that all of these can use the same engine for plotting or cutting trees. The code is still in testing stage and the API may change in the future. _U_s_a_g_e: as.dendrogram(object, ...) ## S3 method for class 'hclust': as.dendrogram(object, hang = -1, ...) ## S3 method for class 'dendrogram': plot(x, type = c("rectangle", "triangle"), center = FALSE, edge.root = is.leaf(x) || !is.null(attr(x,"edgetext")), nodePar = NULL, edgePar = list(), leaflab = c("perpendicular", "textlike", "none"), dLeaf = NULL, xlab = "", ylab = "", xaxt = "n", yaxt = "s", horiz = FALSE, frame.plot = FALSE, xlim, ylim, ...) ## S3 method for class 'dendrogram': cut(x, h, ...) ## S3 method for class 'dendrogram': print(x, digits, ...) ## S3 method for class 'dendrogram': rev(x) ## S3 method for class 'dendrogram': str(object, max.level = NA, digits.d = 3, give.attr = FALSE, wid = getOption("width"), nest.lev = 0, indent.str = "", stem = "--", ...) is.leaf(object) _A_r_g_u_m_e_n_t_s: object: any R object that can be made into one of class '"dendrogram"'. x: object of class '"dendrogram"'. hang: numeric scalar indicating how the _height_ of leaves should be computed from the heights of their parents; see 'plot.hclust'. type: type of plot. center: logical; if 'TRUE', nodes are plotted centered with respect to the leaves in the branch. Otherwise (default), plot them in the middle of all direct child nodes. edge.root: logical; if true, draw an edge to the root node. nodePar: a 'list' of plotting parameters to use for the nodes (see 'points') or 'NULL' by default which does not draw symbols at the nodes. The list may contain components named 'pch', 'cex', 'col', and/or 'bg' each of which can have length two for specifying separate attributes for _inner_ nodes and _leaves_. edgePar: a 'list' of plotting parameters to use for the edge 'segments' and labels (if there's an 'edgetext'). The list may contain components named 'col', 'lty' and 'lwd' (for the segments), 'p.col', 'p.lwd', and 'p.lty' (for the 'polygon' around the text) and 't.col' for the text color. As with 'nodePar', each can have length two for differentiating leaves and inner nodes. leaflab: a string specifying how leaves are labeled. The default '"perpendicular"' write text vertically (by default). '"textlike"' writes text horizontally (in a rectangle), and '"none"' suppresses leaf labels. dLeaf: a number specifying the *d*istance in user coordinates between the tip of a leaf and its label. If 'NULL' as per default, 3/4 of a letter width or height is used. horiz: logical indicating if the dendrogram should be drawn _horizontally_ or not. frame.plot: logical indicating if a box around the plot should be drawn, see 'plot.default'. h: height at which the tree is cut. xlim, ylim: optional x- and y-limits of the plot, passed to 'plot.default'. The defaults for these show the full dendrogram. ..., xlab, ylab, xaxt, yaxt: graphical parameters, or arguments for other methods. digits: integer specifying the precision for printing, see 'print.default'. max.level, digits.d, give.attr, wid, nest.lev, indent.str: arguments to 'str', see 'str.default()'. Note that 'give.attr = FALSE' still shows 'height' and 'members' attributes for each node. stem: a string used for 'str()' specifying the _stem_ to use for each dendrogram branch. _D_e_t_a_i_l_s: Warning: This documentation is preliminary. The dendrogram is directly represented as a nested list where each component corresponds to a branch of the tree. Hence, the first branch of tree 'z' is 'z[[1]]', the second branch of the corresponding subtree is 'z[[1]][[2]]' etc.. Each node of the tree carries some information needed for efficient plotting or cutting as attributes, of which only 'members', 'height' and 'leaf' for leaves are compulsory: '_m_e_m_b_e_r_s' total number of leaves in the branch '_h_e_i_g_h_t' numeric non-negative height at which the node is plotted. '_m_i_d_p_o_i_n_t' numeric horizontal distance of the node from the left border (the leftmost leaf) of the branch (unit 1 between all leaves). This is used for 'plot(*, center=FALSE)'. '_l_a_b_e_l' character; the label of the node '_x._m_e_m_b_e_r' for 'cut()$upper', the number of _former_ members; more generally a substitute for the 'members' component used for 'horizontal' (when 'horiz = FALSE', else 'vertical') alignment. '_e_d_g_e_t_e_x_t' character; the label for the edge leading to the node '_n_o_d_e_P_a_r' a named list (of length-1 components) specifying node-specific attributes for 'points' plotting, see the 'nodePar' argument above. '_e_d_g_e_P_a_r' a named list (of length-1 components) specifying attributes for 'segments' plotting of the edge leading to the node, and drawing of the 'edgetext' if available, see the 'edgePar' argument above. '_l_e_a_f' logical, if 'TRUE', the node is a leaf of the tree. 'cut.dendrogram()' returns a list with components '$upper' and '$lower', the first is a truncated version of the original tree, also of class 'dendrogram', the latter a list with the branches obtained from cutting the tree, each a 'dendrogram'. There are '[[', 'print', and 'str' methods for '"dendrogram"' objects where the first one (extraction) ensures that selecting sub-branches keeps the class. Objects of class '"hclust"' can be converted to class '"dendrogram"' using method 'as.dendrogram'. 'rev.dendrogram' simply returns the dendrogram 'x' with reversed nodes, see also 'reorder.dendrogram'. 'is.leaf(object)' is logical indicating if 'object' is a leaf (the most simple dendrogram). 'plotNode()' and 'plotNodeLimit()' are helper functions. _W_a_r_n_i_n_g: Some operations on dendrograms (including plotting) make use of recursion. For very deep trees It may be necessary to increase 'options("expressions")': if you do you are likely to need to set the C stack size larger than the OS default if possible (which it is not on Windows). _N_o_t_e: When using 'type = "triangle"', 'center = TRUE' often looks better. _S_e_e _A_l_s_o: 'order.dendrogram' also on the 'labels' method for dendrograms. _E_x_a_m_p_l_e_s: require(graphics); require(utils) hc <- hclust(dist(USArrests), "ave") (dend1 <- as.dendrogram(hc)) # "print()" method str(dend1) # "str()" method str(dend1, max = 2) # only the first two sub-levels op <- par(mfrow= c(2,2), mar = c(5,2,1,4)) plot(dend1) ## "triangle" type and show inner nodes: plot(dend1, nodePar=list(pch = c(1,NA), cex=0.8, lab.cex = 0.8), type = "t", center=TRUE) plot(dend1, edgePar=list(col = 1:2, lty = 2:3), dLeaf=1, edge.root = TRUE) plot(dend1, nodePar=list(pch = 2:1,cex=.4*2:1, col = 2:3), horiz=TRUE) dend2 <- cut(dend1, h=70) plot(dend2$upper) ## leafs are wrong horizontally: plot(dend2$upper, nodePar=list(pch = c(1,7), col = 2:1)) ## dend2$lower is *NOT* a dendrogram, but a list of .. : plot(dend2$lower[[3]], nodePar=list(col=4), horiz = TRUE, type = "tr") ## "inner" and "leaf" edges in different type & color : plot(dend2$lower[[2]], nodePar=list(col=1),# non empty list edgePar = list(lty=1:2, col=2:1), edge.root=TRUE) par(op) str(d3 <- dend2$lower[[2]][[2]][[1]]) ## "Zoom" in to the first dendrogram : plot(dend1, xlim = c(1,20), ylim = c(1,50)) nP <- list(col=3:2, cex=c(2.0, 0.75), pch= 21:22, bg= c("light blue", "pink"), lab.cex = 0.75, lab.col = "tomato") plot(d3, nodePar= nP, edgePar = list(col="gray", lwd=2), horiz = TRUE) addE <- function(n) { if(!is.leaf(n)) { attr(n, "edgePar") <- list(p.col="plum") attr(n, "edgetext") <- paste(attr(n,"members"),"members") } n } d3e <- dendrapply(d3, addE) plot(d3e, nodePar= nP) plot(d3e, nodePar= nP, leaflab = "textlike")