Learn R Programming

dendextend (version 0.16.1)

rect.dendrogram: Draw Rectangles Around a Dendrogram's Clusters

Description

Draws rectangles around the branches of a dendrogram highlighting the corresponding clusters. First the dendrogram is cut at a certain level, then a rectangle is drawn around selected branches.

Usage

rect.dendrogram(tree, k = NULL, which = NULL, x = NULL, h = NULL,
  border = 2, cluster = NULL, horiz = FALSE, density = NULL,
  angle = 45, text = NULL, text_cex = 1, text_col = 1, ...)

Arguments

tree
a dendrogram object.
k
Scalar. Cut the dendrogram such that exactly k clusters (if possible) are produced.
h
Scalar. Cut the dendrogram by cutting at height h. (k overrides h)
which
A vector selecting the clusters around which a rectangle should be drawn. which selects clusters by number (from left to right in the tree), Default is which = 1:k.
x
A vector selecting the clusters around which a rectangle should be drawn. x selects clusters containing the respective horizontal coordinates.
border
Vector with border colors for the rectangles.
cluster
Optional vector with cluster memberships as returned by cutree(dend_obj, k = k), can be specified for efficiency if already computed.
...
parameters passed to rect (such as lwd, lty, etc.)
horiz
logical (FALSE), indicating if the rectangles should be drawn horizontally or not (for when using plot(dend, horiz = TRUE) ) .
density
Passed to rect: the density of shading lines, in lines per inch. The default value of NULL means that no shading lines are drawn. A zero value of density means no shading lines whereas negative values (and NA) suppress
angle
Passed to rect: angle (in degrees) of the shading lines. (default is 45)
text
a character vector of labels to plot underneath the clusters. When NULL (default), no text is displayed.
text_cex
a numeric (scalar) value of the text's cex value.
text_col
a (scalar) value of the text's col(or) value.

Value

  • (Invisibly) returns a list where each element contains a vector of data points contained in the respective cluster.

source

This function is based on rect.hclust, with slight modifications to have it work with a dendrogram, as well as a few added features (e.g: ... to rect, and horiz) The idea of adding text and shading lines under the clusters comes from skullkey from here: http://stackoverflow.com/questions/4720307/change-dendrogram-leaves

See Also

rect.hclust, order.dendrogram, cutree.dendrogram

Examples

Run this code
set.seed(23235)
ss <- sample(1:150, 10 )
hc <- iris[ss,-5] %>% dist %>% hclust
dend <- hc %>% as.dendrogram

plot(dend)
rect.dendrogram(dend,2, border = 2)

plot(dend)
rect.dendrogram(dend, 3 , border = 1:3,
         density = 2, text = c("1", "b", "miao"), text_cex = 3)

plot(dend)
rect.dendrogram(dend,4, which = c(1,3), border = c(2,3))
rect.dendrogram(dend,4, x = 5, border = c(4))
rect.dendrogram(dend,3, border = 3, lwd = 2, lty = 2)
# now THIS, you can not do with the old rect.hclust
plot(dend, horiz = TRUE)
rect.dendrogram(dend,2, border = 2, horiz = TRUE)
rect.dendrogram(dend,4, border = 4, lty = 2, lwd = 3, horiz = TRUE)

Run the code above in your browser using DataLab