This vignette demonstrates the use of *treespace* to compare a collection of trees. For this example we use trees inferred from 17 dengue virus serotype 4 sequences from Lanciotti et al. (1997). We include a sample of trees from BEAST (v1.8), as well as creating neighbour-joining (NJ) and maximum-likelihood (ML) trees.

Load the required packages:

Load BEAST trees:

We load a random sample of 500 of the trees (from the second half of the posterior) produced using BEAST v1.8 with xml file 4 from Drummond and Rambaut (2007). It uses the standard GTR + Gamma + I substitution model with uncorrelated lognormal-distributed relaxed molecular clock. Each tree has 17 tips.

For convenience in our initial analysis we will take a random sample of 200 of these trees; sample sizes can be increased later.

```
suppressWarnings(RNGversion("3.5.0"))
set.seed(123)
BEASTtrees <- DengueTrees[sample(1:length(DengueTrees),200)]
```

Load nucleotide sequences:

Create a neighbour-joining (NJ) tree using the Tamura and Nei (1993) model (see `?dist.dna`

for more information) and root it on the outgroup `"D4Thai63"`

:

```
makeTree <- function(x){
tree <- nj(dist.dna(x, model = "TN93"))
tree <- root(tree, resolve.root=TRUE, outgroup="D4Thai63")
tree
}
DnjRooted <- makeTree(DengueSeqs)
# Note, there is a (small) negative branch length.
# We set this to 0 to avoid warnings from the phangorn package later:
DnjRooted$edge.length[which(DnjRooted$edge.length < 0)] <- 0
plot(DnjRooted)
```

We use `boot.phylo`

to bootstrap the tree:

and we can plot the tree again, annotating it with the bootstrap clade support values:

We create a maximum-likelihood (ML) tree and root it as before:

```
Dfit.ini <- pml(DnjRooted, as.phyDat(DengueSeqs), k=4)
Dfit <- optim.pml(Dfit.ini, optNni=TRUE, optBf=TRUE,
optQ=TRUE, optGamma=TRUE, model="GTR")
```

`## Warning: I unrooted the tree`

View the ML tree:

Create bootstrap trees:

```
# bootstrap supports:
DMLboots <- bootstrap.pml(Dfit, optNni=TRUE)
# root:
DMLbootsrooted <- lapply(DMLboots, function(x) root(x, resolve.root=TRUE, outgroup="D4Thai63"))
class(DMLbootsrooted) <- "multiPhylo"
```

Plot the ML tree again, with bootstrap support values:

We now use the function `treespace`

to find and plot distances between all these trees:

```
# collect the trees into a single object of class multiPhylo:
DengueTrees <- c(BEASTtrees, Dnjboots$trees, DMLbootsrooted,
DnjRooted, DfitTreeRooted)
class(DengueTrees) <- "multiPhylo"
# add tree names:
names(DengueTrees)[1:200] <- paste0("BEAST",1:200)
names(DengueTrees)[201:300] <- paste0("NJ_boots",1:100)
names(DengueTrees)[301:400] <- paste0("ML_boots",1:100)
names(DengueTrees)[[401]] <- "NJ"
names(DengueTrees)[[402]] <- "ML"
# create vector corresponding to tree inference method:
Dtype <- c(rep("BEAST",200),rep("NJboots",100),rep("MLboots",100),"NJ","ML")
# use treespace to find and project the distances:
Dscape <- treespace(DengueTrees, nf=5)
```

The function `plotGrovesD3`

produces interactive d3 plots which enable zooming, moving, tooltip text and legend hovering. We now refine the plot with colour-blind friendly colours (selected using ColorBrewer2), bigger points, varying symbols and point opacity to demonstrate the NJ and ML trees, informative legend title and smaller legend width:

```
Dcols <- c("#1b9e77","#d95f02","#7570b3")
Dmethod <- c(rep("BEAST",200),rep("NJ",100),rep("ML",100),"NJ","ML")
Dbootstraps <- c(rep("replicates",400),"NJ","ML")
Dhighlight <- c(rep(1,400),2,2)
plotGrovesD3(Dscape$pco,
groups=Dmethod,
colors=Dcols,
col_lab="Tree type",
size_var=Dhighlight,
size_range = c(100,500),
size_lab="",
symbol_var=Dbootstraps,
symbol_lab="",
point_opacity=c(rep(0.4,400),1,1),
legend_width=80)
```

We can also add tree labels to the plot. Where these overlap, the user can use “drag and drop” to move them around for better visibility.

```
plotGrovesD3(Dscape$pco,
groups=Dmethod,
treeNames = names(DengueTrees), # add the tree names as labels
colors=Dcols,
col_lab="Tree type",
size_var=Dhighlight,
size_range = c(100,500),
size_lab="",
symbol_var=Dbootstraps,
symbol_lab="",
point_opacity=c(rep(0.4,400),1,1),
legend_width=80)
```

Alternatively, where labels are too cluttered, it may be preferable not to plot them but to make the tree names available as tooltip text instead:

```
plotGrovesD3(Dscape$pco,
groups=Dmethod,
tooltip_text = names(DengueTrees), # add the tree names as tooltip text
colors=Dcols,
col_lab="Tree type",
size_var=Dhighlight,
size_range = c(100,500),
size_lab="",
symbol_var=Dbootstraps,
symbol_lab="",
point_opacity=c(rep(0.4,400),1,1),
legend_width=80)
```

The scree plot is available as part of the `treespace`

output:

We can also view the plot in 3D:

From these plots we can see that *treespace* has identified variation in the trees according to the Kendall Colijn metric (\(\lambda=0\), ignoring branch lengths). The NJ and ML bootstrap trees have broadly similar topologies but are different from any of the BEAST trees. We can check whether any bootstrap trees have the same topology as either the NJ or ML tree, as follows:

```
## NJ_boots91 NJ
## 291 401
```

```
## ML
## 402
```

This shows that the NJ tree has the same topology as one NJ bootstrap tree and one ML bootstrap tree. The ML tree has the same topology as 15 ML bootstrap trees, but no NJ bootstrap trees.

We can compare pairs of trees using the `plotTreeDiff`

function to see exactly where their differences arise. Tips with identical ancestry in the two trees are coloured grey, whereas tips with differing ancestry are coloured peach-red, with the colour darkening according to the number of ancestral differences found at each tip. Since we are comparing the trees topologically (ignoring branch lengths, for the moment), we plot with constant branch lengths for clarity.

`## [1] 13.93`

We can adjust the plot settings to make the visualisation clearer: