label edge number in ggtree

[This article was first published on R on G. Yu, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

set.seed(1)
tr = rtree(30)
library(ape)
plot(tr, main="ape")
edgelabels()

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.

library(ggtree)
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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)