简体   繁体   中英

Adding multiple labels to a branch in a phylogenetic tree using geom_label

I am VERY new to R, so I am sorry if this question is obvious. I would like to add multiple labels to branches in a phylogenetic tree, but I can only figure out how to add one label per branch. I am using the following code:

treetext = "(Japon[&&NHX:S=2],(((Al,Luteo),(Loam,(Niet,Cal))),(((Car,Bar),(Aph,Long[&&NHX:S=1],((Yam,Stig),((Zey,Semp),(A,(Hap,(This,That))))))));"

mytree <- read.nhx(textConnection(treetext))

ggtree(mytree) + geom_tiplab() +
  geom_label(aes(x=branch, label=S))

I can add multiple symbols to a branch using the code below, but is so labor-intensive that I may as well do it by hand:

ggtree(mytree) + 
  geom_tiplab()+
  geom_nodepoint(aes(subset = node == 32, x = x - .5),
                 size = 5, colour = "black", shape = 15) +
  geom_nodepoint(aes(subset = node == 32, x = x - 2),
                 size = 5, colour = "gray", shape = 15)

Thanks in advance for any tips or leads!

A solution using the "ape" package would be:

library(ape)
mytree <- rtree(7) # A random tree
labels1 <- letters[1:6]
labels2 <- LETTERS[1:6]

plot(mytree)
# Setting location of label with `adj`
nodelabels(labels1, adj = c(1, -1), frame = 'none') 
# We could also use `pos =` 1: below node; 3: above node
nodelabels(labels2, pos = 1, frame = 'n')

绘图输出

You might want to tweak the adj parameter to set the location as you desire it.

As I couldn't parse the treetext object you provided as an example (unbalanced braces), and I'm not familiar with how read.nhx() stores node labels, you might need a little R code to extract the labels elements; you can use a bare nodelabels() to plot the numbers of the nodes on the tree to be sure that your vectors are in the correct sequence.

If you wanted labels to appear on edges rather than at nodes, the function is ape::edgelabels() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM