Permalink
Browse files

label methods for cophylo plot

  • Loading branch information...
liamrevell committed Oct 23, 2015
1 parent d9970c0 commit 43ba09bd89fba3fc195363b142fb437c0c1bda23
Showing with 38 additions and 3 deletions.
  1. +38 −3 R/cophylo.R
View
@@ -44,8 +44,8 @@ cophylo<-function(tr1,tr2,assoc=NULL,rotate=TRUE,...){
## called internally by plot.cophylo to plot a phylogram
## written by Liam J. Revell
phylogram<-function(tree,part=1,direction="right",fsize=1,ftype="i",lwd=1,...){
d<-if(direction=="right") 1 else -1
phylogram<-function(tree,part=1,direction="rightwards",fsize=1,ftype="i",lwd=1,...){
d<-if(direction=="rightwards") 1 else -1
## check if edge lenths
if(is.null(tree$edge.length)) tree<-compute.brlen(tree)
## rescale tree so it fits in one half of the plot
@@ -87,6 +87,14 @@ phylogram<-function(tree,part=1,direction="right",fsize=1,ftype="i",lwd=1,...){
sub("_"," ",tree$tip.label[i]), pos=if(d<0) 4 else 2,offset=0,
cex=fsize,font=font)
}
PP<-list(type="phylogram",use.edge.length=TRUE,node.pos=1,
show.tip.label=if(ftype!="off") TRUE else FALSE,show.node.label=FALSE,
font=ftype,cex=fsize,adj=0,srt=0,no.margin=FALSE,label.offset=0,
x.lim=par()$usr[1:2],y.lim=par()$usr[3:4],
direction=direction,tip.color="black",Ntip=Ntip(cw),Nnode=cw$Nnode,
edge=cw$edge,xx=d*sapply(1:(Ntip(cw)+cw$Nnode),
function(x,y,z) y[match(x,z)],y=X,z=cw$edge),yy=y)
assign("last_plot.phylo",PP,envir=.PlotPhyloEnv)
## return rightmost or leftmost edge of tip labels
invisible(d*max(h+fsize*strwidth(tree$tip.label)))
}
@@ -119,10 +127,13 @@ plot.cophylo<-function(x,...){
par(mar=mar)
plot.window(xlim=xlim,ylim=ylim)
x1<-phylogram(x$trees[[1]],part=0.4,...)
x2<-phylogram(x$trees[[2]],part=0.4,direction="left",...)
left<-get("last_plot.phylo",envir=.PlotPhyloEnv)
x2<-phylogram(x$trees[[2]],part=0.4,direction="leftwards",...)
right<-get("last_plot.phylo",envir=.PlotPhyloEnv)
if(!is.null(x$assoc)) makelinks(x,c(x1,x2))
else cat("No associations provided.\n")
if(any(scale.bar>0)) add.scalebar(x,scale.bar,fsize)
assign("last_plot.cophylo",list(left=left,right=right),envir=.PlotPhyloEnv)
}
## add scale bar
@@ -183,3 +194,27 @@ tipRotate<-function(tree,x,...){
attr(tree,"minRotate")<-min(oo,pp)
tree
}
## labeling methods for plotted "cophylo" object
## written by Liam J. Revell 2015
nodelabels.cophylo<-function(...,which=c("left","right")){
obj<-get("last_plot.cophylo",envir=.PlotPhyloEnv)
if(which[1]=="left") assign("last_plot.phylo",obj[[1]],envir=.PlotPhyloEnv)
else if(which[1]=="right") assign("last_plot.phylo",obj[[2]],envir=.PlotPhyloEnv)
nodelabels(...)
}
edgelabels.cophylo<-function(...,which=c("left","right")){
obj<-get("last_plot.cophylo",envir=.PlotPhyloEnv)
if(which[1]=="left") assign("last_plot.phylo",obj[[1]],envir=.PlotPhyloEnv)
else if(which[1]=="right") assign("last_plot.phylo",obj[[2]],envir=.PlotPhyloEnv)
edgelabels(...)
}
tiplabels.cophylo<-function(...,which=c("left","right")){
obj<-get("last_plot.cophylo",envir=.PlotPhyloEnv)
if(which[1]=="left") assign("last_plot.phylo",obj[[1]],envir=.PlotPhyloEnv)
else if(which[1]=="right") assign("last_plot.phylo",obj[[2]],envir=.PlotPhyloEnv)
tiplabels(...)
}

0 comments on commit 43ba09b

Please sign in to comment.