Visualizing clustering dendrogram in R | Hierarchical clustering

See Video ⮞ ☝


Introduction

A dendrogram displays the nested groups produced by hierarchical clustering. This tutorial uses the built-in USArrests data to standardize variables, calculate distances, fit a hierarchy, and improve the plot with factoextra.

  • Distance matrix computation
  • Hierarchical clustering
  • Customizing dendrogram
Let’s start using R studio.

Import data

Load USArrests and inspect the variable definitions, units, and data structure. The data contain measurements on different scales, so any distance calculation should account for scale if equal weighting of variables is intended.

head(USArrests)
#            Murder Assault UrbanPop Rape
# Alabama      13.2     236       58 21.2
# Alaska       10.0     263       48 44.5
# Arizona       8.1     294       80 31.0
# Arkansas      8.8     190       50 19.5
# California    9.0     276       91 40.6
# Colorado      7.9     204       78 38.7
str(USArrests)
# 'data.frame': 50 obs. of  4 variables:
#  $ Murder  : num  13.2 10 8.1 8.8 9 7.9 3.3 5.9 15.4 17.4 ...
#  $ Assault : int  236 263 294 190 276 204 110 238 335 211 ...
#  $ UrbanPop: int  58 48 80 50 91 78 77 72 80 60 ...
#  $ Rape    : num  21.2 44.5 31 19.5 40.6 38.7 11.1 15.8 31.9 25.8 ...

Scale data set

The scale() function centers each column and, by default, scales it to unit standard deviation. Standardization is often useful when variables use different units, but it changes the distance metric and should follow the analysis objective.

data.scaled <- scale(x = USArrests,
                     center = TRUE,
                     scale = TRUE)
head(data.scaled)
#                Murder   Assault   UrbanPop         Rape
# Alabama    1.24256408 0.7828393 -0.5209066 -0.003416473
# Alaska     0.50786248 1.1068225 -1.2117642  2.484202941
# Arizona    0.07163341 1.4788032  0.9989801  1.042878388
# Arkansas   0.23234938 0.2308680 -1.0735927 -0.184916602
# California 0.27826823 1.2628144  1.7589234  2.067820292
# Colorado   0.02571456 0.3988593  0.8608085  1.864967207

Distance matrix computation

The dist() function calculates pairwise dissimilarities between rows. Choose a distance measure that is appropriate for the variable types and scientific question; Euclidean distance after standardization is one common choice for continuous measurements.

Supply the numeric data matrix and selected distance method to dist(). Check the input columns and preprocessing, because the resulting hierarchy depends directly on those choices.

"euclidean", "maximum", "manhattan", "canberra", "binary" or "minkowski"

Convert the compact dist object to a full matrix with as.matrix() only when needed for inspection or another function. The matrix displays pairwise distances; it is not an additional clustering result.

Missing values are allowed, and are excluded from all computations involving the rows within which they occur. 
require(stats)
# Distance matrix computation
res.dist = dist(x = data.scaled,
                method = "euclidean")
# Print distance matrix 
output = as.matrix(res.dist)[1:6, 1:6]
round(output, digits = 3)
#            Alabama Alaska Arizona Arkansas California Colorado
# Alabama      0.000  2.704   2.294    1.290      3.263    2.651
# Alaska       2.704  0.000   2.701    2.826      3.013    2.327
# Arizona      2.294  2.701   0.000    2.718      1.310    1.365
# Arkansas     1.290  2.826   2.718    0.000      3.764    2.831
# California   3.263  3.013   1.310    3.764      0.000    1.288
# Colorado     2.651  2.327   1.365    2.831      1.288    0.000

as.matrix() and as.dist() convert between compact distance objects and square matrices. Retain the compact form for clustering functions that accept a dist object.

d = as.dist(output)

Hierarchical clustering

Hierarchical clustering repeatedly joins observations or clusters according to a chosen dissimilarity and linkage rule. The resulting tree depends on both the distance measure and linkage method.

Pass the distance object to hclust() and select a linkage method such as complete, average, or Ward’s method. These methods define how distances between clusters are updated and can yield different dendrograms.

"ward.D", "ward.D2", "single", "complete", "average" (UPGMA), "mcquitty" (WPGMA), "median" (WPGMC) or "centroid" (UPGMC).
# Cluster dendrogram using stats package
require(stats)
res.hc <- hclust(d = res.dist,
                 method = "complete")
plot(x = res.hc) 

The factoextra package provides fviz_dend() for enhanced dendrogram displays. Use the fitted clustering object and choose a layout that makes group structure and labels clear.

Supply the clustering object to fviz_dend(). Adjust label size and branch width only after confirming that the cluster membership and tree heights are correct.

Branch and cluster colors can improve readability. Use a restrained, color-vision-aware palette and include a clear legend when colors encode cluster membership.

# Cluster dendrogram using factoextra package
require(factoextra)
fviz_dend(x = res.hc, cex = 0.7, lwd = 0.7) 

R provides named colors and palette utilities, but consistent palettes from a plotting package can make figures easier to interpret. Avoid assigning colors that imply an ordered scale when clusters are categorical.

Customize dendrogram

Color choices

A palette from ggsci can be used for a journal-inspired appearance. Check the available palette name and number of colors against the installed package version, and ensure the palette remains legible in print.

require(grDevices)
colors()

The graphics palettes can be set or viewed by using palette() function. In R it is almost always better to specify colors by name. The quick way to show colors in a plot is by using show_col() function.

require(scales)
palette()
show_col(palette(rainbow(6)))

To view the color palettes inspired by plots in journal of clinical oncology use pal_jco() function. The argument palette specify the palette type. Currently there is one available option default (10-color palette). The alpha argument specify transparency level. The value for this argument can be between \(0\) and \(1\).

require("ggsci")
show_col(pal_jco(palette = c("default"))(10))
show_col(pal_jco("default", alpha = 0.6)(10))

Comments

  1. Hi, Thanks for the nice tutorial, can we follow same procedure for categorical variables

    ReplyDelete
    Replies
    1. Thanks for your feedback. Yes you can the same procedure

      Delete
    2. hello sir ,i have a problem in creating Cluster dendrogram,it showing Error in colMeans(x, na.rm = TRUE) : 'x' must be numeric

      Delete
    3. Please share the code that you have used and also the output of str(data) where data is object containing data frame that you have imported in R.

      Delete
  2. Thanks for the robust tutorial. If I want to reduce (shorten) the height, how can I do?

    ReplyDelete
    Replies
    1. If you just need to reduce the height and not the amount then you can simply do this by changing the dimensions of image while saving it.

      In R-studio IDE, go to plots window click on "Export" and choose "save as PDF" then in device size option try different width and heights (in inches) to get the desired one.

      Delete
  3. Thank you for sharing such a useful article. I had a great time. This article was fantastic to read. Continue to publish more articles on

    Data Engineering Services

    ReplyDelete
  4. I truly appreciate the time and work you put into sharing your knowledge. I found this topic to be quite effective and beneficial to me. Thank you very much for sharing. Continue to blog.

    Data Engineering Services 

    AI & ML Solutions

    Data Analytics Services

    Data Modernization Services

    ReplyDelete
  5. Can I change the order of clusters in rectangle denrogram somehow? (I just want to swap order of two brunches coming from one common.)

    ReplyDelete
  6. This comment has been removed by a blog administrator.

    ReplyDelete
  7. I am really very happy to visit your blog. Directly I am found which I truly need. please visit our website for more information
    Data visualization Service in USA

    ReplyDelete
  8. I am really very happy to visit your blog. Directly I am found which I truly need. please visit our website for more information
    Data visualization Service in USA

    ReplyDelete
  9. I am really very happy to visit your blog. Directly I am found which I truly need. please visit our website for more information
    Data visualization Service in USA

    ReplyDelete
  10. Hi There,
    Thank you for sharing the knowledgeable blog with us I hope that you will post many more blog with us:-
    One-Touch Ultra Diabetic Test Strips Mail Order Box of 50 - 2 boxes The One Touch Ultra Diabetic Test Strip is the #1 Test Strip in the United States.
    Email:info@babyformulanutrients.com
    Click here for more information:-more info

    ReplyDelete

Post a Comment