Session Ready
Exercise

Dendrogram aesthetics

So you made a dendrogram…but it's not as eye-catching as you had hoped!

The dendextend package can help your audience by coloring branches and outlining clusters. dendextend is designed to operate on dendrogram objects, so you'll have to change the hierarchical cluster from hclust using as.dendrogram().

A good way to review the terms in your dendrogram is with the labels() function. It will print all terms of the dendrogram. To highlight specific branches, use branches_attr_by_labels(). First, pass in the dendrogram object, then a vector of terms as in c("data", "camp"). Lastly, add a color such as "blue".

After you make your plot, you can call out clusters with rect.dendrogram(). This adds rectangles for each cluster. The first argument to rect.dendrogram() is the dendrogram, followed by the number of clusters (k). You can also pass a border argument specifying what color you want the rectangles to be (e.g. "green").

Instructions
100 XP

The dendextend package has been loaded for you, and a hierarchical cluster object, hc, was created from tweets_dist.

  • Create hcd as a dendrogram using as.dendrogram() on hc.
  • Print the labels of hcd to the console.
  • Use branches_attr_by_labels() to color the branches. Pass it three arguments: the hcd object, c("marvin", "gaye"), and the color "red". Assign to hcd_colored.
  • plot() the dendrogram hcd_colored with the title "Better Dendrogram", added using the main argument.
  • Add rectangles to the plot using rect.dendrogram(). Specify k = 2 clusters and a border color of "grey50".