trait.plot {diversitree}R Documentation

Plot a Phylogeny and Traits

Description

Plot a phylogeny and label the tips with binary traits. This function is experimental, and may change soon. Currently it is limited to radial trees and binary traits.

Usage

trait.plot(tree, dat, cols, lab=names(cols), str=0:1, class=NULL,
           type="f", w=1/50, legend=length(cols) > 1, cex.lab=.5,
           font.lab=3, cex.legend=.75, margin=1/4,
           check=TRUE, quiet=FALSE)

Arguments

tree

Phylogenetic tree, in ape format.

dat

A data.frame of trait values. The row names must be the same names as the tree (tree$tip.label), and each column contains the states (0, 1, or NA). The column names must give the trait names.

cols

A list, each element of which is a vector of length 2 being the colours for state 0 and state 1 for each trait. The list must be named so that all names appear in names(dat). Traits will be plotted in the order given by cols.

lab

Alternative names for the legend (perhaps longer or more informative). Must be in the same order as cols.

.

str

Strings used for the states in the legend.

class

A vector along phy$tip.label giving a higher level classification (e.g., genus or family). No checking is done to ensure that such classifications are not polyphyletic.

type

Plot type (same as type in ?plot.phylo). Currently only f (fan) is implemented.

w

Width of the trait plot, as a fraction of the tree depth.

legend

Logical: should a legend be plotted?

cex.lab, font.lab

Font size and type for the tip labels.

cex.legend

Font size for the legend.

margin

How much space, relative to the total tree depth, should be reserved when plotting a higher level classification.

check

When TRUE (by default), this will check that the classes specified by class are monophyletic. If not, classes will be concatenated and a warning raised.

quiet

When TRUE (FALSE by default), this suppresses the warning caused by check=TRUE.

Author(s)

Richard G. FitzJohn

Examples

These are the parameters: they are a single speciation and extinction rate, then 0->1 (trait A), 1->0 (A), 0->1 (B) and 1->0 (B).

colnames(musse.multitrait.translate(2, depth = 0))
## [1] "lambda0" "mu0"     "qA01.0"  "qA10.0"  "qB01.0"  "qB10.0" 

Simulate a tree where trait A changes slowly and B changes rapidly.

set.seed(1)
phy <- tree.musse.multitrait(c(0.1, 0, 0.01, 0.01, 0.05, 0.05), n.trait = 2, 
    depth = 0, max.taxa = 100, x0 = c(0, 0))

Here is the matrix of tip states (each row is a species, each column is a trait).

head(phy$tip.state)
##     A B
## sp1 0 0
## sp2 0 1
## sp3 1 1
## sp4 0 1
## sp5 0 0
## sp6 0 0

trait.plot(phy, phy$tip.state, cols = list(A = c("pink", "red"), 
    B = c("lightblue", "blue")))
plot of chunk unnamed-chunk-3

plot of chunk unnamed-chunk-3


nodes <- c("nd5", "nd4", "nd7", "nd11", "nd10", "nd8")
descendants <- function(node, phy) {
    ans <- node <- match(node, phy$node.label) + length(phy$tip.label)
    repeat {
        node <- phy$edge[phy$edge[, 1] %in% node, 2]
        if (length(node) > 0) 
            ans <- c(ans, node) else break
    }

    ans <- unlist(ans)
    ans[ans <= length(phy$tip.label)]
}

grp <- lapply(nodes, descendants, phy)
class <- rep(NA, 100)
for (i in seq_along(grp)) class[grp[[i]]] <- paste("group", LETTERS[i])

Now, 'class' is a vector along phy$tip.label indicating which of six groups each species belongs.

Plotting the phylogeny with these groups:

trait.plot(phy, phy$tip.state, cols = list(A = c("pink", "red"), 
    B = c("lightblue", "blue")), class = class, font = 1, cex.lab = 1, cex.legend = 1)
## Warning message: 'group.label.tip.rad' is deprecated.
## Use 'group.label.tip' instead.
## See help("Deprecated")
plot of chunk unnamed-chunk-4

plot of chunk unnamed-chunk-4


[Package diversitree version 0.9-4 ]