Plot of face-to-face phylogenetic trees with links in R
I would like to plot two phylogenies against each other in R using a package ape
. One tree has 40 nodes and has 26 nodes:
library(ape)
tree1 <- rtree(40)
tree2 <- rtree(26)
The function cophyloplot
displays them face to face with the specified links.
I'm having trouble pointing out the links.
Note that in my actual tree files, the nexus
tag labels are text (and I'm not sure how to change them to numbers if needed ...).
Links should be as follows:
If the tree1
nexus file has sequence tag labels 1-40. In the tree2
nexus file, the tag labels are 1-26. Then the links should be:
a <- c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40)
b <- c(14,1,4,1,9,12,2,10,6,3,13,5,14,15,18,19,19,7,14,9,10,11,25,22,21,16,23,24,26,17,1,12,12,21,15,16,21,8,20,21)
association <- cbind(a, b)
(i.e. sequence 1 in tree1
is linked to sequence 14 in tree2
)
So, I use something like this to build trees:
cophyloplot(tree1, tree2, assoc=association,length.line=4, space=28, gap=10, rotate=TRUE)
And calculate the distance matrix:
dist.topo(tree1, tree2, method = "PH85")
I'm not entirely sure where I am going wrong. Any help would be appreciated!
source to share
To build trees try
library(ape)
set.seed(1)
# create trees
tree1 <- rtree(40)
tree2 <- rtree(26)
# modify tip labels
tree1$tip.label <- sub("t", "", tree1$tip.label, fixed = T)
tree2$tip.label <- sub("t", "", tree2$tip.label, fixed = T)
# create associations matrix
a <- as.character(c(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40))
b <- as.character(c(14,1,4,1,9,12,2,10,6,3,13,5,14,15,18,19,19,7,14,9,10,11,25,22,21,16,23,24,26,17,1,12,12,21,15,16,21,8,20,21))
association <- cbind(a, b)
# plot
cophyloplot(tree1, tree2, assoc = association, length.line = 4, space = 28, gap = 3)
source to share
The function cophyloplot
does not require indexing the token mark. You can call taxa by their proper names. Note that lukeA's answer stores numbers from associations as character
. Converting them to text that matches the tooltip labels and building two trees shows the same result.
association <- apply(association, 2, function(x) sub("^","t", x))
head(association)
# a b
# [1,] "t1" "t14"
# [2,] "t2" "t1"
# [3,] "t3" "t4"
# [4,] "t4" "t1"
# [5,] "t5" "t9"
cophyloplot(tree1, tree2, assoc=association, length.line=4, space=28, gap=3)
The order in which the associations are listed in the matrix is ββirrelevant. The best practice would be to import them from an external file using read.table()
.
source to share