1

I am trying to color code only the leaf node labels in a dendogram. I found this stackoverflow post which is the closest to what I am trying to do

Label and color leaf dendrogram

Tal Galili's answer is useful, but what would I need to do if I DIDN'T want the group labels to be attached to the leaf node labels, and just have the leaf node labels colored according to their group membership?

So using Galili's code,

sample = data.frame(matrix(floor(abs(rnorm(20000)*100)),ncol=200))
groupCodes <- c(rep("Cont",25), rep("Tre1",25), rep("Tre2",25), 
rep("Tre3",25))

Instead of making the rownames unique based on the group label as such

rownames(sample) <- make.unique(groupCodes)

What if they were already all unique, e.g.

set.seed(123)
groupLabel <- rnorm(100,0,1)
rownames(sample) <- groupLabel

How would I keep the unique rownames as my node labels, and color them according to group membership (in this it would be the "groupCodes" in Galili's code)?

Tal Galili
  • 24,605
  • 44
  • 129
  • 187
Antsushi
  • 167
  • 10
  • what is the group variable that you use to color? like this? `labels_colors(dend) <- colorCodes[cutree(hc, k = 4)][order.dendrogram(dend)]; plot(dend)` – rawr Mar 26 '18 at 19:28
  • So in this example it would be the `groupCodes`, where the first 25 variables belong to group `Cont`, the next 25 belong to `Tre1`, and so one. In your example the color coding is done based on branch membership, not prior group membership. So with your code variables number 26,3,38,7,56 are all blue, whereas I want them to be colored based on their group ("Cont", "Tre1", etc.). So 3 and 7 would have their own color (group `Cont`), 26 and 38 would have theirs (group `Tre1`), and 56 would have a unique color as well (group `Tre2`). Hopefully it makes sense. – Antsushi Mar 26 '18 at 19:48
  • that's what I thought, but I don't see how the original code wouldn't work--the coloring doesn't depend on whether the labels are unique or not – rawr Mar 26 '18 at 20:01
  • I think the original code depends on labels having a group prefix (e.g. Cont.1, Tre1.12, etc) for it to work, and the unique labels are generated from the group labels. I want to get rid of the group prefix in my labels, but still have them colored based on group membership. So in Galili's dendrogram I would expect to see the first leaf node labels as "9", "11", "16", "15", instead of "Tre3.9", "Tre1.11" etc. – Antsushi Mar 26 '18 at 20:15
  • @Antsushi all you need is to skip the part: `rownames(sample) <- make.unique(groupCodes)` from the linked post and I believe you will achieve the desired result . – missuse Mar 26 '18 at 22:03
  • @missuse, I actually found exactly what I needed in [this page](https://cran.r-project.org/web/packages/dendextend/vignettes/FAQ.html#how-to-color-a-dendrograms-labels-according-to-defined-groups-in-r). A bit embarrassing that I didn't search enough before asking this question. – Antsushi Mar 26 '18 at 22:47
  • @Antsushi I'm glad you found that solution. I voted to close it for duplicate and link to the original question here on SO: https://stackoverflow.com/questions/31117849/how-to-color-a-dendrograms-labels-according-to-defined-groups-in-r – Tal Galili Mar 27 '18 at 07:52
  • 1
    Possible duplicate of [How to color a dendrogram's labels according to defined groups? (in R)](https://stackoverflow.com/questions/31117849/how-to-color-a-dendrograms-labels-according-to-defined-groups-in-r) – Tal Galili Mar 27 '18 at 07:53

0 Answers0