Just adding a code to ensure the order also matches
#random tree
oldTree <- ape::read.tree(text = "(t2, (t5, (t4, (t6, (t3, t1)))));") plot(oldTree)
df <- cbind(old.name=oldTree$tip.label, new_name = c(rbind("tip_2", "tip_5", "tip_4","tip_6", "tip_3", "tip_1"))) %>% as.data.frame()
#Get the position of elements -- matching tip labels with the label of the dataframe
pos_id <-match(oldTree$tip.label, df$old.name) pos_id #element position newTree <- oldTree
newTree$tip.label <- df$new_name[pos_id] #here sorting by pos_id par(mfrow=c(1,2))
plot(oldTree) plot(newTree)
Alternatively look up rename_taxa in the tree_io package the code is the same for that. https://guangchuangyu.github.io/2018/04/rename-phylogeny-tip-labels-in-treeio/
But I find the syntax of the ggtree, and treeio a little troublesome.