I'm building a tree with ggtree on R.
library(ggtree)
here is the newick file content:
tree = read.newick("newick_tree")
Here is the code I use:
colfunc <- colorRampPalette(c("red", "blue"))
col=colfunc(110)
for (i in 1:length(tree$node.label)){
if(as.vector(tree$node.label[i])>0.99){
col[i]<-'#CB1414'
} else if(as.vector(tree$node.label[i])>0.90 & (as.vector(tree$node.label[i]) < 0.99)){
col[i]<-'#F0F014'
} else if(as.vector(tree$node.label[i])>0.76 & (as.vector(tree$node.label[i]) < 0.90)){
col[i]<-'#098527'
} else if(as.vector(tree$node.label[i])>0.51 & (as.vector(tree$node.label[i]) < 0.76)){
col[i]<-'#18D8F7'
} else if(as.vector(tree$node.label[i])>0 & (as.vector(tree$node.label[i]) < 0.50)){
col[i]<-'#1B72DB'
}
}
p<-ggtree(tree,size=0.5,branch.length="none") +geom_tiplab(size = 2, col ="black") + geom_nodepoint(color=col, alpha=1, size=1.5, show.legend = TRUE)+
geom_treescale(x=30, y=1)
In order to get point color depending on the bootstrap level I used this part: geom_nodepoint(color=col, alpha=1, size=1.5, show.legend = TRUE)
where col
is a variable such as:
> col
[1] "#CB1414" "#FC0002" "#FA0004" "#F70007" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
[15] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#F0F014" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
[29] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
[43] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#F0F014" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#098527" "#CB1414"
[57] "#CB1414" "#18D8F7" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
[71] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#F0F014" "#098527" "#098527" "#CB1414"
[85] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#F0F014" "#CB1414" "#098527" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#1B72DB"
[99] "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414" "#CB1414"
That I made depending on the number in this variable:
> tree$node.labe
[1] "1" "" "" "0.99" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
[21] "1" "0.97" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
[41] "1" "1" "1" "1" "1" "1" "1" "1" "1" "0.98" "1" "1" "1" "1" "0.89" "1" "1" "0.64" "1" "1"
[61] "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
[81] "0.92" "0.8" "0.77" "1" "1" "1" "1" "1" "0.96" "1" "0.84" "1" "1" "1" "1" "1" "1" "0.48" "1" "1"
[101] "1" "1" "1" "1" "1" "1" "1" "1" "1" "1"
And now I just would like to display the 5 ranges color legend in the tree such as :
but the option show.legend = TRUE
does not work for that.
Does anyone have an idea? I also tried to add p + theme(legend.position="right")
but there is still no legend.
Thank you for your help.
here is the deput(tree)
The number of node points in your tree is 221, with 111 final points. Thus, your col
vector needs to have 221 elements; the first 111 specify the colors of the final points.
Here is a way to create the proper col
vector starting from tree$node.label
:
x <- c(rep(NA,111), as.numeric(tree$node.label)*100)
col <- cut(x, breaks=c(0,50,75,90,99,100))
col <- factor(col, levels=rev(levels(col)))
col <- factor(col, labels=c("100%","91-99%","76-90%","51-75%","0-50%"))
Node points can be colored using the color
aesthetic:
library(ggtree)
library(ggplot2)
p <- ggtree(tree, size=0.5,branch.length="none") +
geom_tiplab(size = 2, col ="black") +
geom_point(aes(color=col), alpha=1, size=1.5, show.legend = TRUE) +
geom_treescale(x=30, y=1) +
theme(legend.position="right") +
scale_colour_manual(na.translate = F, name="Bootstrap support",
values=c("#CB1414","#F0F014","#098527","#18D8F7","#1B72DB")) +
guides(color = guide_legend(override.aes = list(size = 5)))
p