-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathggtree.Rmd
58 lines (44 loc) · 1.99 KB
/
ggtree.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
---
title: "ggplot2 extensions: ggtree"
---
### ggtree
<https://bioconductor.org/packages/release/bioc/html/ggtree.html>
gtree is designed for visualizing phylogenetic tree and different types of associated annotation data.
```{r, message=FALSE,warning=FALSE}
# Example from https://bioconductor.org/packages/release/bioc/html/ggtree.html
library(ggplot2)
library(ape)
library(ggtree)
```
#### Tree Data Import
```{r}
file <- system.file("extdata/BEAST", "beast_mcc.tree", package="ggtree")
beast <- read.beast(file)
```
Users can use `ggtree(beast)` to visualize the tree and add layer to annotate it.
```{r warning=FALSE, fig.width=10, fig.height=10}
ggtree(beast, ndigits=2, branch.length = 'none') + geom_text(aes(x=branch, label=length_0.95_HPD), vjust=-.5, color='firebrick')
```
#### Tree Visualization
To view a phylogenetic tree, we first need to parse the tree file into `R`. The `ggtree` package supports many file format including output files of commonly used software packages in evolutionary biology. For more details, plase refer to the [Tree Data Import](treeImport.html) vignette.
```{r}
library("ggtree")
nwk <- system.file("extdata", "sample.nwk", package="ggtree")
tree <- read.tree(nwk)
ggplot(tree, aes(x, y)) + geom_tree() + theme_tree()
```
#### Tree Manipulation
###### Internal node number
Some of the functions in `ggtree` works with clade and accepts a parameter of internal node number. To get the internal node number, user can use `geom_text2` to display it:
```{r}
nwk <- system.file("extdata", "sample.nwk", package="ggtree")
tree <- read.tree(nwk)
ggtree(tree) + geom_text2(aes(subset=!isTip, label=node), hjust=-.3) + geom_tiplab()
```
The following example use `groupOTU` to display taxa classification.
```{r fig.width=14, fig.height=14}
data(chiroptera)
groupInfo <- split(chiroptera$tip.label, gsub("_\\w+", "", chiroptera$tip.label))
chiroptera <- groupOTU(chiroptera, groupInfo)
ggtree(chiroptera, aes(color=group), layout='circular') + geom_tiplab(size=1, aes(angle=angle))
```