-
Notifications
You must be signed in to change notification settings - Fork 50
/
ggtree-plotly.Rmd
45 lines (35 loc) · 1.68 KB
/
ggtree-plotly.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
## Convert a `ggtree` object to a `plotly` object {#plotly}
One way to make a quick interactive phylogenetic tree is using `r Biocpkg("ggtree")` with the `r CRANpkg("plotly")` package. The [ggplotly()](https://plotly-r.com/improving-ggplotly.html) is able to convert `ggtree` object to a `plotly` object. Note that the `r Biocpkg("ggtree")` package also supports interactive manipulation of the phylogenetic tree via the [identify()](#identify) method.
```r
# example from https://twitter.com/drandersgs/status/965996335882059776
# LOAD LIBS ---------------------------------------------------------------
library(ape)
library(ggtree)
library(plotly)
# CREATE A TREE -------------------------------------------------------------
n_samples <- 20
n_grp <- 4
tree <- ape::rtree(n = n_samples)
# CREATE SOME METADATA ----------------------------------------------------
id <- tree$tip.label
set.seed(42)
grp <- sample(LETTERS[1:n_grp], size = n_samples, replace = T)
dat <- tibble::tibble(id = id,
grp = grp)
# PLOT THE TREE -----------------------------------------------------------
p1 <- ggtree(tree)
metat <- p1$data %>%
dplyr::inner_join(dat, c('label' = 'id'))
p2 <- p1 +
geom_point(data = metat,
aes(x = x,
y = y,
colour = grp,
label = id))
plotly::ggplotly(p2)
```
(ref:ggtreeplotlyscap) Interactive phylogenetic tree by combining ggtree with plotly.
(ref:ggtreeplotlycap) **Interactive phylogenetic tree by combining ggtree with plotly.**
```{r ggtreeplotly, fig.cap="(ref:ggtreeplotlycap)", fig.scap="(ref:ggtreeplotlyscap)", echo=FALSE, out.width='100%'}
knitr::include_graphics("img/ggtree-plotly.gif")
```