-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path04_ggtree_visualization.qmd
More file actions
1054 lines (798 loc) · 47.6 KB
/
Copy path04_ggtree_visualization.qmd
File metadata and controls
1054 lines (798 loc) · 47.6 KB
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
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\newpage
# Phylogenetic Tree Visualization {#sec-chapter4}
```{r}
#| echo: false
#| warning: false
#| message: false
library("ape")
library("grid")
library("ggplot2")
library("cowplot")
library("treeio")
library("ggtree")
source("setup.R")
source("software-link.R")
```
## Introduction
There are many software packages and web tools that are designed for displaying phylogenetic trees, such as `r pkg_treeview` [@page_visualizing_2002], `r pkg_figtree`\index{FigTree}, `r pkg_treedyn` [@chevenet_treedyn:_2006], *Dendroscope* [@huson_dendroscope_2012], `r pkg_evolview`\index{EvolView} [@he_evolview_2016], and `r pkg_itol`\index{iTOL} [@letunic_interactive_2007], *etc*. Only a few of them, such as `r pkg_figtree`, `r pkg_treedyn` and `r pkg_itol`, allow users to annotate the trees with colored branches, highlighted clades with tree features. However, their pre-defined annotating functions are usually limited to some specific phylogenetic data. As phylogenetic trees are becoming more widely used in multidisciplinary studies, there is an increasing need to incorporate various types of phylogenetic covariates and other associated data from different sources into the trees for visualizations and further analyses. For instance, the influenza virus has a wide host range, diverse and dynamic genotypes, and characteristic transmission behaviors that are mostly associated with the virus's evolution and essentially among themselves. Therefore, in addition to standalone applications that focus on each of the specific analysis and data types, researchers studying molecular evolution need a robust and programmable platform that allows the high levels of integration and visualization of many of these different aspects of data (raw or from other primary analyses) over the phylogenetic trees to identify their associations and patterns.
To fill this gap, we developed `r Biocpkg("ggtree")`\index{ggtree} [@yu_ggtree:_2017], a package for the R programming language [@rstats] released under the Bioconductor\index{Bioconductor} project [@gentleman_bioconductor_2004]. The `r Biocpkg("ggtree")` is built to work with `treedata` objects (see Chapters [1](#chapter1) and [9](#chapter9)), and display tree graphics with the `r CRANpkg("ggplot2")` package [@wickham_ggplot2_2016] that was based on the grammar of graphics [@wilkinson_grammar_2005].
The R language is increasingly used in phylogenetics. However, a comprehensive package, designed for viewing and annotating phylogenetic trees, particularly with complex data integration, is not yet available. Most of the R packages in phylogenetics focus on specific statistical analyses rather than viewing and annotating the trees with more generalized phylogeny-associated data. Some packages, including `r CRANpkg("ape")`\index{ape} [@paradis_ape_2004] and `r CRANpkg("phytools")` [@revell_phytools_2012], which are capable of displaying and annotating trees, are developed using the base graphics system of R. In particular, `r CRANpkg("ape")` is one of the fundamental packages for phylogenetic analysis and data processing. However, the base graphics system is relatively difficult to extend and limits the complexity of the tree figure to be displayed. `r CRANpkg("OutbreakTools")` [@jombart_outbreaktools_2014] and `r Biocpkg("phyloseq")` [@mcmurdie_phyloseq_2013] extended `r CRANpkg("ggplot2")` to plot phylogenetic trees. The `r CRANpkg("ggplot2")` system of graphics allows rapid customization and exploration of design solutions. However, these packages were designed for epidemiology and microbiome data respectively and did not aim to provide a general solution for tree visualization\index{visualization} and annotation\index{annotation}. The `r Biocpkg("ggtree")` package also inherits versatile properties of `r CRANpkg("ggplot2")`, and more importantly allows constructing complex tree figures by freely combining multiple layers of annotations (see also [Chapter 5](#chapter5)) using the tree associated data imported from different sources (see detailed in [Chapter 1](#chapter1) and [@wang_treeio_2020]).
## Visualizing Phylogenetic Tree with `r Biocpkg("ggtree")`
The `r Biocpkg("ggtree")` package [@yu_ggtree:_2017] is designed for annotating phylogenetic trees with their associated data of different types and from various sources. These data could come from users or analysis programs and might include evolutionary rates, ancestral sequences\index{ancestral sequences}, *etc.* that are associated with the taxa from real samples, or with the internal nodes representing hypothetic ancestor strain/species, or with the tree branches indicating evolutionary time courses [@wang_treeio_2020]. For instance, the data could be the geographic positions of the sampled avian influenza viruses (informed by the survey locations) and the ancestral nodes (by phylogeographic inference) in the viral gene tree [@lam_phylodynamics_2012].
The `r Biocpkg("ggtree")` supports `r CRANpkg("ggplot2")`'s graphical language, which allows a high level of customization, is intuitive and flexible. Notably, `r CRANpkg("ggplot2")` itself does not provide low-level geometric objects or other support for tree-like structures, and hence `r Biocpkg("ggtree")` is a useful extension in that regard. Even though the other two phylogenetics-related R packages, `r CRANpkg("OutbreakTools")`, and `r Biocpkg("phyloseq")`, are developed based on `r CRANpkg("ggplot2")`, the most valuable part of the `r CRANpkg("ggplot2")`\index{ggplot2} syntax - adding layers of annotations - is not supported in these packages. For example, if we have plotted a tree without taxa labels, `r CRANpkg("OutbreakTools")` and `r Biocpkg("phyloseq")` provide no easy way for general `R` users, who have little knowledge about the infrastructures of these packages, to add a layer of taxa labels. The `r Biocpkg("ggtree")` extends `r CRANpkg("ggplot2")` to support tree objects by implementing a geometric layer, `geom_tree()`, to support visualizing tree structure. In `r Biocpkg("ggtree")`, viewing a phylogenetic tree is relatively easy, via the command `ggplot(tree_object) + geom_tree() + theme_tree()` or `ggtree(tree_object)` for short. Layers of annotations can be added one-by-one via the `+` operator. To facilitate tree visualization, `r Biocpkg("ggtree")` provides several geometric layers, including `geom_treescale()` for adding legend of tree branch scale (genetic distance, divergence time, *etc.*), `geom_range()` for displaying uncertainty of branch lengths (confidence interval or range, *etc.*), `geom_tiplab()` for adding taxa label, `geom_tippoint()` and `geom_nodepoint()` for adding symbols of tips and internal nodes, `geom_hilight()` for highlighting clades with rectangle, and `geom_cladelab()` for annotating selected clades with bar and text label, *etc.*. A full list of geometric layers provided by `r Biocpkg("ggtree")` is summarized in Table @tbl-geoms.
```{r}
#| echo: false
#| label: tbl-geoms
#| tbl-cap: |
#| A selection of `r Biocpkg("ggtree")` geometric layers.
geoms <- tibble::tribble(
~Layer, ~Description,
"geom_tree()", "draw tree branches",
"geom_tiplab()", "add tip labels",
"geom_tippoint()", "add points for tips",
"geom_nodepoint()", "add points for internal nodes",
"geom_treescale()", "add a scale bar",
"geom_range()", "display uncertainty ranges",
"geom_rootedge()", "draw root edge",
"geom_hilight()", "highlight a clade",
"geom_cladelab()", "label a clade with bar and text",
"geom_nodelab()", "label internal nodes"
)
knitr::kable(geoms, booktabs = TRUE)
```
To view a phylogenetic tree, we first need to parse the tree file into `R`.
The `r Biocpkg("treeio")` package is able to parse diverse annotation data from different software outputs into `S4` phylogenetic data objects (see also [Chapter 1](#chapter1)). The `r Biocpkg("ggtree")` package mainly utilizes these `S4` objects to display and annotate the tree. Other R packages defined `S3`/`S4` classes to store phylogenetic trees with domain-specific associated data, including `phylo4` and `phylo4d` defined in the `r CRANpkg("phylobase")` package, `obkdata` defined in the `r CRANpkg("OutbreakTools")` package, and `phyloseq` defined in the `r Biocpkg("phyloseq")` package, *etc*. All these tree objects are also supported in `r Biocpkg("ggtree")` and their specific annotation data can be used to annotate the tree directly in `r Biocpkg("ggtree")` (see also [Chapter 9](#chapter9)). Such compatibility of `r Biocpkg("ggtree")` facilitates the integration of data and analysis results. In addition, `r Biocpkg("ggtree")` also supports other tree-like structures, including [dendrogram](#dendrogram) and [tree graphs](#igraph).
### Basic Tree Visualization
The `r Biocpkg('ggtree')` package extends `r CRANpkg('ggplot2')` [@wickham_ggplot2_2016] package to support viewing a phylogenetic tree.
It implements `geom_tree()`\index{geom\textunderscore tree} layer for displaying a phylogenetic tree, as shown below in Figure @fig-basicviz-1.
```{r}
#| fig-height: 3
#| fig-align: "center"
#| eval: false
library("treeio")
library("ggtree")
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
ggplot(tree, aes(x, y)) + geom_tree() + theme_tree()
```
The function, `ggtree()`, was implemented as a shortcut to visualize a tree, and it works exactly the same as shown above.
The `r Biocpkg('ggtree')` package takes all the advantages of `r CRANpkg('ggplot2')`. For example, we can change the color, size, and type of the lines as we do with `r CRANpkg('ggplot2')` (Figure @fig-basicviz-2).
```{r}
#| fig-height: 3
#| fig-align: "center"
#| eval: false
ggtree(tree, color="firebrick", size=2, linetype="dotted")
```
By default, the tree is viewed in ladderize form, user can set the parameter `ladderize = FALSE` to disable it (Figure @fig-basicviz-3, see also [FAQ A.5](#branch-setting)).
```{r}
#| fig-height: 3
#| fig-align: "center"
#| eval: false
ggtree(tree, ladderize=FALSE)
```
The `branch.length` is used to scale the edge, user can set the parameter `branch.length = "none"` to only view the tree topology (cladogram, Figure @fig-basicviz-4) or other numerical variables to scale the tree (*e.g.*, _d~N~/d~S~_, see also in [Chapter 5](#chapter5)).
```{r}
#| fig-height: 3
#| fig-align: "center"
#| eval: false
ggtree(tree, branch.length="none")
```
```{r}
#| label: fig-basicviz
#| fig-width: 12
#| fig-height: 3
#| echo: false
#| fig-cap: |
#| **Basic tree visualization.**
#| fig-subcap:
#| - "Default ggtree output with ladderized effect"
#| - "Non-variable setting (e.g., color, size, line type)"
#| - "Non-ladderized tree"
#| - "Cladogram without branch length information"
#| layout-ncol: 4
#| out-width: "100%"
library("treeio")
library("ggtree")
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
p1 <- ggplot(tree, aes(x, y)) + geom_tree() + theme_tree()
p2 <- ggtree(tree, color="firebrick", size=2, linetype="dotted")
p3 <- ggtree(tree, ladderize=FALSE)
p4 <- ggtree(tree, branch.length="none")
p1
p2
p3
p4
```
### Layouts of a phylogenetic tree {#sec-tree-layouts}
Viewing phylogenetic with `r Biocpkg("ggtree")` is quite simple, just pass the tree object to the `ggtree()` function. We have developed several types of layouts for tree presentation (Figure @fig-layout), including *rectangular* (by default), *roundrect* (rounded rectangular), *ellipse*, *slanted*, *circular*, *fan*, *unrooted*\index{unrooted} (equal angle, daylight, and TreeAndLeaf-inspired methods), time-scaled, and two-dimensional layouts\index{tree layout}.
Here are examples of visualizing a tree with different layouts:
```{r}
library(ggtree)
set.seed(2017-02-16)
tree <- rtree(50)
ggtree(tree)
ggtree(tree, layout="roundrect")
ggtree(tree, layout="slanted")
ggtree(tree, layout="ellipse")
ggtree(tree, layout="circular")
ggtree(tree, layout="fan", open.angle=120)
ggtree(tree, layout="equal_angle")
ggtree(tree, layout="daylight")
ggtree(tree, branch.length='none')
ggtree(tree, layout="ellipse", branch.length="none")
ggtree(tree, branch.length='none', layout='circular')
ggtree(tree, layout="daylight", branch.length = 'none')
```
```{r}
#| echo: false
#| label: fig-layout
#| fig-width: 12
#| fig-height: 9
#| fig-cap: |
#| **Tree layouts.**
#| fig-subcap:
#| - "Phylogram: rectangular layout"
#| - "Phylogram: rounded rectangular layout"
#| - "Phylogram: slanted layout"
#| - "Phylogram: ellipse layout"
#| - "Phylogram: circular layout"
#| - "Phylogram: fan layout"
#| - "Unrooted: equal-angle method"
#| - "Unrooted: daylight method"
#| - "Cladogram: rectangular layout"
#| - "Cladogram: ellipse"
#| - "Cladogram: circular layout"
#| - "Cladogram: unrooted layout"
#| layout-ncol: 4
#| out-extra: ''
#| message: false
#| out-width: "100%"
library(ggtree)
set.seed(2017-02-16)
tree <- rtree(50)
p1 <- ggtree(tree)
p2 <- ggtree(tree, layout="roundrect")
p3 <- ggtree(tree, layout="slanted")
p4 <- ggtree(tree, layout="ellipse")
p5 <- ggtree(tree, layout="circular")
p6 <- ggtree(tree, layout="fan", open.angle=120)
p7 <- ggtree(tree, layout="equal_angle")
p8 <- ggtree(tree, layout="daylight")
p9 <- ggtree(tree, branch.length='none')
p10 <- ggtree(tree, layout="ellipse", branch.length="none")
p11 <- ggtree(tree, branch.length='none', layout='circular')
p12 <- ggtree(tree, layout="daylight", branch.length = 'none')
p1
p2
p3
p4
p5
p6
p7
p8
p9
p10
p11
p12
```
Other possible layouts that can be drawn by modifying
scales/coordination (Figure @fig-layout2).
<!-- for examples, [reverse label of time
scale](https://github.com/GuangchuangYu/ggtree/issues/87), [repropotion
circular/fan tree](
https://groups.google.com/d/msg/bioc-ggtree/UoGQekWHIvw/ZswUUZKSGwAJ), *etc.*. -->
```{r}
ggtree(tree) + scale_x_reverse()
ggtree(tree) + coord_flip()
ggtree(tree) + layout_dendrogram()
ggplotify::as.ggplot(ggtree(tree), angle=-30, scale=.9)
ggtree(tree, layout='slanted') + coord_flip()
ggtree(tree, layout='slanted', branch.length='none') + layout_dendrogram()
ggtree(tree, layout='circular') + xlim(-10, NA)
ggtree(tree) + layout_inward_circular()
ggtree(tree) + layout_inward_circular(xlim=15)
```
```{r}
#| echo: false
#| warning: false
# tree_angle <- grid::grid.grabExpr(print(ggtree(tree), newpage=TRUE, vp = grid::viewport(angle=-30, width=.9, height=.9)))
tree_angle <- ggplotify::as.ggplot(ggtree(tree), angle=-30, scale=.9)
```
```{r}
#| label: fig-layout2
#| fig-cap: |
#| **Derived Tree layouts.**
#| fig-subcap:
#| - "Right-to-left rectangular layout"
#| - "Bottom-up rectangular layout"
#| - "Top-down rectangular layout (dendrogram)"
#| - "Rotated rectangular layout"
#| - "Bottom-up slanted layout"
#| - "Top-down slanted layout (cladogram)"
#| - "Circular layout"
#| - "Circular inward layout"
#| - "Circular inward layout (custom xlim)"
#| fig-width: 8
#| fig-height: 8
#| echo: false
#| warning: false
#| out-width: "100%"
#| fig-show: hold
#| layout-ncol: 3
ggtree(tree) + scale_x_reverse()
ggtree(tree) + coord_flip()
ggtree(tree) + layout_dendrogram()
tree_angle
ggtree(tree, layout='slanted') + coord_flip()
ggtree(tree, layout='slanted', branch.length='none') + layout_dendrogram()
ggtree(tree, layout='circular') + xlim(-10, NA)
ggtree(tree) + layout_inward_circular()
ggtree(tree) + layout_inward_circular(xlim=15)
```
**Phylogram.** Layouts of *rectangular*, *roundrect*, *slanted*, *ellipse*, *circular*, and *fan* are supported to visualize phylogram\index{phylogram} (by default, with branch length scaled) as demonstrated in Figures @fig-layout-1–@fig-layout-6.
**Unrooted layout.** Unrooted (also called `r squote('radial')`) layout is supported by the equal-angle, daylight, and `tree_and_leaf` algorithms. Users can choose among them by setting the `layout` parameter to `"equal_angle"`, `"daylight"`, or `"tree_and_leaf"`. The equal-angle method was proposed by Christopher Meacham in *PLOTREE* and later incorporated into `r pkg_phylip` [@retief_phylogenetic_2000]. Starting from the root, it allocates angular space to each subtree in proportion to the number of descendant tips. This strategy is fast and has been implemented in many software packages, but it often leaves large unused areas while densely packing the tips (Figure @fig-layout-7). The daylight method begins with an equal-angle tree and iteratively swings subtrees around internal nodes so that the arcs of "daylight" are more evenly distributed (Figure @fig-layout-8); this method was first implemented in `r pkg_paup` [@wilgenbusch_inferring_2003]. The `tree_and_leaf` layout, inspired by the `r Biocpkg("TreeAndLeaf")` package and now integrated directly into `ggtree()`, provides another deterministic alternative. It starts from an existing unrooted layout and applies a static relaxation step to increase the separation among leaves while preserving the overall shape of the tree. This behavior is especially useful when tip labels or other annotations are concentrated near the periphery.
**Cladogram.** To visualize a cladogram\index{cladogram} that is without branch length scaling and only displays the tree structure, `branch.length` is set to "none" and it works for all types of layouts (Figures @fig-layout-9–@fig-layout-12).
```{r}
#| echo: false
#| label: fig-treeAndLeafLayout
#| fig-cap: |
#| Comparing unrooted layouts with the leaf-centric `tree_and_leaf` layout.
#| fig-subcap:
#| - "Equal-angle layout"
#| - "Daylight layout"
#| - "TreeAndLeaf-inspired layout"
#| out-width: "100%"
#| fig-height: 3.5
#| fig-show: hold
#| layout-ncol: 3
set.seed(2017-02-16)
tree_tl <- rtree(50)
p_equal_angle <- ggtree(tree_tl, layout = "equal_angle")
p_daylight <- ggtree(tree_tl, layout = "daylight")
p_tree_and_leaf <- ggtree(tree_tl, layout = "tree_and_leaf")
p_equal_angle
p_daylight
p_tree_and_leaf
```
Figure @fig-treeAndLeafLayout illustrates that `tree_and_leaf` preserves the overall topology while allocating more space to the tips. It should therefore be viewed as a complement to the daylight layout rather than a replacement for it. When the main visual burden lies near the leaves, such as when many labels or tip-associated data layers are present, `tree_and_leaf` can provide a clearer display.
```{r}
#| label: fig-treeAndLeafLabels
#| fig-cap: |
#| **Leaf-focused unrooted layouts.** Compared with the daylight layout, `tree_and_leaf` provides more room for tip labels while preserving the overall tree shape.
#| fig-subcap:
#| - "Daylight layout with tip labels"
#| - "`tree_and_leaf` layout with tip labels"
#| out-width: "100%"
#| fig-height: 4.4
#| fig-show: hold
#| layout-ncol: 2
set.seed(20260309)
tree_leaf_demo <- rtree(30)
ggtree(tree_leaf_demo, layout = "daylight") +
geom_tiplab(size = 2.4, align = TRUE, linesize = 0.25)
ggtree(tree_leaf_demo, layout = "tree_and_leaf") +
geom_tiplab(size = 2.4, align = TRUE, linesize = 0.25)
```
```{r}
#| label: fig-ggdoubletree
#| fig-cap: |
#| **Paired-tree layout.** `ggdoubletree()` compares two trees and draws association links between matched taxa while preserving tree-side annotations.
#| fig-scap: "Paired-tree layout"
#| out-width: '100%'
#| fig-height: 4.8
tr_left <- ape::read.tree(text = "(((a,b),c),(d,e));")
tr_right <- ape::read.tree(text = "((e,d),(c,(b,a)));")
assoc <- data.frame(
left = c("a", "b", "c", "d", "e"),
right = c("a", "b", "c", "d", "e")
)
left_view <- ggtree(tr_left) +
geom_tiplab() +
geom_tippoint(color = "#3C78D8", size = 2)
right_view <- ggtree(tr_right) +
geom_tiplab() +
geom_tippoint(color = "#D95F02", size = 2)
p_tangle <- ggdoubletree(
left_view,
right_view,
assoc,
optimize = TRUE,
optimize_side = "both"
)
p_tangle +
geom_tanglelink(alpha = 0.35, color = "grey50")
```
**Timescaled layout.** For a timescaled tree\index{time-scaled tree}, the most recent sampling date must be specified via the `mrsd` parameter, and `ggtree()` will scale the tree by sampling (tip) and divergence (internal node) time, and a timescale axis will be displayed under the tree by default. Users can use the `r CRANpkg("deeptime")` package to add geologic timescale (e.g., periods and eras) to a `ggtree()` plot.
```{r}
#| label: fig-timescaled
#| fig-cap: |
#| **Timescaled layout.** The *x*-axis is the timescale (in units of the year). The divergence time in this example was inferred by `r pkg_beast` using the molecular clock model.
#| fig-scap: "Timescaled layout"
#| out-extra: ''
#| fig-height: 4.5
beast_file <- system.file("examples/MCC_FluA_H3.tree",
package="ggtree")
beast_tree <- read.beast(beast_file)
ggtree(beast_tree, mrsd="2013-01-01") + theme_tree2()
```
**Two-dimensional tree layout.** A two-dimensional tree\index{two-dimensional tree} is a projection of the phylogenetic tree in a space defined by the associated phenotype (numerical or categorical trait, on the _y_-axis) and tree branch scale (*e.g.*, evolutionary distance, divergent time, on the _x_-axis). The phenotype can be a measure of certain biological characteristics of the taxa and hypothetical ancestors in the tree. This layout is useful to track the virus phenotypes or other behaviors (*y*-axis) changing with the virus evolution (*x*-axis). In fact, the analysis of phenotypes or genotypes over evolutionary time have been widely used for study of influenza virus evolution [@neher_prediction_2016], though such analysis diagrams are not tree-like, *i.e.*, no connection between data points, unlike our two-dimensional tree layout that connects data points with the corresponding tree branches. Therefore, this new layout we provided will make such data analysis easier and more scalable for large sequence datasets.
In this example, we used the previous timescaled tree of H3 human and swine influenza viruses (Figure @fig-timescaled; data published in [@liang_expansion_2014]) and scaled the *y*-axis based on the predicted *N*-linked glycosylation sites (NLG) for each of the taxon and ancestral sequences of hemagglutinin proteins. The NLG sites were predicted using the [NetNGlyc 1.0 Server](http://www.cbs.dtu.dk/services/NetNGlyc/). To scale the *y*-axis, the parameter `yscale` in the `ggtree()` function is set to a numerical or categorical variable. If `yscale` is a categorical variable as in this example, users should specify how the categories are to be mapped to numerical values via the `yscale_mapping` variables.
```{r}
#| label: fig-2d
#| fig-cap: |
#| **Two-dimensional tree layout.** The trunk and other branches are highlighted in red (for swine) and blue (for humans). The *x*-axis is scaled to the branch length (in units of year) of the timescaled tree. The *y*-axis is scaled to the node attribute variable, in this case, the number of predicted *N*-linked glycosylation sites (NLG) on the hemagglutinin protein. Colored circles indicate the different types of tree nodes. Note that nodes assigned the same *x*- (temporal) and *y*- (NLG) coordinates are superimposed in this representation and appear as one node, which is shaded based on the colors of all the nodes at that point.
#| fig-width: 8.5
#| fig-height: 5
#| fig-scap: "Two-dimensional tree layout"
#| out-extra: ''
#| out-width: '100%'
NAG_file <- system.file("examples/NAG_inHA1.txt", package="ggtree")
NAG.df <- read.table(NAG_file, sep="\t", header=FALSE,
stringsAsFactors = FALSE)
NAG <- NAG.df[,2]
names(NAG) <- NAG.df[,1]
## separate the tree by host species
tip <- as.phylo(beast_tree)$tip.label
beast_tree <- groupOTU(beast_tree, tip[grep("Swine", tip)],
group_name = "host")
p <- ggtree(beast_tree, aes(color=host), mrsd="2013-01-01",
yscale = "label", yscale_mapping = NAG) +
theme_classic() + theme(legend.position='none') +
scale_color_manual(values=c("blue", "red"),
labels=c("human", "swine")) +
ylab("Number of predicted N-linked glycosylation sites")
## (optional) add more annotations to help interpretation
p + geom_nodepoint(color="grey", size=3, alpha=.8) +
geom_rootpoint(color="black", size=3) +
geom_tippoint(size=3, alpha=.5) +
annotate("point", 1992, 5.6, size=3, color="black") +
annotate("point", 1992, 5.4, size=3, color="grey") +
annotate("point", 1991.6, 5.2, size=3, color="blue") +
annotate("point", 1992, 5.2, size=3, color="red") +
annotate("text", 1992.3, 5.6, hjust=0, size=4, label="Root node") +
annotate("text", 1992.3, 5.4, hjust=0, size=4,
label="Internal nodes") +
annotate("text", 1992.3, 5.2, hjust=0, size=4,
label="Tip nodes (blue: human; red: swine)")
```
As shown in Figure @fig-2d, a two-dimensional tree is good at visualizing the change of phenotype over the evolution in the phylogenetic tree. In this example, it is shown that the H3 gene of the human influenza A virus maintained a high level of *N*-linked glycosylation sites (n=8 to 9) over the last two decades and dropped significantly to 5 or 6 in a separate viral lineage transmitted to swine populations and established there. It was indeed hypothesized that the human influenza virus with a high level of glycosylation on the viral hemagglutinin protein provides a better shielding effect to protect the antigenic sites from exposure to the herd immunity, and thus has a selective advantage in human populations that maintain a high level of herd immunity against the circulating human influenza virus strains. For the viral lineage that newly jumped across the species barrier and transmitted to the swine population, the shielding effect of the high-level surface glycan oppositely imposes selective disadvantage because the receptor-binding domain may also be shielded which greatly affects the viral fitness of the lineage that newly adapted to a new host species. Another example of a two-dimensional tree can be found in Figure @fig-phenogram.
## Displaying Tree Components
### Displaying treescale (evolution distance) {#sec-geom-trescale}
To show treescale, the user can use `geom_treescale()` layer (Figures @fig-treescale-1–@fig-treescale-3).
```{r}
#| fig-height: 4
#| fig-align: "center"
#| eval: false
ggtree(tree) + geom_treescale()
```
`geom_treescale()` supports the following parameters:
+ *x* and *y* for treescale position
+ *width* for the length of the treescale
+ *fontsize* for the size of the text
+ *linesize* for the size of the line
+ *offset* for relative position of the line and the text
+ *color* for color of the treescale
```{r}
ggtree(tree) + geom_treescale(x=0, y=45, width=1, color='red')
ggtree(tree) + geom_treescale(fontsize=6, linesize=2, offset=1)
```
We can also use `theme_tree2()` to display the treescale by adding *x axis* (Figure @fig-treescale-4).
```{r}
#| fig-height: 3
#| fig-align: "center"
#| eval: false
ggtree(tree) + theme_tree2()
```
```{r}
#| label: fig-treescale
#| fig-width: 8
#| fig-height: 6
#| echo: false
#| fig-cap: |
#| **Display treescale.**
#| fig-subcap:
#| - "Default treescale"
#| - "Modify color, width, and position"
#| - "Modify scale bar and text styling"
#| - "Enable x-axis for timescaled tree"
#| layout-ncol: 2
#| out-width: '100%'
p1 <- ggtree(tree) + geom_treescale()
p2 <- ggtree(tree) + geom_treescale(x=0, y=45, width=1, color='red')
p3 <- ggtree(tree) + geom_treescale(fontsize=6, linesize=2, offset=1)
p4 <- ggtree(tree) + theme_tree2()
p1
p2
p3
p4
```
Treescale is not restricted to evolution distance, `r Biocpkg('treeio')` can rescale the tree with other numerical variables (details described in [session 2.4](#rescale-treeio)), and `r Biocpkg("ggtree")` allows users to specify a numerical variable to serve as branch length for visualization (details described in [session 4.3](#rescale-tree)).
### Displaying nodes/tips {#sec-geom-nodepoint}
Showing all the internal nodes and tips in the tree can be done by adding a layer of points using `geom_nodepoint()`, `geom_tippoint()`, or `geom_point()` (Figure @fig-nodeTip).
```{r}
#| fig-height: 3
#| fig-align: "center"
#| eval: false
ggtree(tree) +
geom_point(aes(shape=isTip, color=isTip), size=3)
p <- ggtree(tree) +
geom_nodepoint(color="#b5e521", alpha=1/4, size=10)
p + geom_tippoint(color="#FDAC4F", shape=8, size=3)
```
```{r}
#| fig-width: 8
#| fig-height: 3
#| echo: false
#| label: fig-nodeTip
#| fig-cap: |
#| Display external and internal nodes.
#| fig-subcap:
#| - "geom_point() automatically adds symbolic points of all nodes"
#| - "geom_nodepoint() adds internal nodes and geom_tippoint() adds external nodes"
#| out-width: '100%'
#| fig-show: hold
#| layout-ncol: 2
p1 <- ggtree(tree) + geom_point(aes(shape=isTip, color=isTip), size=3)
p <- ggtree(tree) + geom_nodepoint(color="#b5e521", alpha=1/4, size=10)
p2 <- p + geom_tippoint(color="#FDAC4F", shape=8, size=3)
p1
p2
```
### Displaying labels
Users can use `geom_text()` or `geom_label()` to display the node (if available) and tip labels simultaneously or `geom_tiplab()` to only display tip labels (Figure @fig-tiplab-1).
```{r}
#| fig-height: 3
#| warning: false
#| fig-align: "center"
#| eval: false
p + geom_tiplab(size=3, color="purple")
```
The `geom_tiplab()` layer not only supports using *text* or *label* geom to display labels, but
it also supports *image* geom to label tip with image files (see [Chapter 7](#chapter7)). A corresponding
geom, `geom_nodelab()` is also provided for displaying node labels.
For *circular* and *unrooted* layouts, `r Biocpkg('ggtree')` supports rotating node labels according to the angles of the branches (Figure @fig-tiplab-2).
```{r}
#| fig-height: 6
#| warning: false
#| fig-align: "center"
#| eval: false
ggtree(tree, layout="circular") + geom_tiplab(aes(angle=angle), color='blue')
```
For long tip labels, the label may be truncated. There are several ways to solve this issue (see [FAQ: Tip label truncated](#faq-label-truncated)). Another solution to solve this issue is to display tip labels as *y*-axis labels (Figure @fig-tiplab-3). However, it only works for rectangular and dendrogram layouts and users need to use `theme()` to adjust tip labels in this case\index{taxa label}.
```{r}
#| fig-height: 6
#| warning: false
#| fig-align: "center"
#| eval: false
ggtree(tree) + geom_tiplab(as_ylab=TRUE, color='firebrick')
```
```{r}
#| fig-width: 13.5
#| fig-height: 5.5
#| echo: false
#| label: fig-tiplab
#| fig-cap: |
#| Display tip labels.
#| fig-subcap:
#| - "geom_tiplab() supports displaying tip labels"
#| - "For the circular, fan, or unrooted layouts, labels can be rotated"
#| - "For the dendrogram/rectangular layout, tip labels can be displayed as y-axis labels"
#| fig-show: hold
#| layout-ncol: 3
#| out-width: '100%'
p1 <- p + geom_tiplab(size=3, color="purple")
p2 <- ggtree(tree, layout="circular") + geom_tiplab(aes(angle=angle), color='blue')
p2 = ggplotify::as.ggplot(p2 + ggimage::theme_transparent(), scale=1.3, hjust=-.1, vjust=.05)
p3 <- ggtree(tree) + geom_tiplab(as_ylab=TRUE, color='firebrick')
p1
p2
p3
```
By default, the positions to display text are based on the node positions; we can change them to be based on the middle of the branch/edge (by setting `aes(x = branch)`), which is very useful when annotating transition from the parent node to the child node.
### Displaying root-edge
The `ggtree()` doesn't plot the root-edge by default. Users can use `geom_rootedge()` to automatically display the root-edge (Figure @fig-rootedge-1). If there is no root edge information, `geom_rootedge()` will display nothing by default (Figure @fig-rootedge-2). Users can set the root-edge to the tree (Figure @fig-rootedge-3) or specify `rootedge` in `geom_rootedge()` (Figure @fig-rootedge-4). A long root length is useful to increase readability of the circular tree (see also [FAQ: Enlarge center space](#faq-enlarge-center-space)).
```{r}
#| eval: false
## with root-edge = 1
tree1 <- read.tree(text='((A:1,B:2):3,C:2):1;')
ggtree(tree1) + geom_tiplab() + geom_rootedge()
## without root-edge
tree2 <- read.tree(text='((A:1,B:2):3,C:2);')
ggtree(tree2) + geom_tiplab() + geom_rootedge()
## setting root-edge
tree2$root.edge <- 2
ggtree(tree2) + geom_tiplab() + geom_rootedge()
## specify the length of root edge for just plotting
## this will ignore tree$root.edge
ggtree(tree2) + geom_tiplab() + geom_rootedge(rootedge = 3)
```
```{r}
#| fig-width: 6
#| fig-height: 4
#| echo: false
#| label: fig-rootedge
#| fig-cap: |
#| Display root-edge.
#| fig-subcap:
#| - "geom_rootedge() displays root-edge when present"
#| - "No root-edge information results in no root-edge"
#| - "Manually setting root edge for the tree"
#| - "Specifying root edge length only for plotting"
#| fig-show: hold
#| layout-ncol: 2
## with root edge = 1
tree1 <- read.tree(text='((A:1,B:2):3,C:2):1;')
p1 = ggtree(tree1) + geom_tiplab() + geom_rootedge()
## without root edge
tree2 <- read.tree(text='((A:1,B:2):3,C:2);')
p2 = ggtree(tree2) + geom_tiplab() + geom_rootedge()
## setting root edge
tree2$root.edge <- 2
p3 = ggtree(tree2) + geom_tiplab() + geom_rootedge()
## specify length of root edge for just plotting
## this will ignore tree$root.edge
p4 = ggtree(tree2) + geom_tiplab() + geom_rootedge(rootedge = 3)
p1
p2
p3
p4
```
### Color tree
In `r Biocpkg("ggtree")` [@yu_two_2018], coloring phylogenetic tree is easy, by using `aes(color=VAR)` to map the color of the tree based on a specific variable (both numerical and categorical variables are supported, see Figure @fig-colortree)\index{color tree}.
```{r}
#| label: fig-colortree
#| fig-width: 5
#| fig-height: 5
#| fig-cap: |
#| **Color tree by continuous or discrete feature.** Edges are colored by values associated with the child nodes.
#| fig-scap: "Color tree by continuous or discrete feature"
ggtree(beast_tree, aes(color=rate)) +
scale_color_continuous(low='darkgreen', high='red') +
theme(legend.position="right")
```
Users can use any feature (if available), including clade posterior and _d~N~/d~S~_, _etc._, to scale the color of the tree. If the feature is a continuous numerical value, `r Biocpkg("ggtree")` provides a `continuous` parameter to support plotting continuous state transition in edges. Here, we use an example^[<http://www.phytools.org/eqg2015/asr.html>] to demonstrate this functionality (Figure @fig-continuousColor-1). If you want to add a thin black border in tree branches, you can place a tree with black and slightly thicker branches below your tree to emulate edge outlines as demonstrated in Figure @fig-continuousColor-2.
```{r}
#| fig-width: 12
#| fig-height: 6
#| label: fig-continuousColor
#| fig-cap: |
#| Continuous state transition in edges.
#| fig-subcap:
#| - "Map trait to edge color directly"
#| - "Overlay colored edges on a thicker black tree for outlines"
#| out-width: '100%'
#| fig-show: hold
#| layout-ncol: 2
library(ggtree)
library(treeio)
library(tidytree)
library(ggplot2)
library(TDbook)
## ref: http://www.phytools.org/eqg2015/asr.html
##
## load `tree_anole` and `df_svl` from 'TDbook'
svl <- as.matrix(df_svl)[,1]
fit <- phytools::fastAnc(tree_anole, svl, vars=TRUE, CI=TRUE)
td <- data.frame(node = nodeid(tree_anole, names(svl)),
trait = svl)
nd <- data.frame(node = names(fit$ace), trait = fit$ace)
d <- rbind(td, nd)
d$node <- as.numeric(d$node)
tree <- full_join(tree_anole, d, by = 'node')
p1 <- ggtree(tree, aes(color=trait), layout = 'circular',
ladderize = FALSE, continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(hjust = -.1) +
xlim(0, 1.2) +
theme(legend.position = c(.05, .85))
p2 <- ggtree(tree, layout='circular', ladderize = FALSE, size=2.8) +
geom_tree(aes(color=trait), continuous = 'colour', size=2) +
scale_color_gradientn(colours=c("red", 'orange', 'green', 'cyan', 'blue')) +
geom_tiplab(aes(color=trait), hjust = -.1) +
xlim(0, 1.2) +
theme(legend.position = c(.05, .85))
p1
p2
```
Besides, we can use a two-dimensional tree (as demonstrated in Figure @fig-2d) to visualize phenotype on the vertical dimension to create the phenogram Figure @fig-phenogram. We can use the `r CRANpkg("ggrepel")` package to repel tip labels to avoid overlapping as demonstrated in Figure @fig-repelTip\index{tree layout}.
```{r}
#| label: fig-phenogram
#| fig-width: 6
#| fig-height: 8
#| fig-cap: |
#| **Phenogram.** Projecting the tree into a space defined by time (or genetic distance) on the horizontal axis and phenotype on the vertical dimension.
#| fig-scap: "Phenogram"
ggtree(tree, aes(color=trait), continuous = 'colour', yscale = "trait") +
scale_color_viridis_c() + theme_minimal()
```
### Rescale tree
Most of the phylogenetic trees are scaled by evolutionary distance (substitution/site). In `r Biocpkg("ggtree")`, users can rescale a phylogenetic tree by any numerical variable inferred by evolutionary analysis (*e.g.*, _d~N~/d~S~_)\index{tree branch}.
This example displays a time tree (Figure @fig-rescaleTree-1) and the branches were rescaled by substitution rate inferred by BEAST (Figure @fig-rescaleTree-2).
```{r}
#| fig-width: 10
#| fig-height: 5
library("treeio")
beast_file <- system.file("examples/MCC_FluA_H3.tree", package="ggtree")
beast_tree <- read.beast(beast_file)
beast_tree
p1 <- ggtree(beast_tree, mrsd='2013-01-01') + theme_tree2() +
labs(caption="Divergence time")
p2 <- ggtree(beast_tree, branch.length='rate') + theme_tree2() +
labs(caption="Substitution rate")
```
The following example draws a tree inferred by CodeML (Figure @fig-rescaleTree-3), and the branches can be rescaled by using _d~N~/d~S~_ values (Figure @fig-rescaleTree-4).
```{r}
#| fig-width: 10
#| fig-height: 5
mlcfile <- system.file("extdata/PAML_Codeml", "mlc", package="treeio")
mlc_tree <- read.codeml_mlc(mlcfile)
p3 <- ggtree(mlc_tree) + theme_tree2() +
labs(caption="nucleotide substitutions per codon")
p4 <- ggtree(mlc_tree, branch.length='dN_vs_dS') + theme_tree2() +
labs(caption="dN/dS tree")
```
```{r}
#| fig-width: 8
#| fig-height: 6
#| echo: false
#| label: fig-rescaleTree
#| fig-cap: |
#| Rescale tree branches.
#| fig-subcap:
#| - "Time-scaled tree inferred by BEAST"
#| - "Branches rescaled by substitution rate inferred by BEAST"
#| - "Tree inferred by CodeML"
#| - "Branches rescaled by dN/dS values"
#| out-width: '100%'
#| fig-show: hold
#| layout-ncol: 2
p1
p2
p3
p4
```
This provides a very convenient way to allow us to explore the relationship between tree associated data and tree structure through visualization.
In addition to specifying `branch.length` in tree visualization, users can change branch length stored in tree object by using `rescale_tree()` function provided by the `r Biocpkg("treeio")` package [@wang_treeio_2020], and the following command will display a tree that is identical to Figure @fig-rescaleTree-2. The `rescale_tree()` function was documented in [session 2.4](#rescale-treeio).
```{r}
#| eval: false
beast_tree2 <- rescale_tree(beast_tree, branch.length='rate')
ggtree(beast_tree2) + theme_tree2()
```
### Modify components of a theme
The `theme_tree()`\index{theme} defined a totally blank canvas, while `theme_tree2()` adds
phylogenetic distance (via *x*-axis). These two themes all accept a parameter of
`bgcolor` that defined the background color. Users can use any [theme components](http://ggplot2.tidyverse.org/reference/theme.html) to the `theme_tree()` or `theme_tree2()` functions to modify them (Figure @fig-theme).
```{r}
set.seed(2019)
x <- rtree(30)
ggtree(x, color="#0808E5", size=1) + theme_tree("#FEE4E9")
ggtree(x, color="orange", size=1) + theme_tree('grey30')
```
```{r}
#| fig-width: 8
#| fig-height: 3
#| fig-align: "center"
#| echo: false
#| label: fig-theme
#| fig-cap: |
#| **Three themes.** All ggplot2 theme components can be modified, and all the ggplot2 themes can be applied to `ggtree()` output.
#| fig-subcap:
#| - "theme_tree() with custom background"
#| - "theme_tree() with a different background"
#| out-width: "100%"
#| fig-show: hold
#| layout-ncol: 2
library(ggimage)
set.seed(2019)
x <- rtree(30)
ggtree(x, color="#0808E5", size=1) + theme_tree("#FEE4E9")
ggtree(x, color="orange", size=1) + theme_tree('grey30')
```
Users can also use an image file as the tree background, see example in [Appendix B](#ggimage-bgimage).
## Paired Trees and Tanglegrams
In [Section @sec-ggtree-fortify], we showed how to manipulate fortified tree data and manually place two trees face-to-face for comparison. That approach remains useful when full control over coordinates, offsets, and layer composition is required. In many routine applications, however, the analytical goal is more specific: two related trees need to be displayed side by side, matched taxa need to be connected, and the annotations on both trees need to remain legible. For this common task, `ggtree` now provides native paired-tree support through `ggdoubletree()`, `fortify.tanglegram()`, `fortify.cophylo()`, and `geom_tanglelink()`.
The main user-facing entry point is `ggdoubletree()`. It accepts two raw trees, two pre-annotated `ggtree` objects, or a `cophylo` object together with an association table. This is particularly convenient when the two trees have already been decorated with labels, points, or other layers, because these annotations can be carried over into the paired display. When `optimize = TRUE`, `ggtree` performs a deterministic tip-order optimization step to reduce link crossings. The `optimize_side` argument specifies whether the optimization is applied to the `"right"` tree, the `"left"` tree, or `"both"`. Figure @fig-pairedTreesNative presents a minimal example constructed from two annotated `ggtree` objects.
```{r}
#| label: fig-pairedTreesNative
#| fig-cap: |
#| **A native paired-tree display.** `ggdoubletree()` compares two annotated `ggtree` objects, preserves their tip annotations, and adds association links as a standard plotting layer.
#| fig-scap: "A native paired-tree display"
#| out-width: '100%'
#| fig-height: 4.8
#| message: false
library(ggtree)
tr_left <- ape::read.tree(text = "(((a,b),c),(d,e));")
tr_right <- ape::read.tree(text = "((e,d),(c,(b,a)));")
assoc <- data.frame(
left = c("a", "b", "c", "d", "e"),
right = c("a", "b", "c", "d", "e")
)
left_view <- ggtree(tr_left) +
geom_tiplab() +
geom_tippoint(color = "#3C78D8", size = 2)
right_view <- ggtree(tr_right) +
geom_tiplab() +
geom_tippoint(color = "#D95F02", size = 2)
ggdoubletree(
left_view,
right_view,
assoc,
optimize = TRUE,
optimize_side = "both"
) +
geom_tanglelink(alpha = 0.35, color = "grey50")
```
Compared with the manual strategy in Section @sec-ggtree-fortify, the native paired-tree workflow removes much of the bookkeeping needed to mirror coordinates and resolve link positions by hand. At the same time, the lower-level `fortify.tanglegram()` and `fortify.cophylo()` methods remain useful when users wish to inspect or further manipulate the paired-tree data before drawing it.
## Visualize a List of Trees
The `r Biocpkg("ggtree")` supports `multiPhylo` and `treedataList` objects and a list of trees can be viewed simultaneously. The trees will visualize one on top of another and can be plotted in different panels through `facet_wrap()` or `facet_grid()` functions (Figure @fig-multiPhylo)\index{multiple trees}.
```{r}
#| label: fig-multiPhylo
#| fig-width: 12
#| fig-height: 4
#| fig-cap: |
#| **Visualizing multiPhylo object.** The `ggtree()` function supports visualizing multiple trees stored in the `multiPhylo` or `treedataList` objects.
#| fig-scap: "Visualizing multiPhylo object"
#| out-width: '100%'
## trees <- lapply(c(10, 20, 40), rtree)
## class(trees) <- "multiPhylo"
## ggtree(trees) + facet_wrap(~.id, scale="free") + geom_tiplab()
f <- system.file("extdata/r8s", "H3_r8s_output.log", package="treeio")
r8s <- read.r8s(f)
ggtree(r8s) + facet_wrap( ~.id, scale="free") + theme_tree2()
```
One hundred bootstrap trees can also be viewed simultaneously (Figure @fig-bp100). This allows researchers to explore a large set of phylogenetic trees to find consensus and distinct trees. The consensus tree can be summarized via a density tree (Figure @fig-densiTree).
```{r}
#| label: fig-bp100
#| fig-width: 20
#| fig-height: 20
#| fig-cap: |
#| **Visualizing one hundred bootstrap trees simultaneously.**
#| fig-scap: "One hundred bootstrap trees"
#| out-width: '100%'
btrees <- read.tree(system.file("extdata/RAxML",
"RAxML_bootstrap.H3",
package="treeio")
)
ggtree(btrees) + facet_wrap(~.id, ncol=10)
```
### Annotate one tree with values from different variables
To annotate one tree (the same tree) with the values from different variables, one can plot them separately and use `r CRANpkg("patchwork")` or `r CRANpkg("aplot")` to combine them side-by-side.