<p>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:</p>

<p>dendextend states for <code>color_lines</code>
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.</p>

<pre><code># Extract labels from dendrogram on the left
labels &lt;- dendA %&gt;% set("labels_to_char") %&gt;% labels 

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

# Make tanglegram
tanglegram(dendA, dendC, color_lines = cols)
</code></pre>

<p>Side note if anyone with a phylogeny needs to convert it to a dendrogram to try this on their data see <a href="https://bioinformatics.stackexchange.com/questions/923/how-to-convert-a-phylogeny-to-a-dendrogram-in-r/930?noredirect=1#comment1714_930">my other question</a>. </p>

CC BY-SA 3.0
2017-06-28T11:55:16.963Z
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.
benn
CC BY-SA 3.0
2017-06-28T12:08:27.173Z
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.
AudileF
CC BY-SA 3.0
2017-06-28T11:19:21.197Z
2017-06-29T15:48:38.730Z
2017-06-29T15:48:38.730Z
AudileF
<p>I think you can try dendextend, in this <a href="https://cran.r-project.org/web/packages/dendextend/vignettes/Cluster_Analysis.html" rel="noreferrer">manual</a> 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.</p>

<p>The manual mentions an argument called <code>color_lines</code> for the function  <code>tanglegram()</code>:</p>

<pre class="lang-r prettyprint-override"><code># The `which` parameter allows us to pick the elements in the list to compare
iris_dendlist %&gt;% dendlist(which = c(1,4)) %&gt;% ladderize %&gt;% 
   # untangle(method = "step1side", k_seq = 3:20) %&gt;%
   set("rank_branches") %&gt;%
   tanglegram(common_subtrees_color_branches = TRUE)
</code></pre>

<p>According to the manual, the code above produces an image like this:</p>

<p><a href="https://i.sstatic.net/v8kWS.png" rel="noreferrer"><img src="https://i.sstatic.net/v8kWS.png" alt="example of a colored tanglegram"></a></p>

<p>Try to see if you can fit your color palette in there.</p>

CC BY-SA 3.0
2017-06-27T12:30:55.550Z
2017-06-27T13:35:46.243Z
2017-06-27T13:35:46.243Z
benn
<p>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 <code>merge</code> for this.</p>

<p>So from your example code it would be something like this:</p>

<pre><code>site &lt;- 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") 

library(ape)
library(dendextend)

t1 &lt;- rcoal(10)
t2 &lt;- rcoal(10)

str(site)
# The name are factors now, so make chr first
site$name &lt;- as.character(site$name)

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

#merge the 2 data.frames in the right order (hence sort=F)
colors &lt;- 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)
</code></pre>

CC BY-SA 3.0
2017-06-28T13:00:43.117Z
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.
AudileF
CC BY-SA 3.0
2017-06-28T12:40:25.230Z
2017-06-28T12:40:25.230Z
benn
<p>As an alternative (though I realise this doesn't really answer the OPs question directly), <a href="http://dendroscope.org/" rel="nofollow noreferrer">Dendroscope</a> and <a href="https://sites.google.com/site/cophylogeny/treemap" rel="nofollow noreferrer">Treemap</a> 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.</p>

<p>Just in case that is of use!</p>

CC BY-SA 3.0
2017-06-27T13:44:49.057Z
2017-06-27T13:44:49.057Z
Joe Healey
<p>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 (<a href="https://stackoverflow.com/questions/44006500/r-coloured-lines-for-tangelgram-package-ape-function-cophyloplot">see previous question</a>).</p>

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

<p>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: <img src="https://i.sstatic.net/HTDfR.jpg" alt=""></p>

<r><phylogeny><software-recommendation><phylogenetics>
How do I generate a color-coded tanglegram?
# How do I generate a color-coded tanglegram?

(Asked by: username_0 on 2017-06-27)

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](https://stackoverflow.com/questions/44006500/r-coloured-lines-for-tangelgram-package-ape-function-cophyloplot)).

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: ![](https://i.sstatic.net/HTDfR.jpg)

**Tags:** r, phylogeny, software-recommendation, phylogenetics

---

## 4 answers

### Answer by username_0 on 2017-06-28. Score: 3 | Accepted answer

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.

> **Comments:**
> **username_1**: 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.  
> **username_0**: 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.  

---

### Answer by username_1 on 2017-06-27. Score: 11

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:

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


---

### Answer by username_1 on 2017-06-28. Score: 3

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") 

library(ape)
library(dendextend)

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

str(site)
# 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)

> **Comments:**
> **username_0**: 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.  

---

### Answer by username_2 on 2017-06-27. Score: 2

As an alternative (though I realise this doesn't really answer the OPs question directly), [Dendroscope](http://dendroscope.org/) and [Treemap](https://sites.google.com/site/cophylogeny/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!


---

