Reputation: 11
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:
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")
Upvotes: 1
Views: 585
Reputation: 25306
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)
For more details, see here.
Upvotes: 1