<p>After searching high and low I have found an answer from this <a href="https://stackoverflow.com/questions/7445684/how-to-convert-a-tree-to-a-dendrogram-in-r">thread</a></p>

<p>Workflow goes like:</p>

<pre><code>library(DECIPHER)
dend1 &lt;- ReadDendrogram(file="clipboard") 

# Alternatively to load from existing environment 
Tree$node.label &lt;- NULL # Need to remove node labels
dend1 &lt;- ReadDendrogram(textConnection(write.tree(Tree))) 
</code></pre>

<p>This object can then be used in the dendextend package and tanglegram function. </p>

CC BY-SA 3.0
2017-06-28T10:01:06.353Z
Does that give different results than the chronos function?
benn
CC BY-SA 3.0
2017-06-28T11:05:41.647Z
Contrary to what you say, you can read a character string from within R by passing a [`textConnection`](https://www.rdocumentation.org/packages/base/versions/3.4.0/topics/textConnection) to `ReadDendrogram`.
Konrad Rudolph
CC BY-SA 3.0
2017-06-28T11:17:07.387Z
Thanks @KonradRudolph I was not aware of that. Im only a novice to be honest. Ill change my answer above to include this.
AudileF
CC BY-SA 3.0
2017-06-29T08:08:21.520Z
Hi @KonradRudolph I have been trying the text connect as so:` dend1 <- ReadDendrogram(textConnection(Tree))` but I get an error. `Error in textConnection(Tree) : invalid 'text' argument`
AudileF
CC BY-SA 3.0
2017-06-29T10:08:37.720Z
@AudileF Did you read the documentation? What’s `Tree`? A representation of the dendrogram in text format?
Konrad Rudolph
CC BY-SA 3.0
2017-09-30T17:28:25.080Z
@AudileF not sure if you've figured it out already but what you should have done is `dend1 <- ReadDendrogram(textConnection(write.tree(Tree)))`
august
CC BY-SA 3.0
2017-10-02T10:44:00.527Z
I hadn't actually @august thanks a mill I'll include it above now.
AudileF
CC BY-SA 3.0
2017-06-28T09:23:27.727Z
2017-10-04T15:45:16.353Z
2017-10-04T15:45:16.353Z
AudileF
<p>For your example to work you should replace the <code>rtree</code> function with the <code>rcoal</code> function, see example below.</p>

<pre><code>library(ape)  
library(dendextend)   
Tree &lt;- rtree(10, rooted=F)
is.ultrametric(Tree) 
[1] FALSE 
is.binary.tree(Tree) 
[1] TRUE

Tree_rcoal &lt;- rcoal(10, rooted=F) 
is.ultrametric(Tree_rcoal) 
[1] TRUE
is.binary.tree(Tree_rcoal) 
[1] TRUE

Tree_rcoal2 &lt;- rcoal(10, rooted=F)

tanglegram(Tree_rcoal, Tree_rcoal2)
</code></pre>

<p>To make use of a file in newick format, you can try to convert with the <code>chronos</code> function.</p>

<pre><code>s &lt;- "owls(((Strix_aluco:4.2,Asio_otus:4.2):3.1,Athene_noctua:7.3):6.3,Tyto_alba:13.5);"
cat(s, file = "ex.tre", sep = "\n")
tree.owls &lt;- read.tree("ex.tre")
is.ultrametric(tree.owls)
[1] FALSE
dendrogram &lt;- chronos(tree.owls)

Setting initial dates...
Fitting in progress... get a first set of estimates
         Penalised log-lik = -24.04992 
Optimising rates... dates... -24.04992 
Optimising rates... dates... -24.04908 

Done.
is.ultrametric(dendrogram)
[1] TRUE
</code></pre>

CC BY-SA 3.0
2017-06-27T20:09:33.340Z
Hi b.nota. Thats all well and good but I have an actual newick file I need convert to a dendrogram. rtree was to give an example for troubleshooting. Thanks though.
AudileF
CC BY-SA 3.0
2017-06-27T21:15:39.557Z
Yes, I understand. But you didn't mention any newick file in your original post. Check [link] https://stackoverflow.com/questions/7445684/how-to-convert-a-tree-to-a-dendrogram-in-r this post from years ago, I think the chronos function might be helpful in your case.
benn
CC BY-SA 3.0
2017-06-27T19:56:19.770Z
2017-06-28T08:04:56.623Z
2017-06-28T08:04:56.623Z
benn
<p>I want to convert my phylogeny into a dendrogram so I can use it with dendextend in R to produce a tanglegram. I have made some progress but I keep encountering errors, see below:</p>

<pre><code>library(ape)
library(dendextend)

Tree &lt;- rtree(10, rooted=F)

Tree &lt;- read.tree(file="clipboard", text=NULL) 
# test &lt;- as.dendrogram(Tree) 
## Error in ape::as.hclust.phylo(object) : the tree is not ultrametric

Tree.ultra &lt;- chronos(Tree)  
# test &lt;- as.dendrogram(Tree.ultra) 
## Error in ape::as.hclust.phylo(object) : the tree is not rooted

Tree.ultra$root.edge &lt;- 0
# test &lt;- as.dendrogram(Tree.ultra) 
## Error in ape::as.hclust.phylo(object) : the tree is not binary
</code></pre>

<p>This <a href="https://www.biostars.org/p/4576/" rel="nofollow noreferrer">thread</a> seems to be the closest to answering my issues but the workflow is different and Im not sure how to address this binary issue. I have tried using as.hclust, as.hclust.phylo and hclust. </p>

CC BY-SA 3.0
2017-06-27T16:30:11.583Z
Please [edit] your question and give us a [minimal reproducible example](https://stackoverflow.com/q/5963269/1081936). Something that lets us test your code (and ours) and make sure our answers would work for you. A simple version of the tree you are using, for example, or even a way to generate random data that are similar to what you're working with.
terdon
CC BY-SA 3.0
2017-06-27T18:22:06.427Z
Its not really clear to me (and maybe it is just me) what you mean? A dendrogram and a phylogeny are synonyms. Unless theres some suble difference I'm not aware of, so I'll let others enlighten me if thats the case.
Joe Healey
CC BY-SA 3.0
2017-06-27T18:58:30.520Z
Ive added a small example which incurs the same errors. @JoeHealey. Depends whom you ask about dendrogram vs phylogeny. Many say they are synonyms but classically a dendrogram is seen as a simple graphical representation of hierarchy vs phylogeny which is based on more sound and substantial, evidence and data.
AudileF
CC BY-SA 3.0
2017-06-27T19:11:17.637Z
If dendrogram is the same as cladogram than it contains just a tree topology, phylogeny is more general term, it frequently contains branch lengths corresponding to mutation rate * time. See this [post](https://www.quora.com/What-is-the-difference-between-cladogram-and-phylogenetic-tree).
Kamil S Jaron
CC BY-SA 3.0
2017-06-28T14:53:28.600Z
I was googling and it seems that [dendrogram](https://en.wikipedia.org/wiki/Dendrogram) is a term used for visualisation of hierarchical clustering, which could be analogous to NJ-tree, but then it is not just a topology. If you mean cladogram, can you use this word?
Kamil S Jaron
<r><phylogenetics><phylogeny>
How to convert a phylogeny to a dendrogram in R
# How to convert a phylogeny to a dendrogram in R

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

I want to convert my phylogeny into a dendrogram so I can use it with dendextend in R to produce a tanglegram. I have made some progress but I keep encountering errors, see below:
library(ape)
library(dendextend)

Tree <- rtree(10, rooted=F)

Tree <- read.tree(file="clipboard", text=NULL) 
# test <- as.dendrogram(Tree) 
## Error in ape::as.hclust.phylo(object) : the tree is not ultrametric

Tree.ultra <- chronos(Tree)  
# test <- as.dendrogram(Tree.ultra) 
## Error in ape::as.hclust.phylo(object) : the tree is not rooted

Tree.ultra$root.edge <- 0
# test <- as.dendrogram(Tree.ultra) 
## Error in ape::as.hclust.phylo(object) : the tree is not binary

This thread seems to be the closest to answering my issues but the workflow is different and Im not sure how to address this binary issue. I have tried using as.hclust, as.hclust.phylo and hclust.

**Tags:** r, phylogenetics, phylogeny
> **Comments:**
> **username_1**: Please [edit] your question and give us a [minimal reproducible example](https://stackoverflow.com/q/5963269/1081936). Something that lets us test your code (and ours) and make sure our answers would work for you. A simple version of the tree you are using, for example, or even a way to generate random data that are similar to what you're working with.  
> **username_2**: Its not really clear to me (and maybe it is just me) what you mean? A dendrogram and a phylogeny are synonyms. Unless theres some suble difference I'm not aware of, so I'll let others enlighten me if thats the case.  
> **username_0**: Ive added a small example which incurs the same errors. @JoeHealey. Depends whom you ask about dendrogram vs phylogeny. Many say they are synonyms but classically a dendrogram is seen as a simple graphical representation of hierarchy vs phylogeny which is based on more sound and substantial, evidence and data.  
> **username_3**: If dendrogram is the same as cladogram than it contains just a tree topology, phylogeny is more general term, it frequently contains branch lengths corresponding to mutation rate * time. See this [post](https://www.quora.com/What-is-the-difference-between-cladogram-and-phylogenetic-tree).  
> **username_3**: I was googling and it seems that [dendrogram](https://en.wikipedia.org/wiki/Dendrogram) is a term used for visualisation of hierarchical clustering, which could be analogous to NJ-tree, but then it is not just a topology. If you mean cladogram, can you use this word?  

---

## 2 answers

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

After searching high and low I have found an answer from this thread
Workflow goes like:
library(DECIPHER)
dend1 <- ReadDendrogram(file="clipboard") 

# Alternatively to load from existing environment 
Tree$node.label <- NULL # Need to remove node labels
dend1 <- ReadDendrogram(textConnection(write.tree(Tree))) 

This object can then be used in the dendextend package and tanglegram function.

> **Comments:**
> **username_4**: Does that give different results than the chronos function?  
> **username_5**: Contrary to what you say, you can read a character string from within R by passing a [`textConnection`](https://www.rdocumentation.org/packages/base/versions/3.4.0/topics/textConnection) to `ReadDendrogram`.  
> **username_0**: Thanks @KonradRudolph I was not aware of that. Im only a novice to be honest. Ill change my answer above to include this.  
> **username_0**: Hi @KonradRudolph I have been trying the text connect as so:` dend1 <- ReadDendrogram(textConnection(Tree))` but I get an error. `Error in textConnection(Tree) : invalid 'text' argument`  
> **username_5**: @AudileF Did you read the documentation? What’s `Tree`? A representation of the dendrogram in text format?  
> **username_6**: @AudileF not sure if you've figured it out already but what you should have done is `dend1 <- ReadDendrogram(textConnection(write.tree(Tree)))`  
> **username_0**: I hadn't actually @august thanks a mill I'll include it above now.  

---

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

For your example to work you should replace the rtree function with the rcoal function, see example below.
library(ape)  
library(dendextend)   
Tree <- rtree(10, rooted=F)
is.ultrametric(Tree) 
[1] FALSE 
is.binary.tree(Tree) 
[1] TRUE

Tree_rcoal <- rcoal(10, rooted=F) 
is.ultrametric(Tree_rcoal) 
[1] TRUE
is.binary.tree(Tree_rcoal) 
[1] TRUE

Tree_rcoal2 <- rcoal(10, rooted=F)

tanglegram(Tree_rcoal, Tree_rcoal2)

To make use of a file in newick format, you can try to convert with the chronos function.
s <- "owls(((Strix_aluco:4.2,Asio_otus:4.2):3.1,Athene_noctua:7.3):6.3,Tyto_alba:13.5);"
cat(s, file = "ex.tre", sep = "\n")
tree.owls <- read.tree("ex.tre")
is.ultrametric(tree.owls)
[1] FALSE
dendrogram <- chronos(tree.owls)

Setting initial dates...
Fitting in progress... get a first set of estimates
         Penalised log-lik = -24.04992 
Optimising rates... dates... -24.04992 
Optimising rates... dates... -24.04908 

Done.
is.ultrametric(dendrogram)
[1] TRUE

> **Comments:**
> **username_0**: Hi b.nota. Thats all well and good but I have an actual newick file I need convert to a dendrogram. rtree was to give an example for troubleshooting. Thanks though.  
> **username_4**: Yes, I understand. But you didn't mention any newick file in your original post. Check [link] https://stackoverflow.com/questions/7445684/how-to-convert-a-tree-to-a-dendrogram-in-r this post from years ago, I think the chronos function might be helpful in your case.  

---

