Search code examples
rphylogenyapeape-phylo

Coloured lines for tangelgram - package ape function cophyloplot


I am trying to do a phylogenetic comparison of two trees which contain the same taxa. I want to colour the connections based on isolation site. I had thought I had performed this successfully but there is error in my work flow i.e. the coloured lines are not corresponding to isolation site accurately . I was wondering if you have any insights, please find my reproducible example below.

site <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L,9L, 10L, 2L), .Label = c("t1", "t10", "t2", "t3", "t4", "t5","t6", "t7", "t8", "t9"), class = "factor"), site = c(1L, 1L,1L, 2L, 2L, 3L, 1L, 3L, 2L, 2L)), .Names = c("name", "site"), row.names = c(NA,10L), class = "data.frame") 

library(ape)
t1 <- rtree(10)
t2 <- rtree(10)
order <- cbind(t1$tip.label)
list <- merge(order, site, by.x="V1", by.y="name")
x <- list$site
A <- cbind(t1$tip.label, t1$tip.label)
cophyloplot(t1, t2, assoc = A, show.tip.label = T, space=50, col = x) 

As it stands this is my current output:

phylogenetic trees compared


Solution

  • Just spotted this thread on extracting tip labels and it works. correct order of tip labels in ape

    I also need to incorporate sort=F into the merge function.

    So for a finish the workflow looks like:

    site <- structure(list(name = structure(c(1L, 3L, 4L, 5L, 6L, 7L, 8L,9L, 
    10L, 2L), .Label = c("t1", "t10", "t2", "t3", "t4", "t5","t6", "t7", "t8", 
    "t9"), class = "factor"), site = c(1L, 1L,1L, 2L, 2L, 3L, 1L, 3L, 2L, 2L)), 
    .Names = c("name", "site"), row.names = c(NA,10L), class = "data.frame") 
    
    library(ape)
    t1 <- rtree(10)
    t2 <- rtree(10)
    is_tip<- t1$edge[,2] <= length(t1$tip.label)
    ordered_tips <- t1$edge[is_tip,2]
    order <-t1$tip.label[ordered_tips]
    order <- as.data.frame(order)
    list <- merge(order, site, by.x="V1", by.y="name", sort=F)
    x <- list$site
    A <- cbind(t1$tip.label, t1$tip.label)
    cophyloplot(t1, t2, assoc = A, show.tip.label = T, space=50, col = x)