3

In the following example:

hc <- hclust(dist(mtcars))
hcd <- as.dendrogram((hc))
hcut4 <- cutree(hc,h=200)
class(hcut4)
plot(hcd,ylim=c(190,450))

I'd like to add the labels of the classes. I can do:

hcd4 <- cut(hcd,h=200)$upper
plot(hcd4)

Besides the fact labels are oddly shifted, does the numbering of the branches from cut() always correspond to the classes in hcut4? In this case, they do:

hcd4cut <- cutree(hcd4, h=200)
hcd4cut

But is this the general case?

The example using dendextend (Label and color leaf dendrogram in r) is nice

library(dendextend)
colorCodes <- c("red","green","blue","cyan")
labels_colors(hcd) <- colorCodes[hcut4][order.dendrogram(hcd)]
plot(hcd)

Unfortunately, I always have many individuals, so plotting individuals is rarely a useful option for me. I can do:

hcd <- as.dendrogram((hc))
hcd4 <- cut(hcd,h=200)$upper

and I can add colors

hcd4cut <- cutree(hcd4, h=200)
labels_colors(hcd4) <- colorCodes[hcd4cut][order.dendrogram(hcd4)]
plot(hcd4)

but the following does not work:

plot(hcd4,labels=hcd4cut)

Is there a better way to plot the cut dendrogram labelling branches according to the classes (consistent with the result of cutree())?

This is an example of what I would need (class labels edited on the picture), but note that the problem is that I do not know if the labels are actually at the right branch: enter image description here

Community
  • 1
  • 1
user2955884
  • 405
  • 2
  • 11
  • Can you add a figure (edit it using paint) to illustrate what output you are interested in? – Tal Galili Feb 25 '17 at 16:49
  • Done. Note that the problem is that I do not know if the labels are actually at the right branch: – user2955884 Feb 25 '17 at 17:38
  • mmm, why don't you just use the `prune` function from dendextend? – Tal Galili Feb 25 '17 at 21:30
  • Because I want to plot the labels of the classes created by hcut(). Actually, the correct ordering that I should have added to the figure is 4 3 2 1. I know it thanks to the colors in `colorCodes <- c("red","green","blue","cyan"); labels_colors(hcd) <- colorCodes[hcut4][order.dendrogram(hcd)]; plot(hcd)` – user2955884 Feb 26 '17 at 09:42
  • 2
    But obviously this cannot be the general solution: it does not make sense plotting the color-code whole tree to be able to identify the classes of the branches. Also, I do not understand why another package is needed for such a simple operation: if we cut a tree with hcut(), it is obvious that, in many cases, users will want to visualize the cut tree with the branches labelled by their respective class. In other words, something like plot(hc, h=200) – user2955884 Feb 26 '17 at 09:50

0 Answers0