Skip to content

Commit

Permalink
label methods for cophylo plot
Browse files Browse the repository at this point in the history
  • Loading branch information
liamrevell committed Oct 23, 2015
1 parent d9970c0 commit 43ba09b
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions R/cophylo.R
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ cophylo<-function(tr1,tr2,assoc=NULL,rotate=TRUE,...){


## called internally by plot.cophylo to plot a phylogram ## called internally by plot.cophylo to plot a phylogram
## written by Liam J. Revell ## written by Liam J. Revell
phylogram<-function(tree,part=1,direction="right",fsize=1,ftype="i",lwd=1,...){ phylogram<-function(tree,part=1,direction="rightwards",fsize=1,ftype="i",lwd=1,...){
d<-if(direction=="right") 1 else -1 d<-if(direction=="rightwards") 1 else -1
## check if edge lenths ## check if edge lenths
if(is.null(tree$edge.length)) tree<-compute.brlen(tree) if(is.null(tree$edge.length)) tree<-compute.brlen(tree)
## rescale tree so it fits in one half of the plot ## rescale tree so it fits in one half of the plot
Expand Down Expand Up @@ -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, sub("_"," ",tree$tip.label[i]), pos=if(d<0) 4 else 2,offset=0,
cex=fsize,font=font) 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 ## return rightmost or leftmost edge of tip labels
invisible(d*max(h+fsize*strwidth(tree$tip.label))) invisible(d*max(h+fsize*strwidth(tree$tip.label)))
} }
Expand Down Expand Up @@ -119,10 +127,13 @@ plot.cophylo<-function(x,...){
par(mar=mar) par(mar=mar)
plot.window(xlim=xlim,ylim=ylim) plot.window(xlim=xlim,ylim=ylim)
x1<-phylogram(x$trees[[1]],part=0.4,...) 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)) if(!is.null(x$assoc)) makelinks(x,c(x1,x2))
else cat("No associations provided.\n") else cat("No associations provided.\n")
if(any(scale.bar>0)) add.scalebar(x,scale.bar,fsize) 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 ## add scale bar
Expand Down Expand Up @@ -183,3 +194,27 @@ tipRotate<-function(tree,x,...){
attr(tree,"minRotate")<-min(oo,pp) attr(tree,"minRotate")<-min(oo,pp)
tree 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.