最简单的解决方案确实是更改对象tip.label
中的元素phylo
。
## Making a tree with three tips of 20 characters each
tree <- rcoal(3, tip.label = replicate(3, paste(sample(letters, 20), collapse = "")))
## The tree tip labels
tree$tip.label
# [1] "thsdmrigufpykvlawqbz" "dlicefyjonmqugbptxzr" "adioznspgbkjqryelfum"
然后,您可以通过换行符 ( ) 将每个第 n 个字符换行\n
,如下所示:
## Declaring the pattern and text to replace at a specific position
position <- 10
pattern <- paste0('^([a-z]{', position-1, '})([a-z]+)$')
text <- paste0('\\1', "\n", '\\2')
wrap_tips <- gsub(pattern, text, tree$tip.label)
wrap_tips
# [1] "thsdmrigu\nfpykvlawqbz" "dlicefyjo\nnmqugbptxzr" "adioznspg\nbkjqryelfum"
当然,您也可以用更简单的特定字符(如_
或.
)替换(例如)。\n
gsub("_", "\\n", tree$tip.label)
然后,您可以创建树的副本并为其提供包装的提示标签:
## Duplicating the tree
tree_to_plot <- tree
## Changing the labels
tree_to_plot$tip.label <- wrap_tips
## Plotting the wrapped labels
plot(tree_to_plot)