[英]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.我对 R 非常陌生,所以如果这个问题很明显,我很抱歉。 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.
我想为系统发育树中的分支添加多个标签,但我只能弄清楚如何为每个分支添加一个 label。 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:使用“猿”package 的解决方案是:
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.您可能需要调整
adj
参数以根据需要设置位置。
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;由于我无法解析您作为示例(不平衡大括号)提供的树形文本
treetext
,而且我不熟悉read.nhx()
如何存储节点标签,因此您可能需要一点 R 代码来提取labels
元素; 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.您可以使用裸
nodelabels()
到 plot 树上的节点数,以确保您的向量的顺序正确。
If you wanted labels to appear on edges rather than at nodes, the function is ape::edgelabels()
.如果您希望标签出现在边缘而不是节点上,则 function 是
ape::edgelabels()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.