I want to compare two phylogenies and colour the association lines based on some metadata I have. I have been using ape cophyloplot but I have not had any success in getting the lines to colour accurately according to my data (see previous question).

Note that in my actual work flow I define the colour scheme using a palette to control the colour outcome.

I want a means to make a tangle using phylogenies which I can format. Preferably in R. I like to get an output like this:


4 Answers 4


I think you can try dendextend, in this manual there is an example of coloring the branches. I don't think it is exactly like your coloring, but with a little tweaking you might get your colorscheme in there.

The manual mentions an argument called color_lines for the function tanglegram():

# The `which` parameter allows us to pick the elements in the list to compare
iris_dendlist %>% dendlist(which = c(1,4)) %>% ladderize %>% 
   # untangle(method = "step1side", k_seq = 3:20) %>%
   set("rank_branches") %>%
   tanglegram(common_subtrees_color_branches = TRUE)

According to the manual, the code above produces an image like this:

example of a colored tanglegram

Try to see if you can fit your color palette in there.


To get your colors in the original order, you can make a dataframe with the labels in the right order with your metadata. You can use merge for this.

So from your example code it would be something like this:

site <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L,9L, 10L, 2L), .Label = c("t1", "t10", "t2", "t3", "t4", "t5","t6", "t7", "t8", "t9"), class = "factor"), site = c(1L, 1L,1L, 2L, 2L, 3L, 1L, 3L, 2L, 2L)), .Names = c("name", "site"), row.names = c(NA,10L), class = "data.frame") 


t1 <- rcoal(10)
t2 <- rcoal(10)

# The name are factors now, so make chr first
site$name <- as.character(site$name)

# make a data.frame of your labels
labels_df <- data.frame(t1$tip.label)

#merge the 2 data.frames in the right order (hence sort=F)
colors <- merge(labels_df,site,by.x="t1.tip.label", by.y="name",all.x=T, all.y=F,sort=F)

tanglegram(t1, t2, color_lines=colors$site)
  • $\begingroup$ Thanks b.nota. this is something akin to a workflow I had before its good but still inaccurate to get tip labels in correct order. See my answer from that thread. I did try this just in case and it still doesnt fix my issue. Thanks though. $\endgroup$
    – AudileF
    Commented Jun 28, 2017 at 13:00

Thanks everyone for the suggestions @b.nota answer is useful to colour according to clade groups but does not address my actual question on using metadata to colour the lines. For this answer see below:

dendextend states for color_lines a vector of colors for the lines connected the labels. If the colors are shorter than the number of labels, they are recycled (and a warning is issued). The colors in the vector are applied on the lines from the bottom up.

# Extract labels from dendrogram on the left
labels <- dendA %>% set("labels_to_char") %>% labels 

#Using a metadata table with colours create a vector of colours
labels <- as.data.frame(labels)
labels2 <- merge(labels, metadata, by.x="labels", by.y="Sample.name", sort=F)
cols <- as.character(labels2$Colours) 

# Make tanglegram
tanglegram(dendA, dendC, color_lines = cols)

Side note if anyone with a phylogeny needs to convert it to a dendrogram to try this on their data see my other question.

  • $\begingroup$ Hi AudileF, the vector of colors for color_lines takes the order of the labels in the tree. If you want to have it follow the labels in another order, you'll need to put the colors in the initial dataframe with your metadata first, and direct color_lines to the right column of the data frame. It is hard to explain without any example. $\endgroup$
    – benn
    Commented Jun 28, 2017 at 11:55
  • $\begingroup$ Hi b.nota I have a workable example in my previous thread linked above, if that is of any use? Just swap rtree for rcoal, like you mentioned earlier. $\endgroup$
    – AudileF
    Commented Jun 28, 2017 at 12:08

As an alternative (though I realise this doesn't really answer the OPs question directly), Dendroscope and Treemap can also produce these types of plots. I don't know if they'll do coloured connections off the top of my head, but I think they can differentially colour the tree branches.

Just in case that is of use!


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.