Clusters based on height
In previous exercises, you have grouped your observations into clusters using a pre-defined number of clusters (k). In this exercise, you will leverage the visual representation of the dendrogram in order to group your observations into clusters using a maximum height (h), below which clusters form.
You will work the color_branches()
function from the dendextend
library in order to visually inspect the clusters that form at any height along the dendrogram.
The hc_players has been carried over from your previous work with the soccer line-up data.
This exercise is part of the course
Cluster Analysis in R
Exercise instructions
- Create a dendrogram object
dend_players
from yourhclust
result using the functionas.dendrogram()
. - Plot the dendrogram.
- Using the
color_branches()
function create & plot a new dendrogram with clusters colored by a cut height of 20. - Repeat the above step with a height of 40.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
library(dendextend)
dist_players <- dist(lineup, method = 'euclidean')
hc_players <- hclust(dist_players, method = "complete")
# Create a dendrogram object from the hclust variable
dend_players <- as.dendrogram(___)
# Plot the dendrogram
# Color branches by cluster formed from the cut at a height of 20 & plot
dend_20 <- color_branches(___, h = ___)
# Plot the dendrogram with clusters colored below height 20
# Color branches by cluster formed from the cut at a height of 40 & plot
dend_40 <- ___
# Plot the dendrogram with clusters colored below height 40