If I've interpreted correctly, you want the time to the most recent common ancestor of any two species in the tree? If so, something like the following works:
library("ggtree")
library("treeio")
require("dplyr")
beast_file <- system.file("examples/MCC_FluA_H3.tree",
package="ggtree")
beast_tree <- read.beast(beast_file)
beast_tree@data %>% dplyr::select(node,height)
# choose some random tips for the purpose of this answer
tip1 <- beast_tree@phylo$tip.label[1]
tip2 <- beast_tree@phylo$tip.label[47]
mrca <- MRCA(beast_tree,tip1,tip2)
time_of_mrca <- beast_tree@data %>% dplyr::filter(node == mrca) %>% dplyr::select(height)
# use a function instead
get_mrca_height = function(tree,tip1,tip2){
mrca_node <- MRCA(tree,tip1,tip2)
mrca_height <- tree@data %>% dplyr::filter(node == mrca_node) %>% dplyr::pull(height)
result <- dplyr::tibble(tip1=tip1, tip2=tip2, mrca_node_number=mrca_node,mrca_height=mrca_height)
return(result)
}
get_mrca_height(beast_tree,tip1,tip2)
Output of the last function:
> get_mrca_height(beast_tree,tip1,tip2)
# A tibble: 1 × 4
tip1 tip2 mrca_node_number mrca_height
<chr> <chr> <int> <dbl>
1 A/Hokkaido/30-1-a/2013 A/Swine/Binh_Duong/03_10/2010 96 9.16