# How to convert a phylogeny to a dendrogram in R

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)

# 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. • Please edit your question and give us a minimal reproducible example. 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. Jun 27, 2017 at 16:30 • 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. Jun 27, 2017 at 18:22 • 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. Jun 27, 2017 at 18:58 • 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. Jun 27, 2017 at 19:11 • I was googling and it seems that 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? Jun 28, 2017 at 14:53 ## 2 Answers 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  • 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. Jun 27, 2017 at 20:09 • Yes, I understand. But you didn't mention any newick file in your original post. Check [link] stackoverflow.com/questions/7445684/… this post from years ago, I think the chronos function might be helpful in your case. – benn Jun 27, 2017 at 21:15 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

• Contrary to what you say, you can read a character string from within R by passing a textConnection to ReadDendrogram. Jun 28, 2017 at 11:05
• 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 Jun 29, 2017 at 8:08
• @AudileF not sure if you've figured it out already but what you should have done is dend1 <- ReadDendrogram(textConnection(write.tree(Tree))) Sep 30, 2017 at 17:28