rggplot2bioinformaticsphylogenyggtree

How do I annotate a ggtree object with bootstrap values?


I have a phylogenetic tree of the class phylo with 24 tips and 23 internal nodes. I ran a bootstrap analysis on this tree and the data using boot.phylo, which returned a vector of 23 bootstrap values. I created a ggtree object from my original tree and am now trying to add the bootstrap values to the nodes. I am doing something wrong but I don't know what.

Here is what I did:

gg.tr <- ggtree(mp.tree)
gg.tr + geom_label2(aes(subset=!isTip, label=bphylo$BP))

bphylo$BP is the vector of 23 bootstrap values. When I run this code, I get the following error:

Error: Aesthetics must be either length 1 or the same as the data (47): subset, label, x, y

I don't understand this error, because I only want to put the bootstrap values on 23 of the possible 47 locations.

When I call the following function, I get a value of 23:

length(which(gg.tr$data$isTip==FALSE))

If the length of gg.tr$data$isTip==FALSE is 23 and I have 23 bootstrap values, why am I getting an error telling me that my label is the wrong length?


Solution

  • You can annotate your tree with geom_text. Without seeing your data, it's hard to know what is happening, but here is an example with a reproducible dataset.

    library(devtools)
    devtools::install_github("GuangchuangYu/ggtree")
    
    bs <- data.frame(nodename = c("t30", "t12", "t22", "t26", "t6", "t17", "t4","t7", "t9", "t1", "t8", "t25","t23",
                              "t28", "t10", "t20", "t3",  "t11", "t19", "t29", "t2","t18", "t24",
                              "t27", "t15", "t13", "t14", "t16", "t5","t21"), bootstrap = c(runif(30, 85, 98)), stringsAsFactors = F)
    bs$bootstrap <- round(bs$bootstrap, digits = 0)
    rownames(bs) <- NULL
    nodesupport = c(round(runif(59, 65, 80), digits = 0))
    
    set.seed(0)
    tree <- rtree(30)
    p <- ggtree(tree)
    p <- p %<+% bs + geom_tiplab()
    p <- p + geom_text(aes(label = bootstrap), hjust = 1, vjust = -0.4, size = 3) + geom_nodelab(aes(label = nodesupport)) # specify your node label here, looks like BP
    p