4
votes

The following code will plot a phylogenetic tree with tip labels in italics and underscores replaced with spaces.

library(ape)
tr <- rtree(5, tip.label=c("a_b_x1", "b_c_2", "c_d_3y", "d_e_4", "e_f_5"))
plot(tr)

How can I combine regular text with text in italics in tip labels on a phylogeny? I'm interested in letters in italics and alphanumeric segment after the last underscore displayed as regular text.

I tried using expression with tiplabels, but it does not work due to interpretation of indexing. Also formatting sub to select the two parts in expression failed.

tip <- unlist(strsplit("a_b_x1", "_"))
tiplabels(expression(italic(paste(tip[1:length(tip)-1], collapse = " ")) * tip[length(tip)]), 
    tip = which(tr$tip.label == "a_b_x1"), frame = "n", bg = "white")
1

1 Answers

1
votes

We can construct expressions with bquote:

library(ape)
set.seed(1)
tr <- rtree(5, tip.label=c("a_b_x1", "b_c_2", "c_d_3y", "d_e_4", "e_f_5"))
plot(tr, show.tip.label = F)

for(i in seq_along(tr$tip.label)){
    tip <- unlist(strsplit(tr$tip.label[i], "_"))

    tiplabels(
        bquote(italic(.(paste(tip[-length(tip)], collapse = ' '))) ~ .(tip[length(tip)])),
        tip = i, adj = c(0, 0.5), frame = "n", bg = "white"
    )

}

enter image description here