# Lets load the data first
data(USArrests)
# Computer distances and hierarchical clustering
dd <- dist(scale(USArrests), method = "euclidean")
hc <- hclust(dd, method = "ward.D2")
#Lets install the necessary packages
library(factoextra)
## Loading required package: ggplot2
## Welcome! Want to learn more? See two factoextra-related books at https://goo.gl/ve3WBa
fviz_dend(hc, cex = 0.5)
## Warning: The `<scale>` argument of `guides()` cannot be `FALSE`. Use "none" instead as
## of ggplot2 3.3.4.
## ℹ The deprecated feature was likely used in the factoextra package.
## Please report the issue at <https://github.com/kassambara/factoextra/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## FViz
fviz_dend(hc, cex = 0.5,
main = "Dendrogram - ward.D2",
xlab = "Objects", ylab = "Distance", sub = "")
drawing horizontal
fviz_dend(hc, cex = 0.5, horiz = TRUE)
Coloring branches and add rectangle
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
rect = TRUE, # Add rectangle around groups
rect_border = "blue",
rect_fill = TRUE)
# change the theme
fviz_dend(hc, k = 4,
cex = 0.5,
k_colors = c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07"),
color_labels_by_k = TRUE,
ggtheme = theme_gray())
change group color using jco
fviz_dend(hc, cex = 0.5, k = 4, k_colors = "jco")
horizontal with rectangles around
fviz_dend(hc, k = 4, cex = 0.4, horiz = TRUE, k_colors = "jco", rect = TRUE, rect_border = "jco", rect_fill = TRUE)
circular dend
fviz_dend(hc, cex=0.5, k = 4, k_colors = "jco", type = "circular")
require("igraph")
## Loading required package: igraph
##
## Attaching package: 'igraph'
## The following objects are masked from 'package:stats':
##
## decompose, spectrum
## The following object is masked from 'package:base':
##
## union
fviz_dend(hc, k = 4, k_colors = "jco", type = "phylogenic", repel = TRUE)
lets try the layout.gem
require("igraph")
fviz_dend(hc, k = 4, k_colors = "jco", type = "phylogenic", repel = TRUE, phylo_layout = "layout.gem")
## Zooming in
fviz_dend(hc, xlim = c(1,20), ylim = c(1,8))
Lets break the dendrogram at a given height(h) and lets look at the truncated one
dend_plot <- fviz_dend(hc, k = 4, cex = 0.5, k_colors = "jco")
dend_data <- attr(dend_plot, "dendrogram")
dend_cuts <- cut(dend_data, h = 10)
fviz_dend(dend_cuts$upper)
## Warning in min(-diff(our_dend_heights)): no non-missing arguments to min;
## returning Inf
print(dend_plot)
#CLuster dendrogram
fviz_dend(dend_cuts$lower[[1]], main = "Subtree 1")
fviz_dend(dend_cuts$lower[[2]], main = "Subtree 2")
drawing circular in the second format
fviz_dend(dend_cuts$lower[[2]], type = "circular")
##Saving into pdf
pdf("dendrogram.pdf", width = 30, height = 15)
p <- fviz_dend(hc, k = 4, cex = 1, k_colors = "jco")
print(p)
dev.off()
## png
## 2
Creat a new dendrogram
data <- scale(USArrests)
dist.res <- dist(data)
hc <- hclust(dist.res, method = "ward.D2")
dend <- as.dendrogram(hc)
plot(dend)
Creating dendromgram chaining order
library(dendextend)
##
## ---------------------
## Welcome to dendextend version 1.17.1
## Type citation('dendextend') for how to cite the package.
##
## Type browseVignettes(package = 'dendextend') for the package vignette.
## The github page is: https://github.com/talgalili/dendextend/
##
## Suggestions and bug-reports can be submitted at: https://github.com/talgalili/dendextend/issues
## You may ask questions at stackoverflow, use the r and dendextend tags:
## https://stackoverflow.com/questions/tagged/dendextend
##
## To suppress this message use: suppressPackageStartupMessages(library(dendextend))
## ---------------------
##
## Attaching package: 'dendextend'
## The following object is masked from 'package:stats':
##
## cutree
dend <- USArrests[1:5,] %>%
scale %>%
dist %>%
hclust(method = "ward.D2") %>%
as.dendrogram
plot(dend)
Using set as an example
library (dendextend)
# 1. Create a customized dendrogram
mycols <- c("#2E9FDF", "#00AFBB", "#E7B800", "#FC4E07")
dend <- as.dendrogram (hc) %>%
set ("branches_lwd", 1) %>%
set ("branches_k_color", mycols, k = 4) %>%
set("labels_colors", mycols, k = 4) %>%
set ("labels_cex", 0.5)
# 2. Create plot
fviz_dend (dend)