Friday, August 19, 2016

New user control of plot method for "cophylo" objects

Following a suggestion by Eliot Miller, I have added some additional user control to the S3 plot method for objects of class "cophylo". These options now permit user control of basically all aspects of the linking lines - from the line type (argument link.lty), to the line color (link.col) and line width (link.lwd). These arguments can be supplied as a single value, or as a vector with a length equal to the number of rows in the association table, assoc. These updates can be seen here.

I also added a summary method for the object class that prints out the association table, assoc, along with some other information about the object. This update I pushed separately and can be viewed here.

We can try it out as follows:

library(phytools)
t1
## 
## Phylogenetic tree with 20 tips and 19 internal nodes.
## 
## Tip labels:
##  Z._szgp, D._hgpq, K._ogeszq, D._xhwpobav, S._oyqfmig, Q._bchviyf, ...
## 
## Rooted; includes branch lengths.
t2
## 
## Phylogenetic tree with 40 tips and 39 internal nodes.
## 
## Tip labels:
##  G._tbvmo, I._hrtmxoy, D._qonmgjx, E._dlmov, X._pinwva, C._zpekl, ...
## 
## Rooted; includes branch lengths.
assoc
##   [,1]          [,2]         
## D "D._hgpq"     "Q._eocwr"   
## D "D._hgpq"     "B._galucwd" 
## D "D._hgpq"     "E._helanvgx"
## D "D._xhwpobav" "E._dlmov"   
## D "D._xhwpobav" "C._zpekl"   
## E "E._chlauw"   "S._stcn"    
## E "E._chlauw"   "Q._eocwr"   
## E "E._poqyd"    "D._jlrpfati"
## E "E._poqyd"    "C._zpekl"   
## E "E._poqyd"    "Q._eocwr"   
## E "E._poqyd"    "I._zjyhw"   
## F "F._djztolsb" "K._tiwfhler"
## F "F._djztolsb" "E._dlmov"   
## I "I._infumtpg" "G._tbvmo"   
## I "I._ymzacesf" "D._qonmgjx" 
## I "I._ymzacesf" "C._zpekl"   
## K "K._guxhip"   "W._xgapntmd"
## K "K._guxhip"   "B._qtkl"    
## K "K._hswluo"   "B._galucwd" 
## K "K._ogeszq"   "C._zpekl"   
## L "L._pgayvzc"  "Q._eocwr"   
## L "L._pgayvzc"  "K._tiwfhler"
## M "M._vksyp"    "G._tbvmo"   
## M "M._vksyp"    "D._pqfdcnsx"
## M "M._vksyp"    "S._stcn"    
## N "N._panljtox" "W._xgapntmd"
## N "N._panljtox" "O._ytjn"    
## Q "Q._bchviyf"  "D._pqfdcnsx"
## Q "Q._bchviyf"  "S._stcn"    
## Q "Q._bchviyf"  "A._ahodzte" 
## Q "Q._iqzp"     "O._ytjn"    
## S "S._oyqfmig"  "B._qtkl"    
## S "S._oyqfmig"  "A._ahodzte" 
## S "S._oyqfmig"  "E._helanvgx"
## Y "Y._itpzkvc"  "I._zjyhw"   
## Y "Y._itpzkvc"  "D._qonmgjx" 
## Y "Y._vnlf"     "D._jlrpfati"
## Z "Z._szgp"     "S._stcn"    
## Z "Z._szgp"     "B._fvakurtm"
## Z "Z._szgp"     "B._fvakurtm"
obj<-cophylo(t1,t2,assoc)
## Rotating nodes to optimize matching...
## Done.
summary(obj)
## 
## Co-phylogenetic ("cophylo") object: obj 
## 
## Tree 1 (left tree) is an object of class "phylo" containing 20 species.
## 
## Tree 2 (right tree) is an object of class "phylo" containing 40 species.
## 
## Association (assoc) table as follows:
## 
##  left:   ----    right:
##   D._hgpq    ----     Q._eocwr 
##   D._hgpq    ----     B._galucwd 
##   D._hgpq    ----     E._helanvgx 
##   D._xhwpobav    ----     E._dlmov 
##   D._xhwpobav    ----     C._zpekl 
##   E._chlauw  ----     S._stcn 
##   E._chlauw  ----     Q._eocwr 
##   E._poqyd   ----     D._jlrpfati 
##   E._poqyd   ----     C._zpekl 
##   E._poqyd   ----     Q._eocwr 
##   E._poqyd   ----     I._zjyhw 
##   F._djztolsb    ----     K._tiwfhler 
##   F._djztolsb    ----     E._dlmov 
##   I._infumtpg    ----     G._tbvmo 
##   I._ymzacesf    ----     D._qonmgjx 
##   I._ymzacesf    ----     C._zpekl 
##   K._guxhip  ----     W._xgapntmd 
##   K._guxhip  ----     B._qtkl 
##   K._hswluo  ----     B._galucwd 
##   K._ogeszq  ----     C._zpekl 
##   L._pgayvzc     ----     Q._eocwr 
##   L._pgayvzc     ----     K._tiwfhler 
##   M._vksyp   ----     G._tbvmo 
##   M._vksyp   ----     D._pqfdcnsx 
##   M._vksyp   ----     S._stcn 
##   N._panljtox    ----     W._xgapntmd 
##   N._panljtox    ----     O._ytjn 
##   Q._bchviyf     ----     D._pqfdcnsx 
##   Q._bchviyf     ----     S._stcn 
##   Q._bchviyf     ----     A._ahodzte 
##   Q._iqzp    ----     O._ytjn 
##   S._oyqfmig     ----     B._qtkl 
##   S._oyqfmig     ----     A._ahodzte 
##   S._oyqfmig     ----     E._helanvgx 
##   Y._itpzkvc     ----     I._zjyhw 
##   Y._itpzkvc     ----     D._qonmgjx 
##   Y._vnlf    ----     D._jlrpfati 
##   Z._szgp    ----     S._stcn 
##   Z._szgp    ----     B._fvakurtm 
##   Z._szgp    ----     B._fvakurtm
plot(obj,fsize=c(1,0.8)) ## basic method

plot of chunk unnamed-chunk-1

Now, let's change the linking line width by some criterion - say, the number of links from each tip for the tree on the left:

lwd<-summary(as.factor(obj$assoc[,1]))
lwd
##     D._hgpq D._xhwpobav   E._chlauw    E._poqyd F._djztolsb I._infumtpg 
##           3           2           2           4           2           1 
## I._ymzacesf   K._guxhip   K._hswluo   K._ogeszq  L._pgayvzc    M._vksyp 
##           2           2           1           1           2           3 
## N._panljtox  Q._bchviyf     Q._iqzp  S._oyqfmig  Y._itpzkvc     Y._vnlf 
##           2           3           1           3           2           1 
##     Z._szgp 
##           3
lwd<-lwd[obj$assoc[,1]]
lwd
##     D._hgpq     D._hgpq     D._hgpq D._xhwpobav D._xhwpobav   E._chlauw 
##           3           3           3           2           2           2 
##   E._chlauw    E._poqyd    E._poqyd    E._poqyd    E._poqyd F._djztolsb 
##           2           4           4           4           4           2 
## F._djztolsb I._infumtpg I._ymzacesf I._ymzacesf   K._guxhip   K._guxhip 
##           2           1           2           2           2           2 
##   K._hswluo   K._ogeszq  L._pgayvzc  L._pgayvzc    M._vksyp    M._vksyp 
##           1           1           2           2           3           3 
##    M._vksyp N._panljtox N._panljtox  Q._bchviyf  Q._bchviyf  Q._bchviyf 
##           3           2           2           3           3           3 
##     Q._iqzp  S._oyqfmig  S._oyqfmig  S._oyqfmig  Y._itpzkvc  Y._itpzkvc 
##           1           3           3           3           2           2 
##     Y._vnlf     Z._szgp     Z._szgp     Z._szgp 
##           1           3           3           3
plot(obj,fsize=c(1,0.8),link.lwd=lwd,link.lty="solid")

plot of chunk unnamed-chunk-2

We can do the line colors by the same criterion:

col<-heat.colors(n=max(lwd))[max(lwd)-lwd+1]
plot(obj,fsize=c(1,0.8),link.lwd=lwd,
    link.lty="solid",link.col=col)

plot of chunk unnamed-chunk-3

Or, using link.type="curved", here I'll also make the linking lines semi-transparent:

par(lend=1) ## change to square lend
plot(obj,fsize=c(1,0.8),link.lwd=lwd,link.lty="solid",
    link.col=sapply(col,make.transparent,0.6),
    link.type="curved")

plot of chunk unnamed-chunk-4

(although it gets a little messy for these random data).

This example was simulated as follows:

library(phytools)
t1<-rtree(n=20,tip.label=
    sapply(sample(LETTERS,20,replace=TRUE),
    function(x) 
    paste(x,paste(sample(letters,sample(4:8,1)),
    collapse=""),sep="._")))
t2<-rtree(n=40,tip.label=
    sapply(sample(LETTERS,40,replace=TRUE),
    function(x) 
    paste(x,paste(sample(letters,sample(4:8,1)),
    collapse=""),sep="._")))
assoc<-cbind(sample(t1$tip.label,40,replace=TRUE),
    sample(t2$tip.label,20,replace=TRUE))
ii<-order(assoc[,1])
assoc<-assoc[ii,]

No comments:

Post a Comment