Exploring the branches cut from the tree
The cutree()
function you used in exercises 5 & 6 can also be used to cut a tree at a given height by using the h
parameter. Take a moment to explore the clusters you have generated from the previous exercises based on the heights 20 & 40.
This exercise is part of the course
Cluster Analysis in R
Exercise instructions
- Build the cluster assignment vector
clusters_h20
usingcutree()
with ah = 20
. - Append the cluster assignments as a column
cluster
to thelineup
data frame and save the results to a new data frame calledlineup_h20_complete
. - Repeat the above two steps for a height of 40, generating the variables
clusters_h40
andlineup_h40_complete
. - Use ggplot2 to create a scatter plot, colored by the cluster assignment for both heights.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
dist_players <- dist(lineup, method = 'euclidean')
hc_players <- hclust(dist_players, method = "complete")
# Calculate the assignment vector with a h of 20
clusters_h20 <- ___
# Create a new data frame storing these results
lineup_h20_complete <- mutate(lineup, cluster = ___)
# Calculate the assignment vector with a h of 40
clusters_h40 <- ___
# Create a new data frame storing these results
lineup_h40_complete <- ___
# Plot the positions of the players and color them using their cluster for height = 20
ggplot(___, aes(x = ___, y = ___, color = factor(___))) +
geom_point()
# Plot the positions of the players and color them using their cluster for height = 40