title: Dendrogram edge (branch) colors to match tip (leaf) colors (ape package)
body: <p>I'm trying to add color to the edges (lines) of a phylogeny-type plot in R using the plot.phylo command from the ape package. This example is for a "fan" type plot, though I expect the approach would be the same with a "phylogram type" or whatever.</p>
  
  <pre><code>library('ape')
  hc &lt;- hclust(dist(USArrests), "ave")
  plot(as.phylo(hc), type="fan")
  </code></pre>
  
  <p>Adding colour to the tips (labels) based on a set of groups is no problem with the tip.color option combined with cutree command.</p>
  
  <pre><code>hc.cuts &lt;- cutree(hc, k=5)
  plot(as.phylo(hc), type="fan", tip.color=rainbow(5)[hc.cuts])
  </code></pre>
  
  <p>The edge.color option defines the colors of the edges, but not in a logincal manner when many colours are desired.</p>
  
  <pre><code>plot(as.phylo(hc), type="fan", tip.color=rainbow(5)[hc.cuts], edge.color=rainbow(5)[hc.cuts])
  </code></pre>
  
  <p>However, I would like the edges to match the terminal tip colours once that branch of the dendrogram is destined for a given group. In the given example, towards the red &amp; blue groups, the first levels of edges would stay black (as it's headed for two groups: red and blue), but edges beyond this would be colored the same as the eventual tip color.</p>
  
  <p>I suspect the key lies in figuring out the ordering of the $edge values in the as.phylo object, but I can't figure it myself. Thanks.</p>