0

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)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • What packages did you load? Is working with ggtree critical for you? – nya Jan 25 '22 at 11:51
  • Hi nya :) I loaded packages ape, phytools, ggtree and ggraph. It is not critical at all to use ggtree - it's just the one package I found that may be able to do what I need (although I have now started making the figure I want "by hand" using editing software) – Marybel Soto Gomez Jan 29 '22 at 20:50

1 Answers1

1

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')

Plot output

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

Martin Smith
  • 3,687
  • 1
  • 24
  • 51