시작하기무료로 시작하기

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").

이 연습은 강의의 일부입니다

Text Mining with Bag-of-Words in R

강의 보기

연습 안내

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".

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Create hcd
___ <- ___(___)

# Print the labels in hcd
___(___)

# Change the branch color to red for "marvin" and "gaye"
___ <- ___(___, ___, ___)

# Plot hcd_colored
___(___, ___)

# Add cluster rectangles
___(___, ___, ___)
코드 편집 및 실행