As described in previous chapters, a dendrogram is a tree-based representation of a data created using hierarchical clustering methods.
In this article, we provide examples of dendrograms visualization using R software. Additionally, we show how to save and to zoom a large dendrogram.
Computing hierarchical clustering
We start by computing hierarchical clustering using the USArrests data sets:
# Load data data(USArrests) # Compute distances and hierarchical clustering dd <- dist(scale(USArrests), method = "euclidean") hc <- hclust(dd, method = "ward.D2")
R packages and functions
To visualize the dendrogram, we’ll use the following R functions and packages:
- fviz_dend()[in factoextra R package] to create easily a ggplot2-based beautiful dendrogram.
- dendextend package to manipulate dendrograms
Before continuing, install the required package as follow:
We’ll use the function fviz_dend()[in factoextra R package] to create easily a beautiful dendrogram using either the R base plot or ggplot2. It provides also an option for drawing circular dendrograms and phylogenic-like trees.
To create a basic dendrograms, type this:
library(factoextra) fviz_dend(hc, cex = 0.5)
You can use the arguments main, sub, xlab, ylab to change plot titles as follow:
fviz_dend(hc, cex = 0.5, main = "Dendrogram - ward.D2", xlab = "Objects", ylab = "Distance", sub = "")
To draw a horizontal dendrogram, type this:
fviz_dend(hc, cex = 0.5, horiz = TRUE)
It’s also possible to cut the tree at a given height for partitioning the data into multiple groups as described in the previous chapter. In this case, it’s possible to color branches by groups and to add rectangle around each group.
To change the plot theme, use the argument ggtheme, which allowed values include ggplot2 official themes [ theme_gray(), theme_bw(), theme_minimal(), theme_classic(), theme_void()] or any other user-defined ggplot2 themes.
fviz_dend(hc, k = 4, # Cut in four groups cex = 0.5, # label size k_colors = c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07"), color_labels_by_k = TRUE, # color labels by groups ggtheme = theme_gray() # Change theme )
Allowed values for k_color include brewer palettes from RColorBrewer Package (e.g. “RdBu”, “Blues”, “Dark2”, “Set2”, …; ) and scientific journal palettes from ggsci R package (e.g.: “npg”, “aaas”, “lancet”, “jco”, “ucscgb”, “uchicago”, “simpsons” and “rickandmorty”).
In the R code below, we’ll change group colors using “jco” (journal of clinical oncology) color palette:
Manipulating dendrograms using dendextend
The package dendextend provide functions for changing easily the appearance of a dendrogram and for comparing dendrograms.
In this section we’ll use the chaining operator (%>%) to simplify our code. The chaining operator turns x %>% f(y) into f(x, y) so you can use it to rewrite multiple operations such that they can be read from left-to-right, top-to-bottom. For instance, the results of the two R codes below are equivalent.
- Standard R code for creating a dendrogram:
data <- scale(USArrests) dist.res <- dist(data) hc <- hclust(dist.res, method = "ward.D2") dend <- as.dendrogram(hc) plot(dend)
- R code for creating a dendrogram using chaining operator:
library(dendextend) dend <- USArrests[1:5,] %>% # data scale %>% # Scale the data dist %>% # calculate a distance matrix, hclust(method = "ward.D2") %>% # Hierarchical clustering as.dendrogram # Turn the object into a dendrogram. plot(dend)
- Functions to customize dendrograms: The function set() [in dendextend package] can be used to change the parameters of a dendrogram. The format is:
set(object, what, value)
- object: a dendrogram object
- what: a character indicating what is the property of the tree that should be set/updated
- value: a vector with the value to set in the tree (the type of the value depends on the “what”).
Possible values for the argument what include:
|Value for the argument what||Description|
|labels||set the labels|
|labels_colors and labels_cex||Set the color and the size of labels, respectively|
|leaves_pch, leaves_cex and leaves_col||set the point type, size and color for leaves, respectively|
|nodes_pch, nodes_cex and nodes_col||set the point type, size and color for nodes, respectively|
|hang_leaves||hang the leaves|
|branches_k_color||color the branches|
|branches_col, branches_lwd , branches_lty||Set the color, the line width and the line type of branches, respectively|
|by_labels_branches_col, by_labels_branches_lwd and by_labels_branches_lty||Set the color, the line width and the line type of branches with specific labels, respectively|
|clear_branches and clear_leaves||Clear branches and leaves, respectively|
library(dendextend) # 1. Create a customized dendrogram mycols <- c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07") dend <- as.dendrogram(hc) %>% set("branches_lwd", 1) %>% # Branches line width set("branches_k_color", mycols, k = 4) %>% # Color branches by groups set("labels_colors", mycols, k = 4) %>% # Color labels by groups set("labels_cex", 0.5) # Change label size # 2. Create plot fviz_dend(dend)
We described functions and packages for visualizing and customizing dendrograms including:
- fviz_dend() [in factoextra R package], which provides convenient solutions for plotting easily a beautiful dendrogram. It can be used to create rectangular and circular dendrograms, as well as, a phylogenic tree.
- and the dendextend package, which provides a flexible methods to customize dendrograms.
Additionally, we described how to plot a subset of large dendrograms.