Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a Scree Plot for Hierarchical Cluster in R?

Tags:

r

I have generated a dendrogram using plot() function and used hclust() for hierarchical clustering. I am looking to generate a scree plot for the same. Any suggestions?

like image 427
Dhruv Mehta Avatar asked Oct 23 '25 17:10

Dhruv Mehta


1 Answers

It's a little late, but I have an answer.

# creating a dissimilarity matrix
res.dist <- dist(USArrests, method = "euclidean")

# creating an object of class "hclust"
res.hc <- hclust(d = res.dist, method = "ward.D2")

As can be found in the documentation to hclust, it is a list of values. You can inspect them by using

View(res.hc)

Now, the variable height has exactly what is needed for a scree plot. The following code generates a scree plot:

> ggplot(res.hc$height %>%
+            as.tibble() %>%
+            add_column(groups = length(res.hc$height):1) %>%
+            rename(height=value),
+        aes(x=groups, y=height)) +
+     geom_point() +
+     geom_line()

Basically, what you do is plot the height for a number of groups. (It might not be very elegant, I'd be delighted to hear shorter versions to generate the same outcome).

My outcome is:

enter image description here

like image 187
Lukas Avatar answered Oct 26 '25 06:10

Lukas