Permalink
Browse files

edge.col for plot.cophylo

  • Loading branch information...
liamrevell committed Apr 24, 2017
1 parent 29219bc commit ca832e71283e644517352dfed944b91b4731b16f
Showing with 25 additions and 5 deletions.
  1. +24 −4 R/cophylo.R
  2. +1 −1 man/cophylo.Rd
View
@@ -114,6 +114,8 @@ cophylo<-function(tr1,tr2,assoc=NULL,rotate=TRUE,...){
phylogram<-function(tree,part=1,direction="rightwards",fsize=1,ftype="i",lwd=1,...){
if(hasArg(pts)) pts<-list(...)$pts
else pts<-TRUE
if(hasArg(edge.col)) edge.col<-list(...)$edge.col
else edge.col<-rep("black",nrow(tree$edge))
d<-if(direction=="rightwards") 1 else -1
## sub "_" for " "
tree$tip.label<-gsub("_"," ",tree$tip.label)
@@ -140,10 +142,22 @@ phylogram<-function(tree,part=1,direction="rightwards",fsize=1,ftype="i",lwd=1,.
## compute start & end points of each edge
X<-nodeHeights(cw)-0.5
## plot horizontal edges
for(i in 1:nrow(X)) lines(d*X[i,],rep(y[cw$edge[i,2]],2),lwd=lwd,lend=2)
for(i in 1:nrow(X)) lines(d*X[i,],rep(y[cw$edge[i,2]],2),lwd=lwd,lend=2,
col=edge.col[i])
## plot vertical relationships
for(i in 1:tree$Nnode+n) lines(d*X[which(cw$edge[,1]==i),1],
sort(y[cw$edge[which(cw$edge[,1]==i),2]]),lwd=lwd,lend=2)
for(i in 1:tree$Nnode+n){
ee<-which(cw$edge[,1]==i)
p<-if(i%in%cw$edge[,2]) which(cw$edge[,2]==i) else NULL
if(!is.null(p)){
xx<-c(X[ee,1],X[p,2])
yy<-sort(c(y[cw$edge[ee,2]],y[cw$edge[p,2]]))
} else {
xx<-c(X[ee,1],X[ee[1],1])
yy<-sort(c(y[cw$edge[ee,2]],mean(y[cw$edge[ee,2]])))
}
segments(x0=d*xx[1:(length(xx)-1)],y0=yy[1:(length(yy)-1)],
x1=d*xx[2:length(xx)],y1=yy[2:length(yy)],lwd=lwd,lend=2,col=edge.col[ee])
}
## plot links to tips
h<-max(X)+0.1*(max(X)-min(X))+max(fsize*strwidth(tree$tip.label))-
fsize*strwidth(tree$tip.label)
@@ -195,7 +209,7 @@ plot.multiCophylo<-function(x,...){
}
## plot an object of class "cophylo"
## written by Liam J. Revell 2015, 2016
## written by Liam J. Revell 2015, 2016, 2017
plot.cophylo<-function(x,...){
plot.new()
if(hasArg(mar)) mar<-list(...)$mar
@@ -214,10 +228,16 @@ plot.cophylo<-function(x,...){
else link.col<-"black"
if(hasArg(link.lty)) link.lty<-list(...)$link.lty
else link.lty<-"dashed"
if(hasArg(edge.col)) edge.col<-list(...)$edge.col
else edge.col<-list(
left=rep("black",nrow(x$trees[[1]]$edge)),
right=rep("blue",nrow(x$trees[[1]]$edge)))
obj<-list(...)
par(mar=mar)
plot.window(xlim=xlim,ylim=ylim)
leftArgs<-rightArgs<-obj
leftArgs$edge.col<-edge.col$left
rightArgs$edge.col<-edge.col$right
if(!is.null(obj$fsize)){
if(length(obj$fsize)>1){
leftArgs$fsize<-obj$fsize[1]
View
@@ -12,7 +12,7 @@ cophylo(tr1, tr2, assoc=NULL, rotate=TRUE, ...)
\item{assoc}{matrix containing the tip labels in \code{tr1} to match to the tip labels in \code{tr2}. Note that not all labels in either tree need to be included; and, furthermore, one label in \code{tr1} can be matched with more than one label in \code{tr2}, or vice versa.}
\item{rotate}{logical argument indicating whether nodes on both trees should be rotated to attempt to match in vertical position.}
\item{x}{in the case of \code{plot.cophylo}, an object of class \code{"cophylo"} to be plotted.}
\item{...}{optional arguments to be passed to \code{\link{tipRotate}}, or, in the case of \code{plot.cophylo}, to the internally used tree plotting function, \code{phylogram}. \code{phylogram} takes similar arguments to \code{\link{plotSimmap}}, such as \code{fsize}, \code{ftype}, \code{lwd}, and \code{pts}, though not all options from \code{plotSimmap} and \code{plotTree} are available. If \code{fsize} is supplied as a vector, different size fonts for the left & right facing trees may be used. In addition, the optional argument \code{scale.bar}, which should be a vector containing the lengths of the scale bars desired for the right & left trees, will add scale bars to the plot when supplied to \code{plot.cophylo}. If either tree contains polytomies, the \code{cophylo} argument \code{rotate.multi} should be set to \code{TRUE}. If curved linking lines are desired, the \code{plot.cophylo} argument \code{link.type} should be set to \code{"curved"}. Other arguments for the \code{plot} method include \code{link.col}, \code{link.lty}, and \code{link.lty}, which can be supplied as a scalar or a vector in which the order of the elements corresponds to the order of the associations in \code{assoc}.}
\item{...}{optional arguments to be passed to \code{\link{tipRotate}}, or, in the case of \code{plot.cophylo}, to the internally used tree plotting function, \code{phylogram}. \code{phylogram} takes similar arguments to \code{\link{plotSimmap}}, such as \code{fsize}, \code{ftype}, \code{lwd}, and \code{pts}, though not all options from \code{plotSimmap} and \code{plotTree} are available. If \code{fsize} is supplied as a vector, different size fonts for the left & right facing trees may be used. In addition, the optional argument \code{scale.bar}, which should be a vector containing the lengths of the scale bars desired for the right & left trees, will add scale bars to the plot when supplied to \code{plot.cophylo}. If either tree contains polytomies, the \code{cophylo} argument \code{rotate.multi} should be set to \code{TRUE}. If curved linking lines are desired, the \code{plot.cophylo} argument \code{link.type} should be set to \code{"curved"}. Other arguments for the \code{plot} method include \code{link.col}, \code{link.lty}, and \code{link.lty}, which can be supplied as a scalar or a vector in which the order of the elements corresponds to the order of the associations in \code{assoc}. Finally, \code{edge.col}, a list consisting of two vectors (\code{left} and \code{right}) can be used to specify the edge colors of the two left & right plotted trees. Note that the edge order is the same as in the \emph{rotated} trees, assuming that a rotation has been performed on \code{x}.}
}
\description{
This function creates an object of class \code{"cophylo"} or, in the case of \code{plot.cophylo}, plots that object. The function can (optionally) first attempt to rotate the nodes of both trees to optimize vertical matching of tips.

0 comments on commit ca832e7

Please sign in to comment.