Introduction to dendextend

Author: Tal Galili ( Tal.Galili@gmail.com )

tl;dr: the dendextend package let's you create figures like this:

plot of chunk unnamed-chunk-2

Introduction

The dendextend package offers a set of functions for extending dendrogram objects in R, letting you visualize and compare trees of hierarchical clusterings, you can:

The goal of this document is to introduce you to the basic functions that dendextend provides, and show how they may be applied. We will make extensive use of “chaining” (explained next).

Prerequisites

Acknowledgement

This package was made possible by the the support of my thesis adviser Yoav Benjamini, as well as code contributions from many R users. They are:

#>  [1] "Tal Galili <tal.galili@gmail.com> [aut, cre, cph] (http://www.r-statistics.com)"                  
#>  [2] "Gavin Simpson [ctb]"                                                                              
#>  [3] "Gregory Jefferis <jefferis@gmail.com> [ctb] (imported code from his dendroextras package)"        
#>  [4] "Marco Gallotta [ctb] (a.k.a: marcog)"                                                             
#>  [5] "Johan Renaudie [ctb] (https://github.com/plannapus)"                                              
#>  [6] "R core team [ctb] (Thanks for the Infastructure, and code in the examples)"                       
#>  [7] "Kurt Hornik [ctb]"                                                                                
#>  [8] "Uwe Ligges [ctb]"                                                                                 
#>  [9] "Andrej-Nikolai Spiess [ctb]"                                                                      
#> [10] "Steve Horvath <SHorvath@mednet.ucla.edu> [ctb]"                                                   
#> [11] "Peter Langfelder <Peter.Langfelder@gmail.com> [ctb]"                                              
#> [12] "skullkey [ctb]"                                                                                   
#> [13] "Mark Van Der Loo <mark.vanderloo@gmail.com> [ctb] (https://github.com/markvanderloo d3dendrogram)"
#> [14] "Yoav Benjamini [ths]"

The design of the dendextend package (and this manual!) is heavily inspired by Hadley Wickham's work. Especially his text on writing an R package, the devtools package, and the dplyr package (specifically the use of chaining, and the Introduction text to dplyr).

Chaining

Function calls in dendextend often get a dendrogram and returns a (modified) dendrogram. This doesn't lead to particularly elegant code if you want to do many operations at once. The same is true even in the first stage of creating a dendrogram.

In order to construct a dendrogram, you will (often) need to go through several steps. You can either do so while keeping the intermediate results:

d1 <- c(1:5) # some data
d2 <- dist(d1)
d3 <- hclust(d2, method = "average")
dend <- as.dendrogram(d3)

Or, you can also wrap the function calls inside each other:

dend <- as.dendrogram(hclust(dist(c(1:5)), method = "average"))

However, both solutions are not ideal: the first solution includes redundant intermediate objects, while the second is difficult to read (since the order of the operations is from inside to out, while the arguments are a long way away from the function).

To get around this problem, dendextend encourages the use of the %>% (“pipe” or “chaining”) operator (imported from the magrittr package). This turns x %>% f(y) into f(x, y) so you can use it to rewrite (“chain”) multiple operations such that they can be read from left-to-right, top-to-bottom.

For example, the following will be written as it would be explained:

dend <- c(1:5) %>% # take the a vector from 1 to 5
         dist %>% # calculate a distance matrix, 
         hclust(method = "average") %>% # on it compute hierarchical clustering using the "average" method, 
         as.dendrogram # and lastly, turn that object into a dendrogram.

For more details, you may look at:

A dendrogram is a nested list of lists with attributes

The first step is working with dendrograms, is to understand that they are just a nested list of lists with attributes. Let us explore this for the following (tiny) tree:

# Create a dend:
dend <- 1:2 %>% dist %>% hclust %>% as.dendrogram
# and plot it:
dend %>% plot

plot of chunk unnamed-chunk-7

And here is its structure (a nested list of lists with attributes):

dend %>% unclass %>% str
#> List of 2
#>  $ : atomic [1:1] 1
#>   ..- attr(*, "label")= int 1
#>   ..- attr(*, "members")= int 1
#>   ..- attr(*, "height")= num 0
#>   ..- attr(*, "leaf")= logi TRUE
#>  $ : atomic [1:1] 2
#>   ..- attr(*, "label")= int 2
#>   ..- attr(*, "members")= int 1
#>   ..- attr(*, "height")= num 0
#>   ..- attr(*, "leaf")= logi TRUE
#>  - attr(*, "members")= int 2
#>  - attr(*, "midpoint")= num 0.5
#>  - attr(*, "height")= num 1
dend %>% class
#> [1] "dendrogram"

Installation

To install the stable version on CRAN use:

install.packages('dendextend')
install.packages('dendextendRcpp')

To install the GitHub version:

require2 <- function (package, ...) {
   if (!require(package)) install.packages(package); library(package)
}

## require2('installr')
## install.Rtools() # run this if you are using Windows and don't have Rtools installed

# Load devtools:
require2("devtools")
devtools::install_github('talgalili/dendextend')
require2("Rcpp")
devtools::install_github('talgalili/dendextendRcpp')

# Having colorspace is also useful, since it is used
# In various examples in the vignettes
require2("colorspace")

And then you may load the package using:

library(dendextend)
library(dendextendRcpp)

How to explore a dendrogram's parameters

Taking a first look at a dendrogram

For the following simple tree:

# Create a dend:
dend <- 1:5 %>% dist %>% hclust %>% as.dendrogram
# Plot it:
dend %>% plot

plot of chunk unnamed-chunk-9

Here are some basic parameters we can get:

dend %>% labels # get the labels of the tree
#> [1] 1 2 5 3 4
dend %>% nleaves # get the number of leaves of the tree
#> [1] 5
dend %>% nnodes # get the number of nodes in the tree (including leaves)
#> [1] 9
dend %>% head # A combination of "str" with "head"
#> --[dendrogram w/ 2 branches and 5 members at h = 4]
#>   |--[dendrogram w/ 2 branches and 2 members at h = 1]
#>   |  |--leaf 1 
#>   |  `--leaf 2 
#>   `--[dendrogram w/ 2 branches and 3 members at h = 2]
#>      |--leaf 5 
#>      `--[dendrogram w/ 2 branches and 2 members at h = 1]
#>         |--leaf 3 
#>         `--leaf 4 
#> etc...

Next let us look at more sophisticated outputs.

Getting nodes attributes in a depth-first search

When extracting (or inserting) attributes from a dendrogram's nodes, it is often in a “depth-first search”. Depth-first search is when an algorithm for traversing or searching tree or graph data structures. One starts at the root and explores as far as possible along each branch before backtracking.

Here is a plot of a tree, illustrating the order in which you should read the “nodes attributes”:

plot of chunk unnamed-chunk-11

We can get several nodes attributes using get_nodes_attr (notice the order corresponds with what is shown in the above figure):

# Create a dend:
dend <- 1:5 %>% dist %>% hclust %>% as.dendrogram
# Get various attributes
dend %>% get_nodes_attr("height") # node's height
#> [1] 4 1 0 0 2 0 1 0 0
dend %>% hang.dendrogram %>% get_nodes_attr("height") # node's height (after raising the leaves)
#> [1] 4.0 1.0 0.6 0.6 2.0 1.6 1.0 0.6 0.6
dend %>% get_nodes_attr("members") # number of members (leaves) under that node
#> [1] 5 2 1 1 3 1 2 1 1
dend %>% get_nodes_attr("midpoint") # how much "left" is this node from its left-most child's location
#> [1] 1.625 0.500    NA    NA 0.750    NA 0.500    NA    NA
dend %>% get_nodes_attr("leaf") # is this node a leaf
#> [1]   NA   NA TRUE TRUE   NA TRUE   NA TRUE TRUE
dend %>% get_nodes_attr("label") # what is the label on this node
#> [1] NA NA  1  2 NA  5 NA  3  4
dend %>% get_nodes_attr("nodePar") # empty (for now...)
#> [1] NA NA NA NA NA NA NA NA NA
dend %>% get_nodes_attr("edgePar") # empty (for now...)
#> [1] NA NA NA NA NA NA NA NA NA

A similar function for leaves only is get_leaves_attr

How to change a dendrogram

The “set” function

The fastest way to start changing a dendextend's parameters is by using the set function. It is written as: set(object, what, value), and accepts the following parameters:

  1. object: a dendrogram object,
  2. what: a character indicating what is the property of the tree that should be set/updated
  3. value: a vector with the value to set in the tree (the type of the value depends on the “what”). Many times, vectors which are too short are recycled.

The what parameter accepts many options, each uses some general function in the background. These options deal with labels, nodes and branches. They are:

Two simple trees to play with

For illustration purposes, we will create several small tree, and demonstrate these functions on them.

dend13 <- c(1:3) %>% # take some data
         dist %>% # calculate a distance matrix, 
         hclust(method = "average") %>% # on it compute hierarchical clustering using the "average" method, 
         as.dendrogram # and lastly, turn that object into a dendrogram.
# same, but for 5 leaves:
dend15 <- c(1:5) %>% dist %>% hclust(method = "average") %>% as.dendrogram

par(mfrow = c(1,2))
dend13 %>% plot(main="dend13")
dend15 %>% plot(main="dend15")
# we could have also used plot(dend)

plot of chunk unnamed-chunk-13

Setting a dendrogram's labels

We can get a vector with the tree's labels:

# get the labels:
dend15 %>% labels
#> [1] 1 2 5 3 4
# this is just like labels(dend)

Notice how the tree's labels are not 1 to 5 by order, since the tree happened to place them in a different order. We can change the names of the labels:

# change the labels, and then print them:
dend15 %>% set("labels", c(111:115)) %>% labels
#> [1] "111" "112" "113" "114" "115"
# could also be done using:
# labels(dend) <- c(111:115)

We can change the type of labels to be characters. Not doing so may be a source of various bugs and problems in many functions.

dend15 %>% labels
#> [1] 1 2 5 3 4
dend15 %>% set("labels_to_char") %>% labels
#> [1] "1" "2" "5" "3" "4"

We may also change their color and size:

par(mfrow = c(1,2))
dend15 %>% set("labels_col", "blue") %>% plot(main = "Change label's color") # change color 
dend15 %>% set("labels_cex", 2) %>% plot(main = "Change label's size") # change color 

plot of chunk unnamed-chunk-17

The function recycles, from left to right, the vector of values we give it. We can use this to create more complex patterns:

# Produce a more complex dendrogram:
dend15_2 <- dend15 %>% 
   set("labels", c(111:115)) %>%    # change labels
   set("labels_col", c(1,2,3)) %>%  # change color 
   set("labels_cex", c(2,1))        # change size

par(mfrow = c(1,2))
dend15 %>% plot(main = "Before")
dend15_2 %>% plot(main = "After")

plot of chunk unnamed-chunk-18

Notice how these “labels parameters” are nested within the nodePar attribute:

# looking at only the left-most node of the "after tree":
dend15_2[[1]][[1]] %>% unclass %>% str 
#>  atomic [1:1] 1
#>  - attr(*, "label")= chr "111"
#>  - attr(*, "members")= int 1
#>  - attr(*, "height")= num 0
#>  - attr(*, "leaf")= logi TRUE
#>  - attr(*, "nodePar")=List of 3
#>   ..$ lab.col: num 1
#>   ..$ pch    : logi NA
#>   ..$ lab.cex: num 2
# looking at only the nodePar attributes in this sub-tree:
dend15_2[[1]][[1]] %>% get_nodes_attr("nodePar") 
#>         [,1]
#> lab.col 1   
#> pch     NA  
#> lab.cex 2

When it comes to color, we can also set the parameter “k”, which will cut the tree into k clusters, and assign a different color to each label (based on its cluster):

par(mfrow = c(1,2))
dend15 %>% set("labels_cex", 2) %>% set("labels_col", value = c(3,4)) %>% 
   plot(main = "Recycles color \nfrom left to right")
dend15 %>% set("labels_cex", 2) %>% set("labels_col", value = c(3,4), k=2) %>% 
   plot(main = "Color labels \nper cluster")
abline(h = 2, lty = 2)

plot of chunk unnamed-chunk-20

Setting a dendrogram's nodes/leaves (points)

Each node in a tree can be represented and controlled using the assign_values_to_nodes_nodePar, and for the special case of the nodes of leaves, the assign_values_to_leaves_nodePar function is more appropriate (and faster) to use. We can control the following properties: pch (point type), cex (point size), and col (point color). For example:

par(mfrow = c(2,3))
dend13 %>% set("nodes_pch", 19) %>% plot(main = "(1) Show the\n nodes (as a dot)") #1
dend13 %>% set("nodes_pch", 19) %>% set("nodes_cex", 2) %>% 
   plot(main = "(2) Show (larger)\n nodes") #2
dend13 %>% set("nodes_pch", 19) %>% set("nodes_cex", 2) %>% set("nodes_col", 3) %>% 
   plot(main = "(3) Show (larger+colored)\n nodes") #3

dend13 %>% set("leaves_pch", 19) %>% plot(main = "(4) Show the\n leaves (as a dot)") #4
dend13 %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>% 
   plot(main = "(5) Show (larger)\n leaves") #5
dend13 %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>% set("leaves_col", 3) %>% 
   plot(main = "(6) Show (larger+colored)\n leaves") #6

plot of chunk unnamed-chunk-21

And with recycling we can produce more complex outputs:

par(mfrow = c(1,2))
dend15 %>% set("nodes_pch", c(19,1,4)) %>% set("nodes_cex", c(2,1,2)) %>% set("nodes_col", c(3,4)) %>% 
   plot(main = "Adjust nodes")
dend15 %>% set("leaves_pch", c(19,1,4)) %>% set("leaves_cex", c(2,1,2)) %>% set("leaves_col", c(3,4)) %>%
   plot(main = "Adjust nodes\n(but only for leaves)")

plot of chunk unnamed-chunk-22

Notice how recycling works in a depth-first order (which is just left to right, when we only adjust the leaves). Here are the node's parameters after adjustment:

dend15 %>% set("nodes_pch", c(19,1,4)) %>%
   set("nodes_cex", c(2,1,2)) %>% set("nodes_col", c(3,4)) %>% get_nodes_attr("nodePar")
#>     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
#> pch   19    1    4   19    1    4   19    1    4
#> cex    2    1    2    2    1    2    2    1    2
#> col    3    4    3    4    3    4    3    4    3

We can also change the height of of the leaves by using the hang.dendrogram function:

par(mfrow = c(1,3))
dend13 %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>% set("leaves_col", 2) %>% # adjust the leaves
   hang.dendrogram %>% # hang the leaves
   plot(main = "Hanging a tree")
dend13 %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>% set("leaves_col", 2) %>% # adjust the leaves
   hang.dendrogram(hang_height = .6) %>% # hang the leaves (at some height)
   plot(main = "Hanging a tree (but lower)")
dend13 %>% set("leaves_pch", 19) %>% set("leaves_cex", 2) %>% set("leaves_col", 2) %>% # adjust the leaves
   hang.dendrogram %>% # hang the leaves
   hang.dendrogram(hang = -1) %>% # un-hanging the leaves
   plot(main = "Not hanging a tree")

plot of chunk unnamed-chunk-24

An example of what this function does to the leaves heights:

dend13 %>% get_leaves_attr("height")
#> [1] 0 0 0
dend13 %>% hang.dendrogram %>% get_leaves_attr("height")
#> [1] 1.35 0.85 0.85

We can also control the general heights of nodes using raise.dendrogram:

par(mfrow = c(1,3))
dend13 %>% plot(main = "First tree", ylim = c(0,3))
dend13 %>% 
   raise.dendrogram (-1) %>% 
   plot(main = "One point lower", ylim = c(0,3))
dend13 %>% 
   raise.dendrogram (1) %>% 
   plot(main = "One point higher", ylim = c(0,3))

plot of chunk unnamed-chunk-26

If you wish to make the branches under the root have the same height, you can use the flatten.dendrogram function.

Setting a dendrogram's branches

Adjusting all branches

Similar to adjusting nodes, we can also control line width (lwd), line type (lty), and color (col) for branches:

par(mfrow = c(1,3))
dend13 %>% set("branches_lwd", 4) %>% plot(main = "Thick branches")
dend13 %>% set("branches_lty", 3) %>% plot(main = "Dashed branches")
dend13 %>% set("branches_col", 2) %>% plot(main = "Red branches")

plot of chunk unnamed-chunk-27

We may also use recycling to create more complex patterns:

# Produce a more complex dendrogram:
dend15 %>% 
   set("branches_lwd", c(4,1)) %>%    
   set("branches_lty", c(1,1,3)) %>%  
   set("branches_col", c(1,2,3)) %>% 
   plot(main = "Complex branches", edge.root = TRUE)

plot of chunk unnamed-chunk-28

Notice how the first branch (the root) is considered when going through and creating the tree, but it is ignored in the actual plotting (this is actually a “missing feature” in plot.dendrogram).

Coloring branches based on clustering

We may also control the colors of the branches based on using clustering:

par(mfrow = c(1,2))
dend15 %>% set("branches_k_color", k = 3) %>% plot(main = "Nice defaults")
dend15 %>% set("branches_k_color", value = 3:1, k = 3) %>% 
   plot(main = "Controlling branches' colors\n(via clustering)")

plot of chunk unnamed-chunk-29

# This is like using the `color_branches` function

Adjusting branches based on labels

The most powerful way to control branches is through the branches_attr_by_labels function (with variations through the set function). The function allows you to change col/lwd/lty of branches if they match some “labels condition”. Follow carefully:

par(mfrow = c(1,2))
dend15 %>% set("by_labels_branches_col", value = c(1,4)) %>% 
   plot(main = "Adjust the branch\n if ALL (default) of its\n labels are in the list")
dend15 %>% set("by_labels_branches_col", value = c(1,4), type = "any") %>% 
   plot(main = "Adjust the branch\n if ANY of its\n labels are in the list")

plot of chunk unnamed-chunk-30

We can use this to change the size/type/color of the branches:

# Using "Inf" in "TF_values" means to let the parameters stay as they are.
par(mfrow = c(1,3))
dend15 %>% set("by_labels_branches_col", value = c(1,4), TF_values = c(3,Inf)) %>% 
   plot(main = "Change colors")
dend15 %>% set("by_labels_branches_lwd", value = c(1,4), TF_values = c(8,1)) %>% 
   plot(main = "Change line width")
dend15 %>% set("by_labels_branches_lty", value = c(1,4), TF_values = c(3,Inf)) %>% 
   plot(main = "Change line type")

plot of chunk unnamed-chunk-31

Changing a dendrogram's structure

Rotation

A dendrogram is an object which can be rotated on its hinges without changing its topology. Rotating a dendrogram in base R can be done using the reorder function. The problem with this function is that it is not very intuitive. For this reason the rotate function was written. It has two main arguments: the “object” (a dendrogram), and the “order” we wish to rotate it by. The “order” parameter can be either a numeric vector, used in a similar way we would order a simple character vector. Or, the order parameter can also be a character vector of the labels of the tree, given in the new desired order of the tree. It is also worth noting that some order are impossible to achieve for a given tree's topology. In such cases, the function will do its “best” to get as close as possible to the requested rotation.

par(mfrow = c(1,3))
dend15 %>% 
   set("labels_colors") %>% 
   set("branches_k_color") %>% 
   plot(main = "First tree")
dend15 %>%
   set("labels_colors") %>% 
   set("branches_k_color") %>% 
   rotate(as.character(5:1)) %>% #rotate to match labels new order
   plot(main = "Rotated tree\n based on labels")
dend15 %>% 
   set("labels_colors") %>% 
   set("branches_k_color") %>% 
   rotate(5:1) %>% # the fifth label to go first is "4"
   plot(main = "Rotated tree\n based on order")

plot of chunk unnamed-chunk-32

Unbranching

We can unbranch a tree:

par(mfrow = c(1,3))
dend15 %>% plot(main = "First tree", ylim = c(0,3))
dend15 %>% 
   unbranch %>% 
   plot(main = "Unbranched tree", ylim = c(0,3))
dend15 %>% 
   unbranch(2) %>% 
   plot(main = "Unbranched tree (2)", ylim = c(0,3))

plot of chunk unnamed-chunk-33

Pruning

We can prune a tree based on the labels:

par(mfrow = c(1,2))
dend15 %>% set("labels_colors") %>% 
   plot(main = "First tree", ylim = c(0,3))
dend15 %>% set("labels_colors") %>%
   prune(c("1","5")) %>% 
   plot(main = "Prunned tree", ylim = c(0,3))

plot of chunk unnamed-chunk-34

For pruning two trees to have matching labels, we can use the intersect_trees function:

par(mfrow = c(1,2))
dend_intersected <- intersect_trees(dend13, dend15)
dend_intersected[[1]] %>% plot
dend_intersected[[2]] %>% plot

plot of chunk unnamed-chunk-35

Adding extra bars and rectangles

Adding colored rectangles

Earlier we have seen how to highlight clusters in a dendrogram by coloring branches. We can also draw rectangles around the branches of a dendrogram in order to highlight the corresponding clusters. First the dendrogram is cut at a certain level, then a rectangle is drawn around selected branches. This is done using the rect.dendrogram, which is modeled based on the rect.hclust function. One advantage of rect.dendrogram over rect.hclust, is that it also works on horizontally plotted trees:

layout(t(c(1,1,1,2,2)))

dend15 %>% set("branches_k_color") %>% plot
dend15 %>% rect.dendrogram(k=3, 
                           border = 8, lty = 5, lwd = 2)

dend15 %>% set("branches_k_color") %>% plot(horiz = TRUE)
dend15 %>% rect.dendrogram(k=3, horiz = TRUE,
                           border = 8, lty = 5, lwd = 2)

plot of chunk unnamed-chunk-36

Adding colored bars

Adding colored bars to a dendrogram may be useful to show clusters or some outside categorization of the items. For example:

is_odd <- ifelse(labels(dend15) %% 2, 2,3)
is_345 <- ifelse(labels(dend15) > 2, 3,4)
is_12 <- ifelse(labels(dend15) <= 2, 3,4)
k_3 <- cutree(dend15,k = 3, order_clusters_as_data = FALSE) 
# The FALSE above makes sure we get the clusters in the order of the
# dendrogram, and not in that of the original data. It is like:
# cutree(dend15, k = 3)[order.dendrogram(dend15)]
the_bars <- cbind(is_odd, is_345, is_12, k_3)
the_bars[the_bars==2] <- 8

dend15 %>% plot
colored_bars(colors = the_bars, dend = dend15)

plot of chunk unnamed-chunk-37

Enhancing other packages

The dendextend package aims to extend and enhance features from the R ecosystem. Let us take a look at several examples.

DendSer

The DendSer package helps in re-arranging a dendrogram to optimize visualization-based cost functions. Until now it was only used for hclust objects, but it can easily be connected to dendrogram objects by trying to turn the dendrogram into hclust, on which it runs DendSer. This can be used to rotate the dendrogram easily by using the rotate_DendSer function:

par(mfrow = c(1,2))
library(DendSer)
#> Loading required package: gclus
#> Loading required package: cluster
#> 
#> Attaching package: 'gclus'
#> 
#> The following object is masked from 'package:dendextend':
#> 
#>     order.hclust
#> 
#> Loading required package: seriation
DendSer.dendrogram(dend15)
#> [1] 1 2 5 4 3
dend15 %>% color_branches %>%                      plot
dend15 %>% color_branches %>% rotate_DendSer %>%   plot

plot of chunk unnamed-chunk-38

gplots

The gplots package brings us the heatmap.2 function. In it, we can use our modified dendrograms to get more informative heat-maps:

library(gplots)

data(mtcars) 
x  <- as.matrix(mtcars)

heatmap.2(x)

plot of chunk unnamed-chunk-39

# now let's spice up the dendrograms a bit:
Rowv  <- x %>% dist %>% hclust %>% as.dendrogram %>%
   set("branches_k_color", k = 3) %>% set("branches_lwd", 4) %>%
   rotate_DendSer(ser_weight = dist(x))
Colv  <- x %>% t %>% dist %>% hclust %>% as.dendrogram %>%
   set("branches_k_color", k = 2) %>% set("branches_lwd", 4) %>%
   rotate_DendSer(ser_weight = dist(t(x)))

heatmap.2(x, Rowv = Rowv, Colv = Colv)

plot of chunk unnamed-chunk-39

dynamicTreeCut

The cutreeDynamic function offers a wrapper for two methods of adaptive branch pruning of hierarchical clustering dendrograms. The results of which can now be visualized by both updating the branches, as well as using the colored_bars function (which was adjusted for use with plots of dendrograms):

# let's get the clusters
library(dynamicTreeCut)
data(iris)
x  <- iris[,-5] %>% as.matrix
hc <- x %>% dist %>% hclust
dend <- hc %>% as.dendrogram 

# Find special clusters:
clusters <- cutreeDynamic(hc, distM = as.matrix(dist(x)), method = "tree")
# we need to sort them to the order of the dendrogram:
clusters <- clusters[order.dendrogram(dend)]
clusters_numbers <- unique(clusters) - (0 %in% clusters)
n_clusters <- length(clusters_numbers)

library(colorspace)
cols <- rainbow_hcl(n_clusters)
true_species_cols <- rainbow_hcl(3)[as.numeric(iris[,][order.dendrogram(dend),5])]
dend2 <- dend %>% 
         branches_attr_by_clusters(clusters, values = cols) %>% 
         color_labels(col =   true_species_cols)
plot(dend2)
clusters <- factor(clusters)
levels(clusters)[-1]  <- cols[-5][c(1,4,2,3)] 
   # Get the clusters to have proper colors.
   # fix the order of the colors to match the branches.
colored_bars(clusters, dend, y_scale = 1)

plot of chunk unnamed-chunk-40

pvclust

The pvclust library calculates “p-values”“ for hierarchical clustering via multiscale bootstrap re-sampling. Hierarchical clustering is done for given data and p-values are computed for each of the clusters. The dendextend package let's us reproduce the plot from pvclust, but with a dendrogram (instead of an hclust object), which also lets us extend the visualization.

par(mfrow = c(1,2))

library(pvclust)
data(lung) # 916 genes for 73 subjects
set.seed(13134)
result <- pvclust(lung[1:100, 1:10], 
                  method.dist="cor", method.hclust="average", nboot=10)

# with pvrect
plot(result)
pvrect(result)

# with a dendrogram of pvrect
dend <- as.dendrogram(result)
result %>% as.dendrogram %>% 
   plot(main = "Cluster dendrogram with AU/BP values (%)\n reproduced plot with dendrogram")
result %>% text
result %>% pvrect

plot of chunk unnamed-chunk-41

Let's color and thicken the branches based on the p-values:

par(mfrow = c(2,2))

# with a modified dendrogram of pvrect
dend %>% pvclust_show_signif(result) %>% 
   plot(main = "Cluster dendrogram \n bp values are highlighted by signif")

dend %>% pvclust_show_signif(result, show_type = "lwd") %>% 
   plot(main = "Cluster dendrogram with AU/BP values (%)\n bp values are highlighted by signif")
result %>% text
result %>% pvrect(alpha=0.95)


dend %>% pvclust_show_signif_gradient(result) %>% 
   plot(main = "Cluster dendrogram with AU/BP values (%)\n bp values are colored by signif")

dend %>%
   pvclust_show_signif_gradient(result) %>%
   pvclust_show_signif(result) %>%
   plot(main = "Cluster dendrogram with AU/BP values (%)\n bp values are colored+highlighted by signif")
result %>% text
result %>% pvrect(alpha=0.95)

plot of chunk unnamed-chunk-42

Comparing two dendrograms

dendlist

A dendlist is a function which produces the dendlist class. It accepts several dendrograms and/or dendlist objects and chain them all together. This function aim to help with the usability of comparing two or more dendrograms.

dend15 <- c(1:5) %>% dist %>% hclust(method = "average") %>% as.dendrogram
dend15 <- dend15 %>% set("labels_to_char")
dend51 <- dend15 %>% set("labels", as.character(5:1)) %>% match_order_by_labels(dend15)
dends_15_51 <- dendlist(dend15, dend51)
dends_15_51
#> [[1]]
#> 'dendrogram' with 2 branches and 5 members total, at height 2.5 
#> 
#> [[2]]
#> 'dendrogram' with 2 branches and 5 members total, at height 2.5 
#> 
#> attr(,"class")
#> [1] "dendlist"
head(dends_15_51)
#> ============
#>  dend  1 
#>  ---------
#> --[dendrogram w/ 2 branches and 5 members at h = 2.5]
#>   |--[dendrogram w/ 2 branches and 2 members at h = 1]
#>   |  |--leaf "1" 
#>   |  `--leaf "2" 
#>   `--[dendrogram w/ 2 branches and 3 members at h = 1.5]
#>      |--leaf "5" 
#>      `--[dendrogram w/ 2 branches and 2 members at h = 1]
#>         |--leaf "3" 
#>         `--leaf "4" 
#> etc... 
#> ============
#>  dend  2 
#>  ---------
#> --[dendrogram w/ 2 branches and 5 members at h = 2.5]
#>   |--[dendrogram w/ 2 branches and 2 members at h = 1]
#>   |  |--leaf "5" 
#>   |  `--leaf "4" 
#>   `--[dendrogram w/ 2 branches and 3 members at h = 1.5]
#>      |--leaf "3" 
#>      `--[dendrogram w/ 2 branches and 2 members at h = 1]
#>         |--leaf "2" 
#>         `--leaf "1" 
#> etc...

The function match_order_by_labels makes sure that the order in the leaves corresponds to the same labels in both trees.

tanglegram

A tanglegram plot gives two dendrogram (with the same set of labels), one facing the other, and having their labels connected by lines. Tanglegram can be used for visually comparing two methods of Hierarchical clustering, and are sometimes used in biology when comparing two phylogenetic trees.

Here is an example of creating a tanglegram using dendextend:

tanglegram(dends_15_51)

plot of chunk unnamed-chunk-44

# Same as using:
# plot(dends_15_51) # since there is a plot method for dendlist
# and also: 
# tanglegram(dend15, dend51)

We may wish to improve the layout of the trees. For this we have the entanglement, to measure the quality of the alignment of the two trees in the tanglegram layout, and the untangle function, for improving it.

dends_15_51 %>% entanglement # lower is better
#> [1] 0.9167
dends_15_51 %>% untangle(method = "DendSer") %>% entanglement # lower is better
#> [1] 0.223

Entanglement is measured by giving the left tree's labels the values of 1 till tree size, and than match these numbers with the right tree. Now, entanglement is the L norm distance between these two vectors. That is, we take the sum of the absolute difference (each one in the power of L). e.g: sum(abs(x-y)**L). And this is divided by the "worst case” entanglement level (e.g: when the right tree is the complete reverse of the left tree).

L tells us which penalty level we are at (L0, L1, L2, partial L's etc). L>1 means that we give a big penalty for sharp angles. While L->0 means that any time something is not a straight horizontal line, it gets a large penalty If L=0.1 it means that we much prefer straight lines over non straight lines

Finding an optimal rotation for the tanglegram of two dendrogram is a hard problem. This problem is also harder for larger trees.

Let's see how well some untangle methods can do.

Without doing anything:

x <- dends_15_51 
x %>% plot(main = paste("entanglement =", round(entanglement(x), 2)))

plot of chunk unnamed-chunk-46

Using DendSer:

x <- dends_15_51 %>% untangle(method = "DendSer") 
x %>% plot(main = paste("entanglement =", round(entanglement(x), 2)))

plot of chunk unnamed-chunk-47

One solution for improving the tanglegram would be to randomly search the rotated tree space for a better solution. Here is how to use a random search:

x <- dends_15_51 %>% untangle(method = "random", R = 10) 
x %>% plot(main = paste("entanglement =", round(entanglement(x), 2)))

plot of chunk unnamed-chunk-48

We can see we already got something better. An advantage of the random search is the ability to create many many trees and compare them to find the best pair.

Let's use a greedy forward step wise rotation of the two trees (first the left, then the right, and so on), to see if we can find a better solution for comparing the two trees. Notice that this may take some time to run (the larger the tree, the longer it would take), but we can limit the search for smaller k's, and see what improvement that can bring us using step2side (slowest):

x <- dends_15_51 %>% untangle(method = "step2side") 
x %>% plot(main = paste("entanglement =", round(entanglement(x), 2)))

plot of chunk unnamed-chunk-49

We got perfect entanglement (0).

Correlation measures

Baker's Gamma Index

Baker's Gamma Index (see baker's paper from 1974) is a measure of association (similarity) between two trees of Hierarchical clustering (dendrograms). It is defined as the rank correlation between the stages at which pairs of objects combine in each of the two trees.

Or more detailed: It is calculated by taking two items, and see what is the highest possible level of k (number of cluster groups created when cutting the tree) for which the two item still belongs to the same tree. That k is returned, and the same is done for these two items for the second tree. There are n over 2 combinations of such pairs of items from the items in the tree, and all of these numbers are calculated for each of the two trees. Then, these two sets of numbers (a set for the items in each tree) are paired according to the pairs of items compared, and a Spearman correlation is calculated.

The value can range between -1 to 1. With near 0 values meaning that the two trees are not statistically similar. For exact p-value one should use a permutation test. One such option will be to permute over the labels of one tree many times, calculating the distribution under the null hypothesis (keeping the trees topologies constant).

Notice that this measure is not affected by the height of a branch but only of its relative position compared with other branches.

cor_bakers_gamma(dend15, dend51)
#> [1] 0.3372

Even that we can reach perfect entanglement, Baker's gamma shows us that the tree's topology is not identical. As opposed with the correlation of a tree with itself:

cor_bakers_gamma(dend15, dend15)
#> [1] 1

Since the observations creating the Baker's Gamma Index of such a measure are correlated, we need to perform a permutation test for the calculation of the statistical significance of the index. Let's look at the distribution of Baker's Gamma Index under the null hypothesis (assuming fixed tree topologies). This will be different for different tree structures and sizes. Here are the results when the compared tree is itself (after shuffling its own labels), and when comparing tree 1 to the shuffled tree 2:

set.seed(23235)
the_cor <- cor_bakers_gamma(dend15, dend15)
the_cor2 <- cor_bakers_gamma(dend15, dend51)
the_cor
#> [1] 1
the_cor2
#> [1] 0.3372
R <- 100
cor_bakers_gamma_results <- numeric(R)
dend_mixed <- dend15
for(i in 1:R) {
   dend_mixed <- sample.dendrogram(dend_mixed, replace = FALSE)
   cor_bakers_gamma_results[i] <- cor_bakers_gamma(dend15, dend_mixed)
}
plot(density(cor_bakers_gamma_results),
     main = "Baker's gamma distribution under H0",
     xlim = c(-1,1))
abline(v = 0, lty = 2)
abline(v = the_cor, lty = 2, col = 2)
abline(v = the_cor2, lty = 2, col = 4)
legend("topleft", legend = c("cor", "cor2"), fill = c(2,4))
round(sum(the_cor2 < cor_bakers_gamma_results)/ R, 4)
#> [1] 0.13
title(sub = paste("One sided p-value:",
                  "cor =",  round(sum(the_cor < cor_bakers_gamma_results)/ R, 4),
                  " ; cor2 =",  round(sum(the_cor2 < cor_bakers_gamma_results)/ R, 4)
                  ))

plot of chunk unnamed-chunk-52

We can see that we do not have enough evidence that dend15 and dend51 are significantly “similar” (i.e.: with a correlation larger than 0).

We can also build a bootstrap confidence interval, using sample.dendrogram, for the correlation. This function can be very slow for larger trees, so make sure you use if carefully:

dend1 <- dend15
dend2 <- dend51

set.seed(23801)

R <- 100
dend1_labels <- labels(dend1)
dend2_labels <- labels(dend2)
cor_bakers_gamma_results <- numeric(R)
for(i in 1:R) {
   sampled_labels <- sample(dend1_labels, replace = TRUE)
   # members needs to be fixed since it will be later used in nleaves
   dend_mixed1 <- sample.dendrogram(dend1, 
                                    dend_labels=dend1_labels,
                                    fix_members=TRUE,fix_order=TRUE,fix_midpoint=FALSE,
                                    replace = TRUE, sampled_labels=sampled_labels
                                      )
   dend_mixed2 <- sample.dendrogram(dend2, dend_labels=dend2_labels,
                                    fix_members=TRUE,fix_order=TRUE,fix_midpoint=FALSE,
                                    replace = TRUE, sampled_labels=sampled_labels
                                      )                                    
   cor_bakers_gamma_results[i] <- cor_bakers_gamma(dend_mixed1, dend_mixed2, warn = FALSE)
}


# here is the tanglegram
tanglegram(dend1, dend2)

plot of chunk unnamed-chunk-53

# And here is the tanglegram for one sample of our trees:
dend_mixed1 <- rank_order.dendrogram(dend_mixed1)
dend_mixed2 <- rank_order.dendrogram(dend_mixed2)
dend_mixed1 <- fix_members_attr.dendrogram(dend_mixed1)
dend_mixed2 <- fix_members_attr.dendrogram(dend_mixed2)
tanglegram(dend_mixed1, dend_mixed2)

plot of chunk unnamed-chunk-53

cor_bakers_gamma(dend_mixed1, dend_mixed2, warn = FALSE)
#> [1] 1
CI95 <- quantile(cor_bakers_gamma_results, probs=c(.025,.975))
CI95
#>   2.5%  97.5% 
#> 0.3372 1.0000
par(mfrow = c(1,1))
plot(density(cor_bakers_gamma_results),
     main = "Baker's gamma bootstrap distribution",
     xlim = c(-1,1))
abline(v = CI95, lty = 2, col = 3)
abline(v = cor_bakers_gamma(dend1, dend2), lty = 2, col = 2)
legend("topleft", legend =c("95% CI", "Baker's Gamma Index"), fill = c(3,2))

plot of chunk unnamed-chunk-53

The bootstrap sampling can do weird things with small trees. In this case we had many times that the two trees got perfect correlation. The usage and interpretation should be done carefully!

Cophenetic correlation

The cophenetic distance between two observations that have been clustered is defined to be the inter-group dissimilarity at which the two observations are first combined into a single cluster. This distance has many ties and restrictions. The cophenetic correlation (see sokal 1962) is the correlation between two cophenetic distance matrices of two trees.

The value can range between -1 to 1. With near 0 values meaning that the two trees are not statistically similar. For exact p-value one should result to a permutation test. One such option will be to permute over the labels of one tree many times, and calculating the distribution under the null hypothesis (keeping the trees topologies constant).

cor_cophenetic(dend15, dend51)
#> [1] 0.3125

The function cor_cophenetic is faster than cor_bakers_gamma, and might be preferred for that reason.

The Fowlkes-Mallows Index and the Bk plot

The Fowlkes-Mallows Index

The Fowlkes-Mallows Index (see fowlkes 1983) (FM Index, or Bk) is a measure of similarity between two clusterings. The FM index ranges from 0 to 1, a higher value indicates a greater similarity between the two clusters.

The dendextend package allows the calculation of FM-Index, its expectancy and variance under the null hypothesis, and a creation of permutations of the FM-Index under H0. Thanks to the profdpm package, we have another example of calculating the FM (though it does not offer the expectancy and variance under H0):

hc1 <- hclust(dist(iris[,-5]), "com")
hc2 <- hclust(dist(iris[,-5]), "single")

# FM index of a cluster with himself is 1:
FM_index(cutree(hc1, k=3), cutree(hc1, k=3))
#> [1] 1
#> attr(,"E_FM")
#> [1] 0.3722
#> attr(,"V_FM")
#> [1] 5.985e-05
# FM index of two clusterings:
FM_index(cutree(hc1, k=3), cutree(hc2, k=3)) 
#> [1] 0.806
#> attr(,"E_FM")
#> [1] 0.4462
#> attr(,"V_FM")
#> [1] 6.464e-05
# we got a value far above the expected under H0

# Using the R code:
FM_index_R(cutree(hc1, k=3), cutree(hc2, k=3))
#> [1] 0.806
#> attr(,"E_FM")
#> [1] 0.4462
#> attr(,"V_FM")
#> [1] 6.464e-05
# Or wrapping the code from profdpm: (notice the NA's)
FM_index_profdpm(cutree(hc1, k=3), cutree(hc2, k=3))
#> Loading required package: profdpm
#> [1] 0.806
#> attr(,"E_FM")
#> [1] NA
#> attr(,"V_FM")
#> [1] NA

The Bk plot

In the Bk method we calculate the FM Index (Bk) for each k (k=2,3,…,n-1) number of clusters, giving the association between the two trees when each is cut to have k groups. The similarity between two hierarchical clustering dendrograms, can be investigated, using the (k,Bk) plot: For every level of splitting of the two dendrograms which produces k clusters in each tree, the plot shows the number Bk, and therefore enables the investigation of potential nuances in the structure of similarity. The Bk measures the number of pairs of items which are in the same cluster in both dendrograms, one of the clusters in one of the trees and one of the clusters in the other tree, divided by the geometric mean of the number of pairs of items which are in the same cluster in each tree. Namely, \({a_{uv}} = 1\left( {or{\rm{ }}{{\rm{b}}_{uv}} = 1} \right)\) if the items u and v are in the same cluster in the first tree (second tree), when it is cut so to give k clusters, and otherwise 0:

\[{FM_k} = {B_k} = \frac{{\sum\limits_{}^{} {{a_{uv}}{b_{uv}}} }}{{\sqrt {\sum\limits_{}^{} {{a_{uv}}} \sum\limits_{}^{} {{b_{uv}}} } }}\]

The Bk measure can be plotted for every value of k (except k=n) in order to create the “(k,Bk) plot”. The plot compares the similarity of the two trees for different cuts. The mean and variance of Bk, under the null hypothesis (that the two trees are not “similar”), and under the assumption that the margins of the matching matrix are fixed, are given in Fowlkes and Mallows (see fowlkes 1983). They allow making inference on whether the results obtained are different from what would have been expected under the null hypothesis (of now particular order of the trees' labels).

The Bk and the Bk_plot functions allow the calculation of the FM-Index for a range of k values on two trees. Here are examples:

set.seed(23235)
ss <- TRUE # sample(1:150, 30 ) # TRUE #
hc1 <- hclust(dist(iris[ss,-5]), "com")
hc2 <- hclust(dist(iris[ss,-5]), "single")
dend1 <- as.dendrogram(hc1)
dend2 <- as.dendrogram(hc2)
#    cutree(tree1)   

# It works the same for hclust and dendrograms:
Bk(hc1, hc2, k = 3)
#> $`3`
#> [1] 0.806
#> attr(,"E_FM")
#> [1] 0.4462
#> attr(,"V_FM")
#> [1] 6.464e-05
Bk(dend1, dend2, k = 3)
#> $`3`
#> [1] 0.806
#> attr(,"E_FM")
#> [1] 0.4462
#> attr(,"V_FM")
#> [1] 6.464e-05

The Bk plot:

Bk_plot(hc1, hc2, main = "WRONG Bk plot \n(due to the way cutree works with ties in hclust)", warn = FALSE)

plot of chunk unnamed-chunk-57

Bk_plot(dend1, dend2, main = "CORRECT Bk plot \n(based on dendrograms)")

plot of chunk unnamed-chunk-57

dendextendRcpp - on the need for speed

Since dendrogram objects are often revised through the use of recursion, R often falls short when it comes to speed. In various function which are intended to run only once (such as labels.color), this does not bother us, and we are content at quickly writing the function with R and letting it handle many extreme cases.

However, some functions are intended to be used many times, and in such cases are well served to be handled with C++. Thanks the the Rcpp package, we can comfortably extend several core functions of the dendextend package. These modifications are included in a separate add-on package called “dendextendRcpp” by Tal Galili (with essential contributions by Dirk Eddelbuettel, Romain Francois, and Kevin Ushey). When loading dendextendRcpp, it changes (and speeds up) several R functions from dendextend, these are: get_branches_heights, heights_per_k.dendrogram, cut_lower_fun and labels.dendrogram. Speed gains can range between 3 times to 100 times faster, depending on the size of the tree. These functions are regularly used by other functions in dendextend.

It is highly recommended to install dendextendRcpp for regular use, and it is available on CRAN:

install.packages("dendextendRcpp")

Extras

The Iris dataset

The famous (Fisher's or Anderson's) iris data set gives the measurements in centimeters of the variables sepal length and width and petal length and width, respectively, for 50 flowers from each of 3 species of iris. The species are Iris setosa, versicolor, and virginica. (from ?iris)

The Iris flower data set is fun for learning supervised classification algorithms, and is known as a difficult case for unsupervised learning. This is easily seen through the following Scatter PLot Matrix:

data(iris)

species_labels <- iris[,5]
library(colorspace) # get nice colors
species_col <- rev(rainbow_hcl(3))[as.numeric(species_labels)]

# Plot a SPLOM:
pairs(iris[,-5], col = species_col,
      lower.panel = NULL,
       cex.labels=2, pch=19, cex = 1.2)

# Add a legend
par(xpd = TRUE)
legend(x = 0.05, y = 0.4, cex = 2,
   legend = as.character(levels(species_labels)),
    fill = unique(species_col))
par(xpd = NA)

plot of chunk unnamed-chunk-59

We can see that the Setosa species are distinctly different than Versicolor and Virginica (they have lower petal length and width). But Versicolor and Virginica can not easily be separated based on measurements of their Sepal and Patel width/length.

We can nicely visualize this using a dendrogram with the labels colored with the real species category, and branches colored based on cutting the tree to 3 classes.

data(iris) 
d_iris <- dist(iris[,-5]) # method="man" # is a bit better
hc_iris <- hclust(d_iris)
labels(hc_iris) # no labels, because "iris" has no row names
#> character(0)
dend_iris <- as.dendrogram(hc_iris)
is.integer(labels(dend_iris)) # this could cause problems...
#> [1] TRUE
iris_species <- rev(levels(iris[,5]))
dend_iris <- color_branches(dend_iris,k=3, groupLabels=iris_species)
is.character(labels(dend_iris)) # labels are no longer "integer"
#> [1] TRUE
# have the labels match the real classification of the flowers:
labels_colors(dend_iris) <-
   rainbow_hcl(3)[sort_levels_values(
      as.numeric(iris[,5])[order.dendrogram(dend_iris)]
   )]

# We'll add the flower type
labels(dend_iris) <- paste(as.character(iris[,5])[order.dendrogram(dend_iris)],
                           "(",labels(dend_iris),")", 
                           sep = "")

dend_iris <- hang.dendrogram(dend_iris,hang_height=0.1)

# reduce the size of the labels:
dend_iris <- assign_values_to_leaves_nodePar(dend_iris, 0.5, "lab.cex")

par(mar = c(3,3,3,7))
plot(dend_iris, 
     main = "Clustered Iris dataset
     (the labels give the true flower species)", 
     horiz =  TRUE,  nodePar = list(cex = .007))
legend("topleft", legend = iris_species, fill = rainbow_hcl(3))

plot of chunk unnamed-chunk-60

This simple visualization easily demonstrates how the separation of the Hierarchical clustering is very good with the “setosa” species, but misses in labeling many “versicolor” species as “virginica”.

The hanging of the tree also helps to locate extreme observations. For example, we can see that observation “virginica (107)” is not very similar to the Versicolor species, but still, it is among them. Also, “Versicolor (71)” is too much “within” the Virginica bush, and it is a wonder why that is. Of course, the Iris data set is very well known, and simpler pairs plot often help to locate such problems, yet - dendrogram trees (with all of their limitations) can help gain insights for very high-dimensional data where a simple pairs plot is not possible.

Session info

sessionInfo()
#> R version 3.1.1 (2014-07-10)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> 
#> locale:
#> [1] LC_COLLATE=C                   LC_CTYPE=Hebrew_Israel.1255   
#> [3] LC_MONETARY=Hebrew_Israel.1255 LC_NUMERIC=C                  
#> [5] LC_TIME=Hebrew_Israel.1255    
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#>  [1] profdpm_3.3          plyr_1.8.1           pvclust_1.2-2       
#>  [4] dynamicTreeCut_1.62  gplots_2.14.1        DendSer_1.0.1       
#>  [7] seriation_1.0-13     gclus_1.3.1          cluster_1.15.2      
#> [10] colorspace_1.2-4     dendextendRcpp_0.5.1 Rcpp_0.11.2         
#> [13] knitr_1.6            dendextend_0.17.5   
#> 
#> loaded via a namespace (and not attached):
#>  [1] KernSmooth_2.23-12 MASS_7.3-33        TSP_1.0-9         
#>  [4] bitops_1.0-6       caTools_1.17       codetools_0.2-8   
#>  [7] compiler_3.1.1     digest_0.6.4       evaluate_0.5.5    
#> [10] formatR_0.10       gdata_2.13.3       grid_3.1.1        
#> [13] gtools_3.4.1       magrittr_1.0.1     stringr_0.6.2     
#> [16] tools_3.1.1        whisker_0.3-2