6

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.

library('ape')
hc <- hclust(dist(USArrests), "ave")
plot(as.phylo(hc), type="fan")

Adding colour to the tips (labels) based on a set of groups is no problem with the tip.color option combined with cutree command.

hc.cuts <- cutree(hc, k=5)
plot(as.phylo(hc), type="fan", tip.color=rainbow(5)[hc.cuts])

The edge.color option defines the colors of the edges, but not in a logincal manner when many colours are desired.

plot(as.phylo(hc), type="fan", tip.color=rainbow(5)[hc.cuts], edge.color=rainbow(5)[hc.cuts])

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 & 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.

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.

2
  • 1
    For the off-chance that you won't get a satisfying answer, I'd like to let you know that there are also the dendextend package and the ggdendro package, maybe one of the two can help you out. They're both quite new, too.
    – maj
    Sep 7, 2015 at 16:33
  • 1
    Hi Maj, the ggdendro by Andrie was the first package to help have a dendrogram plot in ggplot2. It was limited in that it didn't take into account various graphical parameters of the branches/nodes/leaves of the tree. The dendextend has a function called "as.ggdend" (which is actually a fork of ggdendro), which does take into account further graphical parameters of the tree. You can see more about it here: cran.r-project.org/web/packages/dendextend/vignettes/…
    – Tal Galili
    Sep 7, 2015 at 23:11

1 Answer 1

6

As @maj suggested in a comment, dendextend can help you out if you don't mind using that package. It is very flexible and has extensive documentation and vignettes.

Here's an example minimally adapted from the dendextend FAQ.

# install.packages("dendextend")
# install.packages("circlize")

library(dendextend)
library(circlize)

hc <- hclust(dist(USArrests))
dend <- as.dendrogram(hc)

num_clades <- 5

dend <- dend %>% 
  color_branches(k=num_clades, col=rainbow) %>% 
  color_labels(k=num_clades, col=rainbow)

par(mar = rep(0, 4))
circlize_dendrogram(dend, dend_track_height = 0.8) 

It outputs

enter image description here

1
  • 1
    Exactly what I'm looking for, thanks. I didn't know about the 'dendextend' package. Lovely! Sep 8, 2015 at 7:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.