/
phylogenies.Rmd
92 lines (83 loc) · 5 KB
/
phylogenies.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: "Adding geological timescales to phylogenies"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Adding geological timescales to phylogenies}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, fig.width = 7, fig.height = 5, fig.align = "center")
```
<div style="text-align: justify">
`coord_geo()` is also designed to work smoothly with phylogenies that are plotted with the [ggtree](https://www.bioconductor.org/packages/ggtree) package. First let's load some important packages.
```{r message = FALSE, warning = FALSE}
# Load deeptime
library(deeptime)
# Load other packages
library(ggplot2)
library(dplyr)
# Load ggtree
library(ggtree)
# Load phytools for some example data
library(phytools)
data(mammal.tree)
# Load paleotree for some example fossil data
library(paleotree)
data(RaiaCopesRule)
```
## Timescales and phylogenies
Now, let's plot some phylogenies with timescales! Note that by default the time axis increases toward the tips for trees plotted with `ggtree()`, so you must use the `revts()` function from [ggtree](https://www.bioconductor.org/packages/ggtree) to change the x-axis values to increasing negative values. Note that once you've done this, you will need to set `neg = TRUE` in `coord_geo()`. Furthermore, any time axis limits that you set will need to be negative. Here we also use the `scale_x_continuous()` function to relabel the negative x-axis values with positive labels.
```{r}
p <- ggtree(mammal.tree) +
coord_geo(xlim = c(-75, 0), ylim = c(-2, Ntip(mammal.tree)), neg = TRUE, abbrv = FALSE) +
scale_x_continuous(breaks = seq(-80, 0, 20), labels = abs(seq(-80, 0, 20))) +
theme_tree2()
revts(p)
```
### Phylogenies with only fossil taxa
Many phylogenies only have fossil taxa in them (i.e., non-ultrametric). These can be handled by `coord_geo()` by using `position_nudge()` function. In most cases, you will want to nudge the time axis values (in this case, the x-axis) by the `root.time` of your phylogeny. Also, note that we have modified `plot.margin` here to accommodate the stacked timescale.
```{r}
ggtree(ceratopsianTreeRaia, position = position_nudge(x = -ceratopsianTreeRaia$root.time)) +
coord_geo(
xlim = c(-163.5, -66), ylim = c(-2, Ntip(ceratopsianTreeRaia)),
pos = list("bottom", "bottom"), skip = c("Paleocene", "Middle Jurassic"),
dat = list("epochs", "periods"), abbrv = FALSE,
size = list(4, 5), neg = TRUE, center_end_labels = TRUE
) +
scale_x_continuous(breaks = -rev(epochs$max_age), labels = rev(epochs$max_age)) +
theme_tree2() +
theme(plot.margin = margin(7, 11, 7, 11))
```
## Circular phylogenies
Phylogenies can be plotted in a circular layout with a timescale background using the `coord_geo_radial()` function. It works just like `coord_geo()` and has many of the same arguments. You can use the `expand` argument in `scale_x_continuous()` to add space at either end of the timescale. You can use the `expand` argument in `scale_y_continuous()` to add space to either side of the phylogeny to prevent the tips from overlapping with the `pie` edges. Note that `coord_geo_radial()` only works with ggplot2 version 3.5.0 and higher. For other versions of ggplot2, you can use `coord_geo_polar()`.
```{r eval = FALSE}
revts(ggtree(mammal.tree)) +
coord_geo_radial(dat = "stages") +
scale_x_continuous(breaks = seq(-60, 0, 20), labels = abs(seq(-60, 0, 20)),
expand = expansion(mult = c(0.05, 0))) +
scale_y_continuous(guide = NULL, expand = expansion(mult = c(0.01, 0.01))) +
theme_classic()
```
<div class="figure">
<img src="phylogenies-1.png" alt="plot of phylogeny with a timescale in the background" width="100%" />
</div>
### Circular phylogenies with "stacked" timescales
Timescales can even be "stacked" like with `coord_geo()`, although the stacking occurs in a circular fashion. We also use the `prop`, `start`, `end`, and `direction` arguments to specify how the different timescales are oriented and split within the polar space.
```{r eval = FALSE}
revts(ggtree(mammal.tree)) +
coord_geo_radial(
dat = list("stages", "periods"), alpha = .5, lty = "dashed",
prop = list(0.66, .34), start = 2 * pi, end = 1.75 * pi, direction = 1,
) +
scale_x_continuous(breaks = seq(-60, 0, 20), labels = abs(seq(-60, 0, 20)),
expand = expansion(mult = c(0.05, 0))) +
scale_y_continuous(guide = NULL, expand = expansion(mult = c(0.01, 0.01))) +
theme_classic()
```
<div class="figure">
<img src="phylogenies-2.png" alt="plot of phylogeny with stacked timescales in the background" width="100%" />
</div>
### Disclaimer
Note that `coord_geo_radial()` has been tested with the plotting of ultrametric and non-ultrametric phylogenies with `ggtree::ggtree()`, but it may have unexpected behavior when combined with other functions from [ggtree](https://www.bioconductor.org/packages/ggtree). Furthermore, `coord_geo_radial()` may be useful (albeit perhaps abstract) for plotting other types of data, but this has not been investigated.
</div>