library("ggplot2")
library("ggtree")
library("treeio")
library("tidytree")
#tree
nwk <- "example.phy.treefile"
tree <- read.tree(nwk)
#Extract node label
bootsv <-data.frame(tree[["node.label"]])
ntip <- length(tree[["tip.label"]])
nnode <- tree[["Nnode"]]
SHaLRT <- data.frame(lapply(bootsv, function(x) as.numeric(sub("/.*", "",x))))
SHaLRT$node <- c((ntip + 1) : (ntip + nnode))
colnames(SHaLRT) <- c("label","node")
# cut data
SHaLRT$label <- cut(as.numeric(SHaLRT$label), breaks = c(0,80,85,90,95,100),right = F,include.lowest = TRUE)
# rename levels
levels(SHaLRT$label) <- c("0<=SHaLRT<80","80<=SHaLRT<85", "85<=SHaLRT<90", "90<=SHaLRT<95", "95<=SHaLRT<=100")
# remove null levels
SHaLRT$label <- SHaLRT$label[, drop=T]
#Put the results back into the tree.
tree[["node.label"]] <- SHaLRT$label
#make a color list according to levels
fun_color_range <- colorRampPalette(c("#f7f7f7", "#252525")) # set color range
my_colors <- fun_color_range(nlevels(SHaLRT$label))
#plot
treeplot <- ggtree(tree) +
xlim(0,1)+ # adjust width of tree
geom_point2(aes(subset = !isTip,color = label), size=3,shape=19)+
geom_tiplab(align=TRUE, linesize=0.25)+
scale_color_manual("SHaLRT branch support", #legend name
values = my_colors, #legend color
na.translate = F,#remove na
labels = levels(SHaLRT$label)) #rename key label of legend with levels