#get the raxml file from example data
raxml_file <- system.file("extdata/RAxML", "RAxML_bipartitionsBranchLabels.H3", package="ggtree")
raxml <- read.raxml(raxml_file)
#build a data frame with the original tip names in one column and some random names in another
N<-length(raxml@phylo$tip.label) #save some typing
name<-data.frame(old_tip = raxml@phylo$tip.label, first = rep("A", N), second = c(1:N))
#create a second raxml object for easy comparison
raxml2<-raxml
#make an empty data frame to confirm what names are used
check<-data.frame(old_tip = rep("", N), new_tip = rep("", N), stringsAsFactors = F)
for (i in 1:N) {
#save names to table, old name pulled from raxml element, new name made on the fly from elements of name
check[i, ] = c(raxml@phylo$tip.label[i], do.call(paste, c(name[c("first","second")], sep = " "))[name$old_tip == raxml2@phylo$tip.label[i]])
#This is the part that actually changes the tip labels
raxml2@phylo$tip.label[i]<-do.call(paste, c(name[c("first","second")], sep = " "))[name$old_tip == raxml2@phylo$tip.label[i]]
}
#plot the original tree
ggtree(raxml) + geom_label(aes(label=bootstrap, fill=bootstrap)) + geom_tiplab() +
scale_fill_continuous(low='darkgreen', high='red') + theme_tree2(legend.position='right')
#plot the tree with tip labels changed
ggtree(raxml2) + geom_label(aes(label=bootstrap, fill=bootstrap)) + geom_tiplab() +
scale_fill_continuous(low='darkgreen', high='red') + theme_tree2(legend.position='right')