Monday, October 23, 2017

Using different link line types & widths in plot.cophylo

Recently, a commenter on the blog asked:

“Is it possible to use different line types (Ed.: in plot.cophylo) (e.g. dashed via solid) with link.lty on the same plot?”

The answer is that 'yes' this is already possible. We just have to supply our vector link.lty in the same order as the rows of our table of associations between the two trees, assoc. For instance, here I will use different link types & widths:

library(phytools)
t1
## 
## Phylogenetic tree with 26 tips and 25 internal nodes.
## 
## Tip labels:
##  A, B, C, D, E, F, ...
## 
## Rooted; includes branch lengths.
t2
## 
## Phylogenetic tree with 40 tips and 39 internal nodes.
## 
## Tip labels:
##  t1, t2, t3, t4, t5, t6, ...
## 
## Rooted; includes branch lengths.
assoc
##       [,1] [,2] 
##  [1,] "A"  "t1" 
##  [2,] "B"  "t3" 
##  [3,] "C"  "t4" 
##  [4,] "D"  "t5" 
##  [5,] "E"  "t7" 
##  [6,] "F"  "t8" 
##  [7,] "G"  "t9" 
##  [8,] "H"  "t10"
##  [9,] "I"  "t11"
## [10,] "J"  "t15"
## [11,] "K"  "t16"
## [12,] "L"  "t18"
## [13,] "M"  "t19"
## [14,] "N"  "t20"
## [15,] "O"  "t23"
## [16,] "P"  "t25"
## [17,] "Q"  "t26"
## [18,] "R"  "t27"
## [19,] "S"  "t28"
## [20,] "T"  "t29"
## [21,] "U"  "t30"
## [22,] "V"  "t33"
## [23,] "W"  "t34"
## [24,] "X"  "t35"
## [25,] "Y"  "t37"
## [26,] "Z"  "t39"
lty<-rep("solid",nrow(assoc))
lty[c(6,15,24,25)]<-"dashed"
lwd<-rep(2,nrow(assoc))
lwd[c(6,15,24,25)]<-1
obj<-cophylo(t1,t2,assoc)
## Rotating nodes to optimize matching...
## Done.
plot(obj,link.lty=lty,link.type="curved",link.lwd=lwd,
    link.col=make.transparent("blue",0.4),fsize=c(0.9,0.7))

plot of chunk unnamed-chunk-1

That's it. I simulated the trees for this example as follows:

library(phytools)
library(phangorn)
t1<-t2<-pbtree(n=40,tip.label=paste("t",1:40,sep=""))
assoc<-cbind(t1$tip.label,t2$tip.label)
t1<-drop.tip(t1,sample(t1$tip.label,14))
rownames(assoc)<-t2$tip.label
assoc<-assoc[t1$tip.label,]
assoc[,1]<-LETTERS
t1$tip.label<-LETTERS
rownames(assoc)<-NULL
t2<-rSPR(t2,moves=3)
t2<-force.ultrametric(t2)

2 comments:

  1. Super cool, thanks for the response

    Clive

    ReplyDelete
  2. Is it possible to alter the width of the linked line plot area in order to bring the two trees closer together?

    ReplyDelete

Note: due to the very large amount of spam, all comments are now automatically submitted for moderation.