forked from YuLab-SMU/treedata-book
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathA-app.Rmd
649 lines (447 loc) · 26.5 KB
/
A-app.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
# (APPENDIX) Appendix {-}
# Frequently asked questions {#faq}
## Installation {#installation}
`r Biocpkg("ggtree")` is released within the Bioconductor project, you need to use `r CRANpkg("BiocManager")` to install it.
```r
## you need to install BiocManager before using it
## install.packages("BiocManager")
library(BiocManager)
install("ggtree")
```
Bioconductor release is adhere to specific R version. Please make sure you are using latest version of R if you want to install the latest release of Bioconductor packages, including `ggtree`. Beware that bugs will only be fixed in current release and develop branches. If you find a bug, please follow the guide^[<https://guangchuangyu.github.io/2016/07/how-to-bug-author/>] to report it.
## Basic R related {#faq-r}
### Use your local file {#faq-local-file}
If you are new to `R` and want to use `ggtree` for tree visualization, please do
learn some basic `R` and `ggplot2`.
A very common issue is that users always copy-paste command without looking at
the function's behavior. `system.file()` was used in the `treeio` and `ggtree` package documentation to find files in the packages.
```
system.file package:base R Documentation
Find Names of R System Files
Description:
Finds the full file names of files in packages etc.
Usage:
system.file(..., package = "base", lib.loc = NULL,
mustWork = FALSE)
```
For users who want to use their own files, please just use relative or absolute file path (*e.g.* `f = "your/folder/filename"`).
## Aesthetic mapping {#faq-aes-mapping}
### Inherit _aes_ {#faq-inherit-aes}
```r
ggtree(rtree(30)) + geom_point()
```
For example, we can add symbolic points to nodes with `geom_point()` directly.
The magic here is we don't need to map `x` and `y` position of the points by providing `aes(x, y)` to `geom_point()` since it was already mapped by `ggtree` function and it serves as a global mapping for all layers.
But what if we provide a `dataset` in a layer and the `dataset` doesn't contain column of `x` and/or `y`,
the layer function also try to map `x` and `y` and also others if you map them in `ggtree` function.
As these variable is not available in your `dataset`, you will get the following error:
```
Error in eval(expr, envir, enclos) : object 'x' not found
```
This can be fixed by using parameter `inherit.aes=FALSE` which will disable inheriting mapping from `ggtree` function.
### Never use `$` in aes {#faq-dollar-aes}
NEVER DO THIS^[<https://groups.google.com/d/msg/bioc-ggtree/hViM6vRZF94/MsZT8qRgBwAJ>
and <https://github.com/GuangchuangYu/ggtree/issues/106>].
See the explaination in the [ggplot2 book 2ed](https://github.com/hadley/ggplot2-book/blob/master/layers.rmd#aesthetic-mappings-secaes):
>Never refer to a variable with `$` (e.g., `diamonds$carat`) in `aes()`. This breaks containment, so that the plot no longer contains everything it needs, and causes problems if ggplot2 changes the order of the rows, as it does when facetting.
## Text & Label {#faq-text}
### Tip label truncated {#faq-label-truncated}
`r CRANpkg("ggplot2")` can't auto adjust `xlim` based on added text^[<https://twitter.com/hadleywickham/status/600280284869697538>].
```r
library(ggtree)
## example tree from https://support.bioconductor.org/p/72398/
tree <- read.tree(text= paste("(Organism1.006G249400.1:0.03977,(Organism2.022118m:0.01337,",
"(Organism3.J34265.1:0.00284,Organism4.G02633.1:0.00468)0.51:0.0104):0.02469);"))
p <- ggtree(tree) + geom_tiplab()
```
In this example, the tip labels displayed on Figure \@ref(fig:truncatedTip)A are truncated. This is because the units are in two different spaces (data and pixel). Users can use `xlim` to allocate more spaces for tip labels (Figure \@ref(fig:truncatedTip)B).
```r
p + xlim(0, 0.08)
```
Another solution is to set `clip = "off"` to allow drawing outside of the plot panel. We may also need to set `plot.margin` to allocate more spaces for margin (Figure \@ref(fig:truncatedTip)C).
```r
p + coord_cartesian(clip = 'off') +
theme_tree2(plot.margin=margin(6, 120, 6, 6))
```
(ref:truncatedTipscap) Allocating more spaces for truncated tip lables.
(ref:truncatedTipcap) **Allocating more spaces for truncated tip lables.** Long tip lables may be truncated (A). One solution is to allocate more spaces for plot panel (B) and another solution is to allow plotting labels outside the plot panel (C).
```{r truncatedTip, fig.width=12, fig.height=4, echo=FALSE, fig.cap="(ref:truncatedTipcap)", fig.scap="(ref:truncatedTipscap)"}
library(ggplot2)
library(ggtree)
## example tree from https://support.bioconductor.org/p/72398/
tree<-read.tree(text="(Organism1.006G249400.1:0.03977,(Organism2.022118m:0.01337,(Organism3.J34265.1:0.00284,Organism4.G02633.1:0.00468)0.51:0.0104):0.02469);")
p <- ggtree(tree) + geom_tiplab()
p2 <- ggtree(tree) + geom_tiplab() + xlim(0, 0.08)
p3 <- p + coord_cartesian(clip = 'off') +
theme_tree2(plot.margin=margin(6, 120, 6, 6))
cowplot::plot_grid(p, p2, p3, ncol=3, labels=c("A", "B", "C"))
```
### Modify (tip) labels {#faq-modify-label}
If you want to modify tip labels of the tree, you can use `treeio::rename_taxa()` to rename a `phylo` or `treedata` object.
```{r renameTaxa}
tree <- read.tree(text = "((A, B), (C, D));")
d <- data.frame(label = LETTERS[1:4],
label2 = c("sunflower", "tree", "snail", "mushroom"))
## rename_taxa use 1st column as key and 2nd column as value by default
## rename_taxa(tree, d)
rename_taxa(tree, d, label, label2) %>% write.tree
```
If the input tree object is a `treedata` instance, you can use `write.beast()` to export the tree with with associated data to a BEAST compatible NEXUS file.
Renaming phylogeny tip labels seems not be a good idea, since it may introduce problems when mapping the original sequence alignment to the tree. Personally, I recommend to store the new labels as a tip annotation in `treedata` object.
```{r warnings = F}
tree2 <- full_join(tree, d, by = "label")
tree2
```
If you just want to show different or additional information when plotting the tree, you don't need to modify tip labels. This could be easily done via the `%<+%` operator to attach the modified version of the labels and than use `geom_tiplab` to display
the modified version (Figure \@ref(fig:renameTip)).
(ref:renameTipscap) Alternative tip labels.
(ref:renameTipcap) **Alternative tip labels.** Original tip lables (A) and modified version (B).
```{r renameTip, fig.width=8, fig.height=3, fig.cap="(ref:renameTipcap)", fig.scap="(ref:renameTipscap)"}
p <- ggtree(tree) + xlim(NA, 3)
p1 <- p + geom_tiplab()
## the following command will produce identical figure of p2
## ggtree(tree2) + geom_tiplab(aes(label = label2))
p2 <- p %<+% d + geom_tiplab(aes(label=label2))
cowplot::plot_grid(p1, p2, ncol=2, labels = c("A", "B"))
```
### Formatting (tip) labels {#faq-formatting-label}
If you want to format labels, you need to set `parse=TRUE` in `geom_text`/`geom_tiplab` and the `label` should be string that can be parsed into expression and displayed as described in `?plotmath`.
For example, the tip labels contains two parts, species name and accession number and we want to display species name in _italic_, we can use command like this to format specific tip/node label (Figure \@ref(fig:formatTip)A):
```{r formatTip-A, eval=F}
set.seed(2019-06-24)
tree <- rtree(30)
p1 <- ggtree(tree) +
geom_tiplab(aes(subset=node==35),
label='paste(italic("species name"),
" accession number")',
parse=T) + xlim(0, 6)
```
Another example for formating all tip labels is demonstrated in Figure \@ref(fig:formatTip)B:
```{r formatTip-B, eval=F}
p2 <- ggtree(tree) +
geom_tiplab(aes(label=paste0('bold(', label,
')~italic(', node, ')')),
parse=TRUE) + xlim(0, 5)
```
The `label` can be provided by a `data.frame` that contains related information
of the taxa (Figure \@ref(fig:formatTip)C).
```{r formatTip-C, eval=F}
tree <- read.tree(text = "((a,(b,c)),d);")
genus <- c("Gorilla", "Pan", "Homo", "Pongo")
species <- c("gorilla", "spp.", "sapiens", "pygmaeus")
geo <- c("Africa", "Africa", "World", "Asia")
d <- data.frame(label = tree$tip.label, genus = genus,
species = species, geo = geo)
p3 <- ggtree(tree) %<+% d + xlim(NA, 6) +
geom_tiplab(aes(label=paste0('italic(', genus,
')~bolditalic(', species, ')~', geo)),
parse=T)
cowplot::plot_grid(p1, p2, p3, ncol=3, labels = LETTERS[1:3])
```
(ref:formatTipscap) Formatting labels.
(ref:formatTipcap) **Formatting labels.** Formatting specific tip/node label (A), all tip labels (B & C).
```{r formatTip, fig.width=12, fig.height=4, echo = F, fig.cap="(ref:formatTipcap)", fig.scap="(ref:formatTipscap)", ref.label = c('formatTip-A', 'formatTip-B', 'formatTip-C'), echo=FALSE}
```
### Avoid overlapping text labels {#faq-ggrepel}
User can use [ggrepel](https://cran.r-project.org/web/packages/ggrepel/) package to repel overlapping text labels^[<https://cran.r-project.org/web/packages/ggrepel/vignettes/ggrepel.html>].
.
For example:
(ref:repelTipscap) Repel labels.
(ref:repelTipcap) **Repel labels.** Repel labels to avoid overlapping.
```{r repelTip, fig.width=12, fig.height=8, fig.cap="(ref:repelTipcap)", fig.scap="(ref:repelTipscap)"}
library(ggrepel)
library(ggtree)
raxml_file <- system.file("extdata/RAxML", "RAxML_bipartitionsBranchLabels.H3", package="treeio")
raxml <- read.raxml(raxml_file)
ggtree(raxml) + geom_label_repel(aes(label=bootstrap, fill=bootstrap)) +
theme(legend.position = c(.1, .8)) + scale_fill_viridis_c()
```
### Bootstrap values from newick format {#faq-bootstrap}
It's quite command to store `bootstrap` value as node label in `newick` format. Visualizing node label is easy using `geom_text2(aes(subset = !isTip, label=label))`.
If you want to only display a subset of `bootstrap` (e.g. bootstrap > 80), you can't simply using `geom_text2(subset= (label > 80), label=label)` since `label` is a character vector, which contains node label (bootstrap value) and tip label (taxa name). If we use `geom_text2(subset=(as.numeric(label) > 80), label=label)`, it will also fail since `NAs` were introduced by coercion. We need to convert `NAs` to logical `FALSE`, this can be done by the following code:
```r
nwk <- system.file("extdata/RAxML","RAxML_bipartitions.H3", package='ggtree')
tr <- read.tree(nwk)
ggtree(tr) + geom_text2(aes(label=label, subset = !is.na(as.numeric(label)) & as.numeric(label) > 80))
```
Another solution is converting the bootstrap value outside `ggtree`.
```r
q <- ggtree(tr)
d <- q$data
d <- d[!d$isTip,]
d$label <- as.numeric(d$label)
d <- d[d$label > 80,]
q + geom_text(data=d, aes(label=label))
```
## Different x labels for different facet panels
This is not supported by `r CRANpkg("ggplot2")` in general. However, we can just draw text labels for each panels and put the labels beyond the plot panels as demonstrated in Figure \@ref(fig:xlabFacets).
(ref:xlabFacetsscap) X-axis titles for different facet panels.
(ref:xlabFacetscap) **X-axis titles for different facet panels.**
```{r xlabFacets, fig.width=7, fig.height=8, fig.cap="(ref:xlabFacetscap)", fig.scap="(ref:xlabFacetsscap)"}
library(ggtree)
library(ggplot2)
set.seed(2019-05-02)
x <- rtree(30)
p <- ggtree(x) + geom_tiplab()
d <- data.frame(label = x$tip.label,
value = rnorm(30))
p2 <- facet_plot(p, panel = "Dot", data = d,
geom = geom_point, mapping = aes(x = value))
p2 <- p2 + theme_bw() +
xlim_tree(5) + xlim_expand(c(-5, 5), 'Dot')
d = data.frame(.panel = c('Tree', 'Dot'),
lab = c("Distance", "Dot Units"),
x=c(2.5,0), y=-2)
p2 + scale_y_continuous(limits=c(0, 31),
expand=c(0,0),
oob=function(x, ...) x) +
geom_text(aes(label=lab), data=d) +
coord_cartesian(clip='off') +
theme(plot.margin=margin(6, 6, 40, 6))
```
## Plot something behind the phylogeny {#faq-under-the-tree}
The `ggtree` function plot the tree structure and normally we add layers on top of the tree.
```{r tree_behind_box}
set.seed(1982)
x <- rtree(5)
p <- ggtree(x) + geom_hilight(7, alpha=1)
```
If we want the layers behind the tree layer, we can reverse the order of all the layers.
```r
p$layers <- rev(p$layers)
```
Another solution is to use `ggplot()` instead of `ggtree()` and `+ geom_tree()` to add the layer of tree structure at the correct position of layer stack.
```r
ggplot(x) + geom_hilight(7, alpha=1) + geom_tree() + theme_tree()
```
(ref:treeLayerOrderscap) Add layers behind tree structure.
(ref:treeLayerOrdercap) **Add layers behind tree structure.** A layer on top of the tree structure (A). Reverse layer order of A (B). Add layer behind the tree layer (C).
```{r treeLayerOrder, echo=F, fig.width=6, fig.height=3, fig.cap="(ref:treeLayerOrdercap)", fig.scap="(ref:treeLayerOrderscap)"}
g <- p
p$layers <- rev(p$layers)
cowplot::plot_grid(g, p,
ggplot(x) + geom_hilight(7, alpha=1) + geom_tree() + theme_tree(),
ncol = 3, labels = LETTERS[1:3])
```
## Enlarge center space in circular/fan layout tree {#faq-enlarge-center-space}
This question was asked several times^[<https://groups.google.com/d/msg/bioc-ggtree/gruC4FztU8I/mwavqWCXAQAJ>, <https://groups.google.com/d/msg/bioc-ggtree/UoGQekWHIvw/ZswUUZKSGwAJ> and <https://github.com/GuangchuangYu/ggtree/issues/95>], and a published example can be found in <https://www.ncbi.nlm.nih.gov/pubmed/27605062>. Increasing percentage of center white space in circular tree is useful to avoid overlapping tip labels and to increase readibility of the tree by moving all nodes and branches further out. This can be done simply by using `+xlim()` to allocate more space, just like in Figure \@ref(fig:layout2)G, or assign a long root branch that is similar to the "Root Length" parameter in FigTree.
(ref:innerspacescap) Enlarge center space in circular tree.
(ref:innerspacecap) **Enlarge center space in circular tree.** Allocate more space by `xlim` (A) or long root branch (B).
```{r circular-space, fig.width=8, fig.height=4, fig.cap="(ref:innerspacecap)", fig.scap="(ref:innerspacescap)"}
set.seed(1982)
tree <- rtree(30)
plot_grid(
ggtree(tree, layout='circular') + xlim(-10, NA),
ggtree(tree, layout='circular') + geom_rootedge(5),
labels = c("A", "B", ncol=2)
)
```
## Use the most distant tip from the root as the origin of the time scale
The `revts` will reverse the x-axis by setting the most recent tip to 0. We can use `scale_x_continuous(labels=abs)` to label x-axis using absolute values.
(ref:distantTipscap) Origin of the time scale.
(ref:distantTipcap) **Origin of the time scale.** Forward: from the root to tips (A). Backward: from the most distant tip to the root (B).
```{r distantTip, fig.cap="(ref:distantTipcap)", fig.scap="(ref:distantTipscap)", fig.width=6, fig.height=3}
tr <- rtree(10)
p <- ggtree(tr) + theme_tree2()
p2 <- revts(p) + scale_x_continuous(labels=abs)
plot_grid(p, p2, ncol=2, labels=c("A", "B"))
```
## Changing branch length of outgroup
When outgroups are on a very long branch length (Figure \@ref(fig:outgroupEdge)A), we would like to keep the out groups in the tree but ignore their branch lengths (Figure \@ref(fig:outgroupEdge)B)^[example from: <https://groups.google.com/d/msg/bioc-ggtree/T2ySvqv351g/mHsyljvBCwAJ>]. This can be easily done by modifying coordination of the out groups.
(ref:outgroupEdgescap) Changing branch length of outgroup.
(ref:outgroupEdgecap) **Changing branch length of outgroup.** Original tree (A) and reduced outgroup branch length version (B).
```{r outgroupEdge, fig.cap="(ref:outgroupEdgecap)", fig.scap="(ref:outgroupEdgescap)", fig.width=6, fig.height=5}
x <- read.tree("data/long-branch-example.newick")
m <- MRCA(x, 75, 76)
y <- groupClade(x, m)
p <- p1 <- ggtree(y, aes(linetype = group)) +
geom_tiplab(size = 2) +
theme(legend.position = 'none')
p$data[p$data$node %in% c(75, 76), "x"] <- mean(p$data$x)
plot_grid(p1, p, ncol=2)
```
## Edit tree graphic details {#export-edit}
It can be hard to modify plot details for ordinary users using `r CRANpkg("ggplot2")`/`r Biocpkg("ggtree")`. We recommend using the `r CRANpkg("export")` package to export `r Biocpkg("ggtree")` output to 'Microsoft Office' Document and edit the tree graphic in 'PowerPoint'.
# Tips for using `ggtree` with `ggimage` {#ggimage-tips}
`r Biocpkg("ggtree")` supports annotating tree with silhouette images via the `r CRANpkg("ggimage")` package. `r CRANpkg("ggimage")` provides grammar of graphic syntax to work with image files. It allows processing images on the fly via the `image_fun` parameter, which accepts a function to process `magick-image` object. The `r CRANpkg("magick")` package provides several functions and these functions can be combined to perform a particular task.
## Example 1: Remove background of images {#ggimage-rm-image-bg}
(ref:ggimagebgscap) Remove image background.
(ref:ggimagebgcap) **Remove image background.** Plotting silhouette images on phylogenetic tree without (A) and with (B) background remove.
```{r ggimagebg, fig.width=8, fig.height=4, fig.cap="(ref:ggimagebgcap)", fig.scap="(ref:ggimagebgscap)"}
set.seed(1982)
x <- rtree(5)
p <- ggtree(x) + theme_grey()
p1 <- p + geom_nodelab(image="img/frogs/frog.jpg", geom="image", size=.12) +
ggtitle("original image")
p2 <- p + geom_nodelab(image="img/frogs/frog.jpg", geom="image", size=.12,
image_fun= function(.) magick::image_transparent(., "white")) +
ggtitle("image with background removed")
plot_grid(p1, p2, ncol=2)
```
## Example 2: Plot tree on a background image {#ggimage-bgimage}
The `geom_bgimage` add a layer of the image and put the layer to the bottom of the layer stack. It is a normal layer and doesn't change the structure of the output `ggtree` object. Users can add annotation layers as without the background image layer.
(ref:bgimagescap) Use image file as tree background.
(ref:bgimagecap) **Use image file as tree background.**
```{r bgimage, fig.width=6, fig.height=4, fig.cap="(ref:bgimagecap)", fig.scap="(ref:bgimagescap)"}
ggtree(rtree(20), size=1.5, color="white") +
geom_bgimage('img/blackboard.jpg') +
geom_tiplab(color="white", size=5, family='xkcd')
```
# Comic (xkcd-like) phylogenetic tree {#commicR}
```{r ggsvg, fig.show='hide'}
library(htmltools)
library(XML)
library(gridSVG)
library(ggplot2)
library(ggtree)
library(comicR)
p <- ggtree(rtree(30), layout="circular") +
geom_tiplab(aes(label=label), color="purple")
print(p)
svg <- grid.export(name="", res=100)$svg
```
(ref:comicRscap) Remove image background.
(ref:comicRcap) **Remove image background.** Plotting silhouette images on phylogenetic tree without (A) and with (B) background remove.
```{r comicR}
tagList(
tags$div(
id = "ggtree_comic",
tags$style("#ggtree_comic text {font-family:Chalkduster;}"),
HTML(saveXML(svg)),
comicR("#ggtree_comic", ff=5)
)
) # %>% html_print
```
# Print ASCII-art rooted tree {#ascii-tree}
```{r asciiTree, comment=NA}
library(data.tree)
tree <- rtree(10)
d <- as.data.frame(as.Node(tree))
names(d) <- NULL
print(d, row.names=FALSE)
```
It is neat to print ASCII-art of phylogeny. Sometimes we don't want to plot the tree, but just take a glance at the tree structure without leaving the focus from R console. However, it is not a good idea to print the whole tree as ASCII text if the tree is large. Sometimes we just want to look at a specific portion of the tree and their immediate relatives. At this scenario, we can use `treeio::tree_subset()` function (see [session 2.4](subsetting-tree-with-data)) to extract selected portion of a tree. Then we can print ASCII-art of the tree subset to explore evolutionary relationship of interested species in R console.
`r Biocpkg("ggtree")` supports parsing tip labels as emoji to create [phylomoji](#phylomoji). With the `r CRANpkg("data.tree")` and `r CRANpkg("emojifont")` packages, we can also print phylomoji as ASCII text.
```{r asciiTreeEmoji, comment=NA}
library(emojifont)
tt <- '((snail,mushroom),(((sunflower,evergreen_tree),leaves),green_salad));'
tree <- read.tree(text = tt)
tree$tip.label <- emoji(tree$tip.label)
d <- as.data.frame(as.Node(tree))
names(d) <- NULL
print(d, row.names=FALSE)
```
# Zoom in selected portion {#facet-zoom}
(ref:facetZoomscap) Zoom in selected clade.
(ref:facetZoomcap) **Zoom in selected clade.**
```{r facetZoom, fig.width=6, fig.height=4, fig.cap="(ref:facetZoomcap)", fig.scap="(ref:facetZoomscap)"}
set.seed(2019-08-05)
x <- rtree(30)
nn <- tidytree::offspring(x, 43, self_include=TRUE)
ggtree(x) + ggforce::facet_zoom(xy = node %in% nn)
```
# Run ggtree in Jupyter notebook
If you have [Jupyter notebook](https://jupyter.org/) installed on your system, you can install [IRkernel](https://irkernel.github.io/) with the following command in R:
```r
install.packages("IRkernel")
IRkernel::installspec()
```
Then you can use ggtree and other R packages in Jupyter notebook. Here is a screenshot of recreating Figure \@ref(fig:phylomoji1) in Jupyter notebook.
(ref:jupyterscap) ggtree in Jupyter notebook.
(ref:jupytercap) **ggtree in Jupyter notebook.** Running ggtree in Jupyter notebook via R kernel.
```{r jupyter, echo=F, fig.cap="(ref:jupytercap)", fig.scap="(ref:jupyterscap)"}
## htmltools::includeHTML("img/ggtree_jupyter.html")
knitr::include_graphics("img/Screenshot_2019-06-24_ggtree-jupyter.png")
```
# Figures and Tables
```{r facet-geom, echo=FALSE,results='asis'}
x <- "ggalt\tgeom_dumbbell\tcreates dumbbell charts\n
ggbio\tgeom_alignment\tshows interval data as alignment\n
ggfittext\tgeom_fit_text\tshrinks, grows or wraps text to fit inside a defined rectangular area\n
gggenes\tgeom_gene_arrow\tdraws genes as arrows\n
ggimage\tgeom_image\tvisualizes image files\n
ggimage\tgeom_phylopic\tqueries image files from phylopic database and visualizes them\n
ggplot2\tgeom_hline\tadds horizontal lines\n
ggplot2\tgeom_jitter\tadds a small amount of random variation to the location of each point\n
ggplot2\tgeom_label\tdraws a rectangle behind the text\n
ggplot2\tgeom_point\tcreats scatterplots\n
ggplot2\tgeom_raster\ta high performance special case for all the tiles are the same size\n
ggplot2\tgeom_rect\tdraws rectangle by using the locations of the four coners\n
ggplot2\tgeom_segment\tdraws a straight line between points\n
ggplot2\tgeom_spoke\ta polar parameterisation of 'geom_segment()'\n
ggplot2\tgeom_text\tadds text to the plot\n
ggplot2\tgeom_tile\tdraws rectangle by using the center of the tile and its size\n
ggplot2\tgeom_vline\tadds vertical lines\n
ggrepel\tgeom_text_repel\tadds text to the plot. The text labels repel away from each other and away from the data points\n
ggrepel\tgeom_label_repel\tdraws a rectangle underneath the text. The text labels repel away from each other and away from the data ponts\n
ggridges\tgeom_density_ridges\tarranges multiple density plots in a staggered fashion\n
ggridges\tgeom_density_ridges_gradient\tworks just like 'geom_density_ridges' except that the 'fill' aesthetic can vary along the x axis\n
ggridges\tgeom_ridgeline\tplots the sum of the 'y' and 'height' aesthetics versus 'x', filling the area between 'y' and 'y + height' with a color\n
ggridges\tgeom_ridgeline_gradient\tworks just like 'geom_ridgeline' except that the 'fill' aesthetic can vary along the x axis\n
ggstance\tgeom_barh\thorizontal version of 'geom_bar()'\n
ggstance\tgeom_boxploth\thorizontal version of 'geom_boxplot()'\n
ggstance\tgeom_crossbarh\thorizontal version of 'geom_crossbar()'\n
ggstance\tgeom_errorbarh\thorizontal version of 'geom_errorbarh()'\n
ggstance\tgeom_histogramh\thorizontal version of 'geom_histogram()'\n
ggstance\tgeom_linerangeh\thorizontal version of 'geom_linerange()'\n
ggstance\tgeom_pointrangeh\thorizontal version of 'geom_pointrange()'\n
ggstance\tgeom_violinh\thorizontal version of 'geom_violin()'\n
ggtree\tgeom_motif\tdraws aligned motifs\n
"
xx <- strsplit(x, "\n\n")[[1]]
y <- strsplit(xx, "\t") %>% do.call("rbind", .)
y <- as.data.frame(y)
colnames(y) <- c("Package", "Geom Layer", "Description")
require(kableExtra)
caption = "Geometric layers that supported by 'geom_facet()' and 'facet_plot()'"
knitr::kable(y, caption=caption, booktabs = T) %>%
collapse_rows(columns = 1, latex_hline = "major", valign ="top") %>%
kable_styling(latex_options = c("striped", "scale_down"),
bootstrap_options = c("striped", "hover")) #%>% landscape
```
<!--
#``` r treeio, echo=F, message=FALSE}
library(kableExtra)
ff <- matrix(c(
'read.beast', "parsing output of BEAST",
"read.codeml", "parsing output of CodeML (rst and mlc files)",
"read.codeml_mlc", "parsing mlc file (output of CodeML)",
"read.hyphy", "parsing output of HYPHY",
"read.jplace", "parsing jplace file including output of EPA and pplacer",
"read.nhx", "parsing NHX file including output of PHYLDOG and RevBayes",
"read.paml_rst", "parsing rst file (output of BaseML or CodeML)",
"read.phylip", "parsing phylip file",
"read.phyloT", "parsing output of phyloT (http://phylot.biobyte.de/)",
"read.r8s", "parsing output of r8s",
"read.raxml", "parsing output of RAxML",
"Nnode", "get number of internal nodes",
"Nnode2", "get number of all nodes (including internal and external nodes)",
"Ntip", "get number of all tips (external nodes)",
"as.phylo", "convert tree object to phylo object",
"as.treedata", "convert tree object to treedata object",
"drop.tip", "remove tips in a tree",
"get.fields", "get available tree attributes stored in tree object",
"get.placements", "get placement result of jplace object",
"get.subs", "get substitution by comparing parent node and child node",
"get.tipseq", "get tip sequences",
"get.treetext", "get newick text of a tree object",
"groupClade", "add clade grouping information to tree object",
"groupOTU", "grouping OTUs by tracing back to most recent commond ancestor",
"merge_tree", "merging tree objects into one",
"write.jpace", "write tree with associated annotation data to a jplace file"
), ncol=2, byrow=TRUE)
ff <- as.data.frame(ff)
colnames(ff) <- c("Function", "Description")
knitr::kable(ff, caption = "Functions defined in treeio.", booktabs = T) #%>%
#kable_styling(latex_options = c("striped", "hold_position"), full_width = T)
```
-->
# Session Info {#session-info}
The book was written using R Markdown and was compiled by `r CRANpkg("bookdown")` package. Here is the session information on the system on which this thesis was compiled:
```{r sessionInfo, echo=FALSE, cache=FALSE}
options(width=90)
devtools::session_info()
## sessionInfo() %>% capture.output %>%
## `[`(., -c(5:8)) %>% paste('\n') %>% cat
## options(width=80)
```