I am trying to plot the results of a hierarchical clustering in R
as a dendrogram, with rectangles identifying clusters.
The following code does the trick for a vertical dendrogram, but for a horizontal dendrogram, (horiz=TRUE
), the rectangles are not drawn. Is there any way to do the same for horizontal dendrograms too.
library("cluster")
dst <- daisy(iris, metric = c("gower"), stand = FALSE)
hca <- hclust(dst, method = "average")
plot(as.dendrogram(hca), horiz = FALSE)
rect.hclust(hca, k = 3, border = "red")
Moreover I would like to plot a line to cut the tree at a desired distance value. How to plot that in R. The cutree
function returns the clusters, but is it possible to plot it as well.
cutree(hca, k = 3)
The desired output that I am looking for is like this.
How to get this done in R?
Both jlhoward and Backlin answers are good.
What you could also try is using the
dendextend
package, designed exactly for this sort of thing. It has arect.dendrogram
function which works likerect.hclust
, but with a horiz parameter (plus some more control over the location of the edge of the rect). For finding the relevant height you can use theheights_per_k.dendrogram
function (which is much faster when also using thedendextendRcpp
package)Here is a simple example for how you would get the same result as in the above examples (with an added bonus of colored branches, just for fun):
Here's a solution using
ggplot
and theggdendro
package. As an added bonus, we can color the labels by cluster...To just get the job done (although in a quite ugly way) you could just manually swap the coordinates in the call to
rect
inrect.hclust
:and call
rhc
like you calledrect.hclust
: