How to get the average nLTT plot

Richel Bilderbeek

2016-09-14

Calculating the average nLTT plot of multiple phylogenies is not a trivial tasks. The function get_average_nltt does exactly this.

Examples

For all examples, the following R code must be run:

Example: Easy trees

Create two easy trees:

newick1 <- "((A:1,B:1):2,C:3);"
newick2 <- "((A:2,B:2):1,C:3);"
phylogeny1 <- ape::read.tree(text = newick1)
phylogeny2 <- ape::read.tree(text = newick2)

There are very similar. phylogeny1 has short tips:

ape::plot.phylo(phylogeny1)
ape::add.scale.bar() #nolint

This can be observed in the nLTT plot:

nLTT::nltt_plot(phylogeny1, ylim = c(0, 1))

As a collection of timepoints:

nLTT::get_phylogeny_nltt_matrix(phylogeny1)
##           time         N
## [1,] 0.0000000 0.3333333
## [2,] 0.6666667 0.6666667
## [3,] 1.0000000 1.0000000

phylogeny2 has longer tips:

ape::plot.phylo(phylogeny2)
ape::add.scale.bar() #nolint

Also this can be observed in the nLTT plot:

nLTT::nltt_plot(phylogeny2, ylim = c(0, 1))

As a collection of timepoints:

nLTT::get_phylogeny_nltt_matrix(phylogeny2)
##           time         N
## [1,] 0.0000000 0.3333333
## [2,] 0.3333333 0.6666667
## [3,] 1.0000000 1.0000000

The average nLTT plot should be somewhere in the middle.

It is constructed from stretched nLTT matrices.

Here is the nLTT matrix of the first phylogeny:

nLTT::stretch_nltt_matrix(
  nLTT::get_phylogeny_nltt_matrix(phylogeny1),
  dt = 0.20,
  step_type = "upper"
)
##      [,1]      [,2]
## [1,]  0.0 0.6666667
## [2,]  0.2 0.6666667
## [3,]  0.4 0.6666667
## [4,]  0.6 0.6666667
## [5,]  0.8 1.0000000
## [6,]  1.0 1.0000000

Here is the nLTT matrix of the second phylogeny:

nLTT::stretch_nltt_matrix(
  nLTT::get_phylogeny_nltt_matrix(phylogeny2),
  dt = 0.20,
  step_type = "upper"
)
##      [,1]      [,2]
## [1,]  0.0 0.6666667
## [2,]  0.2 0.6666667
## [3,]  0.4 1.0000000
## [4,]  0.6 1.0000000
## [5,]  0.8 1.0000000
## [6,]  1.0 1.0000000

Here is the average nLTT matrix of both phylogenies:

nLTT::get_average_nltt_matrix(c(phylogeny1, phylogeny2), dt = 0.20)
##      [,1]      [,2]
## [1,]  0.0 0.6666667
## [2,]  0.2 0.6666667
## [3,]  0.4 0.8333333
## [4,]  0.6 0.8333333
## [5,]  0.8 1.0000000
## [6,]  1.0 1.0000000

Observe how the numbers get averaged.

The same, now shown as a plot:

nLTT::get_average_nltt(c(phylogeny1, phylogeny2), dt = 0.20, plot_nltts = TRUE)

Example: Harder trees

Create two easy trees:

newick1 <- "((A:1,B:1):1,(C:1,D:1):1);"
newick2 <- paste0("((((XD:1,ZD:1):1,CE:2):1,(FE:2,EE:2):1):4,((AE:1,BE:1):1,",
  "(WD:1,YD:1):1):5);"
)
phylogeny1 <- ape::read.tree(text = newick1)
phylogeny2 <- ape::read.tree(text = newick2)

There are different. phylogeny1 is relatively simple, with two branching events happening at the same time:

ape::plot.phylo(phylogeny1)
ape::add.scale.bar() #nolint

This can be observed in the nLTT plot:

nLTT::nltt_plot(phylogeny1, ylim = c(0, 1))

As a collection of timepoints:

nLTT::get_phylogeny_nltt_matrix(phylogeny2)
##            time         N
##  [1,] 0.0000000 0.1111111
##  [2,] 0.5714286 0.2222222
##  [3,] 0.7142857 0.3333333
##  [4,] 0.7142857 0.4444444
##  [5,] 0.7142857 0.5555556
##  [6,] 0.8571429 0.6666667
##  [7,] 0.8571429 0.7777778
##  [8,] 0.8571429 0.8888889
##  [9,] 1.0000000 1.0000000

phylogeny2 is more elaborate:

ape::plot.phylo(phylogeny2)
ape::add.scale.bar() #nolint

Also this can be observed in the nLTT plot:

nLTT::nltt_plot(phylogeny2, ylim = c(0, 1))

As a collection of timepoints:

nLTT::get_phylogeny_nltt_matrix(phylogeny2)
##            time         N
##  [1,] 0.0000000 0.1111111
##  [2,] 0.5714286 0.2222222
##  [3,] 0.7142857 0.3333333
##  [4,] 0.7142857 0.4444444
##  [5,] 0.7142857 0.5555556
##  [6,] 0.8571429 0.6666667
##  [7,] 0.8571429 0.7777778
##  [8,] 0.8571429 0.8888889
##  [9,] 1.0000000 1.0000000

The average nLTT plot should be somewhere in the middle.

It is constructed from stretched nLTT matrices.

Here is the nLTT matrix of the first phylogeny:

nLTT::stretch_nltt_matrix(
  nLTT::get_phylogeny_nltt_matrix(phylogeny1),
  dt = 0.20,
  step_type = "upper"
)
##      [,1] [,2]
## [1,]  0.0  0.5
## [2,]  0.2  0.5
## [3,]  0.4  0.5
## [4,]  0.6  1.0
## [5,]  0.8  1.0
## [6,]  1.0  1.0

Here is the nLTT matrix of the second phylogeny:

nLTT::stretch_nltt_matrix(
  nLTT::get_phylogeny_nltt_matrix(phylogeny2),
  dt = 0.20,
  step_type = "upper"
)
##      [,1]      [,2]
## [1,]  0.0 0.2222222
## [2,]  0.2 0.2222222
## [3,]  0.4 0.2222222
## [4,]  0.6 0.3333333
## [5,]  0.8 0.6666667
## [6,]  1.0 1.0000000

Here is the average nLTT matrix of both phylogenies:

nLTT::get_average_nltt_matrix(c(phylogeny1, phylogeny2), dt = 0.20)
##      [,1]      [,2]
## [1,]  0.0 0.3611111
## [2,]  0.2 0.3611111
## [3,]  0.4 0.3611111
## [4,]  0.6 0.6666667
## [5,]  0.8 0.8333333
## [6,]  1.0 1.0000000

Observe how the numbers get averaged.

The same, now shown as a plot:

nLTT::get_average_nltt(
  c(phylogeny1, phylogeny2),
  dt = 0.20,
  plot_nltts = TRUE
)