Learn R Programming

factoextra (version 1.0.3)

fviz_dend: Enhanced Visualization of Dendrogram

Description

Enhanced visualization of dendrogram.

Usage

fviz_dend(x, k = NULL, k_colors = NULL, show_labels = TRUE, color_labels_by_k = FALSE, label_cols = NULL, type = c("rectangle", "triangle"), rect = FALSE, rect_border = "gray", rect_lty = 2, rect_lwd = 1.5, cex = 0.8, main = "Cluster Dendrogram", xlab = "", ylab = "Height", ...)

Arguments

x
an object of class dendrogram, hclust, agnes, diana, hcut or hkmeans.
k
the number of groups for cutting the tree.
k_colors
a vector containing colors to be used for the groups. It should contains k number of colors.
show_labels
a logical value. If TRUE, leaf labels are shown. Default value is TRUE.
color_labels_by_k
logical value. If TRUE, labels are colored automatically by group when k != NULL.
label_cols
a vector containing the colors for labels.
type
type of plot. Allowed values are one of "rectangle" or "triangle"
rect
logical value specifying whether to add a rectangle around groups. Used only when k != NULL.
rect_border, rect_lty, rect_lwd
border color, line type and line width for rectangles
cex
size of labels
main, xlab, ylab
main and axis titles
...
other arguments to be passed to the function plot.dendrogram()

Examples

Run this code

# Load and scale the data
data(USArrests)
df <- scale(USArrests)

# Hierarchical clustering
res.hc <- hclust(dist(df))

# Default plot
fviz_dend(res.hc)

# Cut the tree
fviz_dend(res.hc, cex = 0.5, k = 4, color_labels_by_k = TRUE)

# Don't color labels, add rectangles
fviz_dend(res.hc, cex = 0.5, k = 4, 
 color_labels_by_k = FALSE, rect = TRUE)
 
# Triangle
fviz_dend(res.hc, cex = 0.5, k = 4, type = "triangle")

# Change the color of tree using black color for all groups
# Change rectangle border colors
fviz_dend(res.hc, rect = TRUE, k_colors ="black",
rect_border = 2:5, rect_lty = 1)

# Customized color for groups
fviz_dend(res.hc, k = 4, 
 k_colors = c("#1B9E77", "#D95F02", "#7570B3", "#E7298A"))
 
 
 # Color labels using k-means clusters
 km.clust <- kmeans(df, 4)$cluster
 fviz_dend(res.hc, k = 4, 
   k_colors = c("blue", "green3", "red", "black"),
   label_cols =  km.clust[res.hc$order], cex = 0.6)


Run the code above in your browser using DataLab