title: How to color a dendrogram's labels according to defined groups? (in R)
body: <p>I have a numeric matrix in R with 24 rows and 10,000 columns. The row names of this matrix are basically file names from which I have read the data corresponding to each of the 24 rows. Apart from this I have a separate factor list with 24 entires, specifying the group to which the 24 files belong. There are 3 groups - Alcohols, Hydrocarbon and Ester. The names and the corresponding group to which they belong look like this:</p>
  
  <pre><code>&gt; MS.mz
  [1] "int-354.19" "int-361.35" "int-368.35" "int-396.38" "int-408.41" "int-410.43" "int-422.43"
  [8] "int-424.42" "int-436.44" "int-438.46" "int-452.00" "int-480.48" "int-648.64" "int-312.14"
  [15] "int-676.68" "int-690.62" "int-704.75" "int-312.29" "int-326.09" "int-326.18" "int-326.31"
  [22] "int-340.21" "int-340.32" "int-352.35"
  
  &gt; MS.groups
  [1] Alcohol     Alcohol     Alcohol     Alcohol     Hydrocarbon Alcohol     Hydrocarbon Alcohol    
  [9] Hydrocarbon Alcohol     Alcohol     Alcohol     Ester       Alcohol     Ester       Ester      
  [17] Ester       Alcohol     Alcohol     Alcohol     Alcohol     Alcohol     Alcohol     Hydrocarbon
  Levels: Alcohol Ester Hydrocarbon
  </code></pre>
  
  <p>I wanted to generate a dendrogram to look how the data in the matrix can be clustered. So, I used the following commands:</p>
  
  <pre><code>require(vegan)
  dist.mat&lt;-vegdist(MS.data.scaled.transposed,method="euclidean")
  clust.res&lt;-hclust(dist.mat)
  plot(clust.res)
  </code></pre>
  
  <p>and I got a dendogram. Now I want to color the file names in the dendrogram according to the group they belong to i.e Alcohol, Hydrocarbon or Ester. I looked at different examples posted on the forum like</p>
  
  <p><a href="https://stackoverflow.com/questions/18802519/label-and-color-leaf-dendrogram-in-r">Label and color leaf dendrogram in r</a></p>
  
  <p><a href="https://stackoverflow.com/questions/18855631/label-and-color-leaf-dendrogram-in-r-using-ape-package">Label and color leaf dendrogram in R using ape package</a></p>
  
  <p><a href="http://rstudio-pubs-static.s3.amazonaws.com/5381_bc2905c1c34142b88251612a2fa625f0.html" rel="nofollow noreferrer">Clustering with bootstrapping</a></p>
  
  <p>, but could not implement it for my data.  I am not sure how to correlate row.names with the MS.groups to get the colored names in the dendrogram. </p>
  
  <p>On generating the tree using dendextend (as explained in <a href="https://nycdatascience.com/wp-content/uploads/2013/09/dendextend-tutorial.pdf" rel="nofollow noreferrer">https://nycdatascience.com/wp-content/uploads/2013/09/dendextend-tutorial.pdf</a>), I get the following tree</p>
  
  <p><img src="https://i.stack.imgur.com/kVWSl.png" alt="enter image description here"></p>
  
  <p>Here is the code used to generate it:</p>
  
  <pre><code>require(colorspace)
  d_SIMS &lt;- dist(firstpointsample5[,-1])
  hc_SIMS &lt;- hclust(d_SIMS)
  labels(hc_SIMS)
  dend_SIMS &lt;- as.dendrogram(hc_SIMS)
  SIMS_groups &lt;- rev(levels(firstpointsample5[, 1]))
  dend_SIMS &lt;- color_branches(dend_SIMS, k = 3, groupLabels = SIMS_groups)
  is.character(labels(dend_SIMS)) 
  plot(dend_SIMS)
  labels_colors(dend_SIMS) &lt;- rainbow_hcl(3)[sort_levels_values(as.numeric(firstpointsample5[,1])[order.dendrogram(dend_SIMS)])]
  labels(dend_SIMS) &lt;- paste(as.character(firstpointsample5[, 1])[order.dendrogram(dend_SIMS)],"(", labels(dend_SIMS), ")", sep = "")
  dend_SIMS &lt;- hang.dendrogram(dend_SIMS, hang_height = 0.1)
  dend_SIMS &lt;- assign_values_to_leaves_nodePar(dend_SIMS, 0.5,"lab.cex")
  par(mar = c(3, 3, 3, 7))
  plot(dend_SIMS, main = "Clustered SIMS dataset\n (the labels give the true m/z groups)",horiz = TRUE, nodePar = list(cex = 0.007))
  legend("topleft", legend = SIMS_groups, fill = rainbow_hcl(3))
  </code></pre>