1

I read in a tree (.nex), convert it to dendro class, and plot it using ggdendrogram from ggplot2. How can I position the tip labels next to the tips in the dendrogram and not at the bottom?

mytree <- read.nexus('mytree.nex')
den_data_mytree <- dendro_data(as.dendrogram(mytree))
pdf('mytree.pdf', h=55,w=55)
ggdendrogram(den_data_mytree, theme_dendro=F, labels=F) +
  labs(x = "x label", y = "y label", title = "Title") + 
  geom_text(data = den_data_mytree$labels, aes(x, y, label = label,color=den_data_mytree$labels$group), hjust = 1.2, size = 9, angle=90) + 
  scale_colour_manual(values=c("purple","orange")) +
  theme(axis.text=element_text(size=24), axis.title=element_text(size=54,face="bold"), title = element_text(size=54), legend.title = element_blank())
dev.off()

The output is this image:

ggdendogram

Here is the data:

>dput(core_snp_tree)
structure(list(edge = structure(c(109L, ..., 108L), .Dim = c(213L, 2L)), edge.length = c(0.00373, 0,  3e-05, ..., 0.00844), Nnode = 106L,      node.label = c("", "100.00", ..., "100.00"), tip.label = c("list","of","tip","labels"), root.edge = 0), class = "phylo", order = "cladewise")
Tal Galili
  • 24,605
  • 44
  • 129
  • 187
echo
  • 11
  • 3
  • Can you post the output of `dput(mytree)`? – Werner Hertzog Dec 11 '20 at 00:36
  • It is in the following format: ```> dput(core_snp_tree) structure(list(edge = structure(c(109L, ..., 108L), .Dim = c(213L, 2L)), edge.length = c(0.00373, 0, 3e-05, ..., 0.00844), Nnode = 106L, node.label = c("", "100.00", ..., "100.00"), tip.label = c("list","of","tip","labels"), root.edge = 0), class = "phylo", order = "cladewise") ``` – echo Dec 11 '20 at 08:11
  • The dput seems wrong: ``` > core_snp_tree <- structure(list(edge = structure(c(109L, ..., 108L), .Dim = c(213L, 2L)), edge.length = c(0.00373, 0, 3e-05, ..., 0.00844), Nnode = 106L, node.label = c("", "100.00", ..., "100.00"), tip.label = c("list","of","tip","labels"), root.edge = 0), class = "phylo", order = "cladewise") Error in structure(c(109L, ..., 108L), .Dim = c(213L, 2L)) : '...' used in an incorrect context ``` – Tal Galili Dec 12 '20 at 15:14

1 Answers1

1

You'd probably want to use the dendextend package, with the hang.dendrogram function together with ggdend (the simplest way is to use ggplot call on the dendrogram object). In general, dendextend::ggdend is a more extended version of ggdendro.

Example:

hc <- hclust(dist(USArrests[1:5, ]), "ave")
library(dendextend)
dend <- hang.dendrogram(as.dendrogram(hc))
library(ggplot2)
ggplot(dend)

enter image description here

For more details, see here.

Tal Galili
  • 24,605
  • 44
  • 129
  • 187
  • When I try the command dend <- hang.dendrogram(as.dendrogram(hc)) with my nexus tree (from Raxml), I get this error: 'Error in ape::as.hclust.phylo(object) : the tree is not ultrametric' What should I do next? – echo Jan 08 '21 at 01:11
  • It means you probably have some branches that are smaller then their leaves. You should take a look at your tree and see if it "makes sense" – Tal Galili Jan 08 '21 at 15:31
  • You may also have a bunch of leaves with height of 0... – Tal Galili Jan 08 '21 at 15:32