YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/treedata-book/
821 stars 172 forks source link

How to get the node number #586

Open Ruiqi-CUB opened 10 months ago

Ruiqi-CUB commented 10 months ago

Hi Dr. Yu,

I am trying to annotate my tree in ggtree. Do you happen to know How I can get the node number? Is there a way to plot tree with tip/internal node numbers?

Thanks a lot

brj1 commented 10 months ago

You can get them with geom_text, geom_nodelab, geom_tiplab, etc. by setting label = node in the aes. For example:

tree <- rtree(10)

ggtree(tree) + geom_text(aes(label = node),  hjust = -0.5) # node number

ggtree(tree) + geom_nodelab(aes(label = node), hjust = -0.5)  # just internal node number

ggtree(tree) + geom_tiplab(aes(label = node), hjust = -0.5)  # just tip number

The hjust moves the labels over a bit so they can be more easily seen.