label edge number in ggtree

January 12, 2016

(This article was first published on R on G. Yu, and kindly contributed to R-bloggers)

This is a question from ggtree user. In ape and phytools, it’s easy to label edge using the edgelabels function.

tr = rtree(30)
plot(tr, main="ape")

I don’t see any necessity to label edge numbers, as they are meaningless. The number is labeled as the row index of tr$edge, and edge can be uniquely mapped to child node. If we need to relate something to edge, we can relate them to corresponding child node. Node is a central hub in tree annotation in ggtree.

To label edge number, we can also attach the edge number to node and then label them as a node attribute.

p = ggtree(tr, ladderize=F) + geom_tiplab() + ggtitle("ggtree")
edge=data.frame(tr$edge, edge_num=1:nrow(tr$edge))
colnames(edge)=c("parent", "node", "edge_num")
p %<+% edge + geom_label(aes(x=branch, label=edge_num))

To leave a comment for the author, please follow the link and comment on their blog: R on G. Yu. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)