-
Notifications
You must be signed in to change notification settings - Fork 9
/
.Rapp.history
2722 lines (2722 loc) · 167 KB
/
.Rapp.history
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
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
text(total.length, mean(heights[start.one:end.one]), "Period", pos=4, font=3, cex=cex.labels)
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=-0.4, lwd=0, lwd.ticks=0.7, mgp=c(3, 0.35, 0), col="black", cex.axis=cex.age.int, pos=Ntip(phy) + (Ntip(phy) / 100), tck=tck.abs.age, ...)
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=-0.4, lwd=0, lwd.ticks=0.7, mgp=c(3, 0.35, 0), col="black", cex.axis=cex.age.int, pos=Ntip(phy) + (Ntip(phy) / 100), tck=tck.abs.age
)
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=-0.4, lwd=0, lwd.ticks=0.7, mgp=c(3, 0.35, 0), col="black", cex.axis=cex.age.int, pos=Ntip(phy) + (Ntip(phy) / 100), tck=-0.01
)
total.length
mtext("Age (Ma)", 3, at=total.length+0.1, font=3, xpd=TRUE, line=-0.4, col="black", cex=cex.labels)
mtext("Age (Ma)", 3, at=total.length+0.1, font=3, xpd=TRUE, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex=cex.labels)
axis("Age (Ma)", 3, at=total.length+0.1, font=3, xpd=TRUE, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex=cex.labels)
total.length+0.1
bin.period
total.length+0.1
axis("Age (Ma)", 3, at=total.length+0.1, font=3, xpd=TRUE, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex=cex.labels)
axis(3, labels="Age (Ma)", at=total.length+0.1, font=3, xpd=TRUE, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex=cex.labels)
cex.labels
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
axis(3, labels="Age (Ma)", at=total.length+0.1, font=3, xpd=TRUE, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex=cex.labels)
axis(3, labels="Age (Ma)", at=total.length+0.1, font=3, xpd=TRUE, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex.axis=cex.labels)
axis(3, labels="Age (Ma)", at=total.length+0.1, font=3, xpd=TRUE, line=-0.4, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex.axis=cex.labels)
axis(3, labels="Age (Ma)", at=total.length+0.1, font=3, xpd=TRUE, line=-0.4, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex.axis=cex.labels, mgp=c(3, 0.35, 0))
total.length
axis(3, labels="Age (Ma)", at=total.length+20, font=3, xpd=TRUE, line=-0.4, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex.axis=cex.labels, mgp=c(3, 0.35, 0))
axis(3, labels="Age (Ma)", at=total.length+5, font=3, xpd=TRUE, line=-0.4, lwd=0, pos=Ntip(phy) + (Ntip(phy) / 100), col="black", cex.axis=cex.labels, mgp=c(3, 0.35, 0))
mtext("Age (Ma)", 3, at=total.length+0.1, font=3, xpd=TRUE, line=1, col="black", cex=cex.labels)
mtext("Age (Ma)", 3, at=total.length+0.1, font=3, xpd=TRUE, col="black", cex=cex.labels)
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
axis(3, at=bin.epoch, labels=signif(total.length-bin.epoch, 2), line=-0.4, lwd=0, lwd.ticks=0.7, mgp=c(3, 0.35, 0), col="black", cex.axis=cex.age.int, pos=Ntip(phy), ...)
axis(3, at=bin.epoch, labels=signif(total.length-bin.epoch, 2), line=-0.4, lwd=0, lwd.ticks=0.7, mgp=c(3, 0.35, 0), col="black", cex.axis=cex.age.int, pos=Ntip(phy)
)
mtext("Age (Ma)", 3, at=total.length+0.1, font=3, xpd=TRUE, col="black", cex=cex.labels)
bin.period
xlimit
last.plot.coord
total.length
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
xlimit <- last.plot.coord$xx
xlimit
abline(h=tail( xlimit, 1))
abline(v=tail( xlimit, 1))
abline(v=min(xlimit))
abline(v=max(xlimit))
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
plot.bar(bin.period, Period.colour, names(Period.colour),start.one,end.one, grey.line=grey.now, cex.labels.int=cex.labels)
text(total.length, mean(heights[start.one:end.one]), "Period", pos=4, font=3, cex=cex.labels)
mean(heights[start.one:end.one])
Ntip(phy) + (Ntip(phy) / 100)
total.length
text(total.length, Ntip(phy) + (Ntip(phy) / 100), "Period", pos=4, font=3, cex=cex.labels)
text(total.length, Ntip(phy) + (Ntip(phy) / 90), "Period", pos=4, font=3, cex=cex.labels)
text(total.length, Ntip(phy) + (Ntip(phy) / 50), "Period", pos=4, font=3, cex=cex.labels)
text(total.length, Ntip(phy) + (Ntip(phy) / 50), "Age (Ma)", pos=4, font=3, cex=cex.labels)
text(total.length, Ntip(phy) + (Ntip(phy) / 50), "Age (Ma)", pos=4, font=4, cex=cex.labels)
Ntip(phy) + (Ntip(phy) / 100)
Ntip(phy) + (Ntip(phy) / 50)
(Ntip(phy) / 50
)
(Ntip(phy) / 50)
Ntip(phy) + (Ntip(phy) / 50)
Ntip(phy) + (Ntip(phy) / 100)
(Ntip(phy) / 100)
text(total.length, pos.age.mgp * 1.05, "Ma", pos=4, font=4, cex=cex.labels)
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
abs.age.mgp=c(3, 0.35, 0); abs.age.lwd.ticks=0.7; tck.abs.age=-0.01; abs.age.line=-0.4; abs.age.lwd=0; pos.age.mgp=NULL
if(is.null(pos.age.mgp)) pos.age.mgp <- Ntip(phy) + (Ntip(phy) / 100)
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age.mgp, tck=tck.abs.age, ...)
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age.mgp, tck=tck.abs.age
)
text(total.length, pos.age.mgp * 1.05, "Ma", pos=4, font=4, cex=cex.labels)
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels)
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, line=abs.age.line)
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, hdj=abs.age.line)
?text
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, adj=c(0, abs.age.line) )
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
abs.age.mgp=c(3, 0.35, 0); abs.age.lwd.ticks=0.7; tck.abs.age=-0.01; abs.age.line=-0.4; abs.age.lwd=0; pos.age.mgp=NULL#
#
if(is.null(pos.age.mgp)) pos.age.mgp <- Ntip(phy) + (Ntip(phy) / 100)#
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age.mgp, tck=tck.abs.age, ...)#
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, adj=c(0, abs.age.line) )
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
abs.age.mgp=c(3, 0.35, 0); abs.age.lwd.ticks=0.7; tck.abs.age=-0.01; abs.age.line=-0.4; abs.age.lwd=0; pos.age.mgp=NULL
if(is.null(pos.age.mgp)) pos.age.mgp <- Ntip(phy) + (Ntip(phy) / 100)
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
abs.age.mgp=c(3, 0.35, 0); abs.age.lwd.ticks=0.7; tck.abs.age=-0.01; abs.age.line=-0.4; abs.age.lwd=0; pos.age.mgp=NULL#
#
if(is.null(pos.age.mgp)) pos.age.mgp <- Ntip(phy) + (Ntip(phy) / 100)#
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age.mgp, tck=tck.abs.age)#
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, adj=c(0, abs.age.line) )
abs.age.line
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, adj=c(0, 1) )
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, adj=c(1, 1) )
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,0.001) )
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,0.001) , xpd=TRUE)
text(total.length, pos.age.mgp, "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,1) , xpd=TRUE)
pos.age.mgp
Ntip(phy) + (Ntip(phy) / 100)
pos.age.mgp
text(total.length, pos.age.mgp + (pos.age.mgp / 100), "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,1) , xpd=TRUE)
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color)
abs.age.mgp=c(3, 0.35, 0); abs.age.lwd.ticks=0.7; tck.abs.age=-0.01; abs.age.line=-0.4; abs.age.lwd=0; pos.age.mgp=NULL#
#
if(is.null(pos.age.mgp)) pos.age.mgp <- Ntip(phy) + (Ntip(phy) / 100)#
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age.mgp, tck=tck.abs.age)#
text(total.length, Ntip(phy) + (Ntip(phy) / 50), "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,1) , xpd=TRUE)
#' Plot time-scaled phylogenies#
#'#
#' Plot time-scaled phylogenies with node uncertainty and timescale#
#' @param phy A timescaled phylogeny, unless analysis.type="mcmctree" and build.tree=TRUE#
#' @param analysis.type The method used to generate the time-scale tree, one of mcmctree, MrBayes, RevBayes, or User.#
#' @param mcmc.chain The full posterior of age estimates for all nodes (default NULL)#
#' @param node.ages User-supplied node ages applicable for analysis.type user#
#' @param directory.files The directory for files to summarise for MrBayes and RevBayes analyses#
#' @param plot.type The plotting method for the phylogram corresponding to the APE definition. Phylogram is available for all analysis types, but cladogram is only avilable for mcmctree analyses at present. Type distributions plots a phylogram with density distributions on each of the nodes.#
#' @param build.tree Logical. Only applicable to mcmctree analyses, whether to timescale the phylogeny based on the full mcmc chain#
#' @param node.method For plot.type phylogram the method to dispay age uncertainty on each node, either bar, node.length, or full.length.#
#' @param all.nodes If NULL (default) node uncertainty is plotted on all nodes. If node numbers are supplied, only these nodes will be labelled with uncertainty.#
#' @param add.time.scale Logical. Adds a timescale to the plotted phylogeny.#
#' @param add.abs.time Logical. Adds an absolute timescale alongside the geological timescale. Only applicable if add.time.scale is TRUE#
#' @param scale.res The geological age designation to add to the plot can be one or a combination of Eon, Period, Epoch, Age. The order of plot (from bottom to top) is the same as the supplied order. Subsequent arguments add.abs.time and grey.bars are based on the last supplied age designation.#
#' @param label.timescale.names Logical. Add names (Eon, Period, Epoch, Age, Ma) to axis if timescale used#
#' @param time.correction Number to place branch lengths and age estimates in absolute time, deafult to one.#
#' @param col.age The colouring of the node.method bars to summarise node age uncertainty#
#' @param tip.lengths Logical. If the tree contains non-present tip ages, the age uncertainty associated with them will be added to the plot#
#' @param density.col Colour of the node distributions (if analysis.type=distributions)#
#' @param density.border.col Colour of the node distributions (if analysis.type=distributions) borders#
#' @param cex.tips Size of the phylogeny tip labels#
#' @param show.tip.label Logical. Should the tree tip labels be displayed#
#' @param col.tree Colour of the phylogeny edges#
#' @param tip.color Colour of the phylogeny tip labels#
#' @param lwd.bar Width of the bar to summarise age uncertainty, applicable only if plot.type is phylogram and node.method is bar#
#' @param grey.bars Logical. Should grey bars be used to signify time bins, applicable only if add.time.scale is TRUE#
#' @param cex.age Size of the labels for the absolute timescale#
#' @param cex.labels Size of the labels for the geological timescale#
#' @param relative.height the relative height of the timescale labels#
#' @param ntrees For analysis.type mrbayes, the number of independent chains to summarise#
#' @param tip.bar.col The colour of uncertainty around non-contemporary tips#
#' @param burn.in The number of points in the chain to discard for MrBayes and RevBayes analyses#
#' @param distribution.height The relative height of node distributions when plot.type is distributions measured as the relative height of the descendent node#
#' @param abs.age.mgp mgp values for the absolute age axis, only applicable if add.abs.time is TRUE#
#' @param abs.age.lwd.ticks lwd values for the absolute age axis ticks, only applicable if add.abs.time is TRUE#
#' @param abs.age.lwd lwd values for the absolute age axis horizontal line, only applicable if add.abs.time is TRUE#
#' @param tck.abs.age tck values for the absolute age axis tick height, only applicable if add.abs.time is TRUE#
#' @param abs.age.line line correction for the absolute age axis tick, only applicable if add.abs.time is TRUE#
#' @param pos.age position position of absolute age axis#
#' @param ... further arguments to be used in plot.phylo#
#' @return If plot=TRUE plot of distributions in file 'pdfOutput' written to current working directory#
#' @export#
#' @examples#
#' # example#
#
mcmc.tree.plot <- function(phy=NULL, analysis.type="mcmctree", mcmc.chain=NULL, node.ages=NULL, directory.files=NULL, plot.type="phylogram", build.tree=FALSE, node.method="bar", all.nodes=NULL, add.time.scale=TRUE, add.abs.time=TRUE, scale.res="Epoch", label.timescale.names=FALSE, time.correction=1, col.age="blue", tip.lengths=FALSE, density.col="#00000050", density.border.col="#00000080", cex.tips=1, show.tip.label=TRUE, col.tree="black", tip.color="black", lwd.bar=1, grey.bars=TRUE, cex.age=1, cex.labels=1, relative.height=0.08, n.trees=2, tip.bar.col="#ff000050", burn.in=0.25, distribution.height=0.8, abs.age.mgp=c(3, 0.35, 0), abs.age.lwd.ticks=0.7, abs.age.lwd=0, tck.abs.age=-0.01, abs.age.line=-0.4, pos.age=NULL, ...) {#
if(is.na(match(tolower(analysis.type), c("mcmctree", "mrbayes", "revbayes", "user")))) {#
stop("analysis.type must be one of 'mcmctree', 'mrbayes', 'revbayes', or 'user'")#
}#
if(any(is.na(match(scale.res, c("Eon", "Period", "Epoch", "Age"))))) {#
stop("scale.res must be one/combination of 'Eon', 'Period', 'Epoch', or 'Age'")#
}#
mb.tree <- TRUE#
if(is.numeric(match(tolower(analysis.type), "mrbayes"))) mb.tree <- FALSE#
if(mb.tree && plot.type == "distributions" && is.null(mcmc.chain)) stop("The distribution plot.type options needs the full mcmc.chain")#
if(analysis.type == "mcmctree") {#
phy.in <- phy#
phy <- phy.in[[1]]#
}#
if(analysis.type == "mrbayes") {#
phy.data <- read.in.mrbayes(directory.files)#
t.name <- gsub(".con.tre", ".run1.t", directory.files)#
phy <- phy.data$phy#
if(plot.type == "cladogram") stop("sorry plot type 'cladogram' only avilable for mcmctree trees")#
}#
if(analysis.type == "revbayes") {#
phy.data <- read.in.revbayes(directory.files)#
phy <- phy.data$phy#
if(plot.type == "cladogram") stop("sorry plot type 'cladogram' only avilable for mcmctree trees")#
}#
if(analysis.type == "user") {#
if(plot.type == "cladogram") stop("sorry plot type 'cladogram' only avilable for mcmctree trees")#
} #
if(add.time.scale) {#
highest.level <- length(scale.res)#
check.level <- any(is.na(match(scale.res, c("Eon", "Period", "Epoch", "Age"))))#
if(is.na(check.level)) stop("scale.res must be one of Eon, Period, Epoch, Age")#
each.height <- -(relative.height * Ntip(phy)) / highest.level#
t.depth <- (each.height * highest.level)#
t.height <- Ntip(phy) + (relative.height * Ntip(phy))#
heights <- seq(from=t.depth, to=0.5, length.out=highest.level + 1)#
} else {#
t.depth <- 0#
t.height <- Ntip(phy)#
}#
if(analysis.type == "mcmctree") {#
if(!is.null(mcmc.chain)) {#
internal.node.local <- which(regexpr("t_n", names(mcmc.chain)) != -1)#
node.estimates <- mcmc.chain[,internal.node.local] * time.correction#
phy.matrix <- matrix(NA, ncol=2, nrow=Ntip(phy) * 2 - 2)#
node.in.ape <- as.numeric(gsub("t_n", "", names(node.estimates)))#
hpd.ages <- apply(node.estimates, 2, function(x) HPDinterval(as.mcmc(x))) #
mean.ages <- apply(node.estimates, 2, function(x) mean(x))#
max.ages <- apply(node.estimates, 2, function(x) max(x))#
end.age <- hpd.ages[2,1]#
if(build.tree) {#
for(ii in 1:length(mean.ages)) phy.matrix[which(node.in.ape[ii] == phy$edge[,1]), 1] <- mean.ages[ii]#
ext.node <- which(phy$edge[,2] > Ntip(phy))#
phy.matrix[ext.node, 2] <- mean.ages[-1]#
phy.matrix[-ext.node, 2] <- 0#
b.lengths <- phy.matrix[,1] - phy.matrix[,2]#
phy$edge.length <- b.lengths#
phy$root.edge <- end.age - phy.matrix[1,1]#
} else {#
phy$edge.length <- phy$edge.length * time.correction#
phy$root.edge <- end.age - nodeTimes(phy)[1,1]#
}#
tip.range <- matrix(0, nrow=2, ncol=Ntip(phy))#
} else {#
phy$edge.length <- phy$edge.length * time.correction#
phy.in$nodeAges <- phy.in$nodeAges * time.correction#
hpd.ages <- t(phy.in$nodeAges[,-1])#
mean.ages <- phy.in$nodeAges[,1]#
end.age <- hpd.ages[2,1]#
node.in.ape <- as.numeric(rownames(phy.in$nodeAges))#
ext.node <- which(phy$edge[,2] > Ntip(phy))#
phy$root.edge <- end.age - mean.ages[1]#
tip.range <- matrix(0, nrow=2, ncol=Ntip(phy))#
}#
}#
if(analysis.type == "mrbayes") {#
node.hpd <- phy.data#
hpd.ages <- sapply(phy.data$node.ages[,"age_95%HPD"], function(xx) as.numeric(strsplit(xx, ",")[[1]]))#
end.age <- hpd.ages[2,1]#
node.in.ape <- as.numeric(phy.data$node.ages[,"node.name"])#
phy$root.edge <- end.age - nodeTimes(phy)[1,1]#
tip.range <- sapply(phy.data$tip.ages[,"age_95%HPD"], function(xx) as.numeric(strsplit(xx, ",")[[1]]))#
}#
if(analysis.type == "revbayes") {#
node.hpd <- phy.data#
hpd.ages <- sapply(phy.data$node.ages[,"age_95%_HPD"], function(xx) as.numeric(strsplit(xx, ",")[[1]]))#
end.age <- hpd.ages[2,1]#
node.in.ape <- as.numeric(phy.data$node.ages[,"node.name"])#
phy$root.edge <- end.age - nodeTimes(phy)[1,1]#
if(colnames(phy.data$tip.ages) == "age_95%_HPD") {#
tip.range <- sapply(phy.data$tip.ages[,"age_95%_HPD"], function(xx) as.numeric(strsplit(xx, ",")[[1]]))#
} else {#
tip.range <- matrix(0, nrow=2, ncol=Ntip(phy))#
}#
}#
if(analysis.type == "user") {#
node.ages <- lapply(node.ages, function(x) x * time.correction)#
phy$edge.length <- phy$edge.length * time.correction#
hpd.ages <- sapply(node.ages, function(xx) HPDinterval(as.mcmc(xx)))#
end.age <- hpd.ages[2,1]#
node.in.ape <- as.numeric(names(node.ages))#
phy$root.edge <- end.age - nodeTimes(phy)[1,1]#
if(length(node.ages) > (Nnode(phy) + 1)) {#
tip.range <- hpd.ages[-c(1:(Nnode(phy) + 1))]#
} else {#
tip.range <- matrix(0, nrow=2, ncol=Ntip(phy))#
}#
node.estimates <- node.ages#
}#
###########################################
############ plot functions ###############
###########################################
############ phylogram ###############
if(plot.type == "phylogram") {#
plot.phylo(ladderize(phy), type="phylogram", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color, ...)#
last.plot.coord <- get("last_plot.phylo", envir = .PlotPhyloEnv)#
int <- last.plot.coord$yy[last.plot.coord$edge[,1]]#
ext <- last.plot.coord$yy[last.plot.coord$edge[,2]]#
structure <- cbind(int, ext) #
node.in.ape <- c(node.in.ape, 1:Ntip(phy))#
mean.ages <- cbind(end.age - hpd.ages, end.age - tip.range)#
match.nodes <- last.plot.coord$edge[1,1]#
location.here <- match(match.nodes, node.in.ape)#
xx <- mean.ages[,location.here]#
match.edges <- which(last.plot.coord$edge[,1] == Ntip(phy) + 1)#
if(is.null(all.nodes)) {#
ext.node <- which(last.plot.coord$edge[,2] > Ntip(phy))#
if(length(col.age) < length(ext.node)) col.age <- rep(col.age, length(ext.node) + 1)#
if(node.method == "full.length") {#
polygon(c(xx[1], xx[2], xx[2], xx[1]), matrix(c(0, 0, Ntip(phy), Ntip(phy)), ncol=2), col=col.age[1], border=FALSE)#
}#
if(node.method == "node.length") {#
polygon(c(xx[1], xx[2], xx[2], xx[1]), rbind(structure[match.edges,2], structure[match.edges,2]), col=col.age[1], border=FALSE)#
}#
if(node.method == "bar") {#
lines(c(xx[1], xx[2]), rep(structure[1,1], 2), col=col.age[1], lwd=lwd.bar)#
}#
} else {#
ext.node <- match(all.nodes, last.plot.coord$edge[,2])#
if(length(col.age) == length(ext.node)) col.age <- rep(col.age, length(ext.node) + 1)#
}#
counter <- 2#
for(uu in ext.node) {#
match.nodes <- last.plot.coord$edge[uu,2]#
location.here <- match(match.nodes, node.in.ape)#
xx <- mean.ages[,location.here]#
match.edges <- range(which(last.plot.coord$edge[,1] == last.plot.coord$edge[uu,2]))#
if(node.method == "full.length") {#
polygon(c(xx[1], xx[2], xx[2], xx[1]), matrix(c(0, 0, Ntip(phy), Ntip(phy)), ncol=2), col=col.age[counter], border=FALSE)#
}#
if(node.method == "node.length") {#
polygon(c(xx[1], xx[2], xx[2], xx[1]), rbind(structure[match.edges,2], structure[match.edges,2]), col=col.age[counter], border=FALSE)#
}#
if(node.method == "bar") {#
lines(xx, rep(structure[uu,2], 2), col=col.age[counter], lwd=lwd.bar)#
} #
counter = counter + 1#
}#
if(tip.lengths) {#
phy.to <- nodeTimes(phy)[,2]#
tip.here <- which(phy$edge[,2] <= Ntip(phy))#
tip.to.label <- tip.here[which(nodeTimes(phy)[tip.here,2] > 0.01)]#
for(uu in phy$edge[tip.to.label,2] ) {#
match.nodes <- Nnode(phy) + uu#
xx <- mean.ages[,match.nodes]#
match.edges <- last.plot.coord$edge[uu,1]#
yy.loc <- last.plot.coord$yy[uu]#
lines(xx, rep(yy.loc, 2), col=tip.bar.col, lwd=lwd.bar)#
}#
}#
}#
############ cladogram ###############
#
if(plot.type == "cladogram") {#
plot.phylo(ladderize(phy), type="cladogram", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color, ...)#
last.plot.coord <- get("last_plot.phylo", envir = .PlotPhyloEnv)#
int <- last.plot.coord$yy[last.plot.coord$edge[,1]]#
ext <- last.plot.coord$yy[last.plot.coord$edge[,2]]#
structure <- cbind(int, ext)#
node.in.ape <- c(node.in.ape, 1:Ntip(phy))#
mean.ages <- cbind(end.age - hpd.ages, end.age - tip.range)#
match.nodes <- last.plot.coord$edge[1,1]#
location.here <- match(match.nodes, node.in.ape)#
xx <- mean.ages[,location.here]#
match.edges <- which(last.plot.coord$edge[,1] == Ntip(phy) + 1)#
match.nodes <- last.plot.coord$edge[1, 1]#
location.here <- which(match.nodes == last.plot.coord$edge[,1])#
xx <- mean.ages[,1]#
ext.node <- which(last.plot.coord$edge[,2] > Ntip(phy))#
if(length(col.age) < dim(last.plot.coord$edge)[1]) {#
col.age <- rep(col.age, dim(last.plot.coord$edge)[1])#
lines(xx, rep(structure[1,1], 2), lwd=lwd.bar, col=col.age[1])#
} #
counter <- 1#
for(uu in 1:dim(last.plot.coord$edge)[1]) {#
match.nodes <- last.plot.coord$edge[uu, ]#
location.here <- match(match.nodes, node.in.ape)#
xx <- c(mean.ages[,location.here])#
polygon(c(xx[1], xx[3], xx[4], xx[2]), c(structure[uu,], rev(structure[uu,])), col=col.age[counter], border=FALSE)#
counter <- counter + 1#
}#
}#
############ distributions ###############
if(plot.type == "distributions") {#
plot.phylo(ladderize(phy), type="p", edge.col=col.tree, root.edge=TRUE, y.lim=c(t.depth, t.height), cex=cex.tips, show.tip.label=show.tip.label, tip.color=tip.color, ...)#
last.plot.coord <- get("last_plot.phylo", envir = .PlotPhyloEnv)#
last.plot.coord <- get("last_plot.phylo", envir = .PlotPhyloEnv)#
if(analysis.type == "mrbayes") {#
internal.nodes <- unique(phy$edge[,1])#
node.times <- vector("list", length(internal.nodes))#
for(kk in 1:n.trees) {#
tre.in.ape <- read.nexus(gsub("run1", paste0("run", kk), t.name))#
tre.in <- scan(gsub("run1", paste0("run", kk), t.name), what="", sep="\n", quiet=TRUE)#
start.names <- which(regexpr("translate", tre.in) != -1) + 1#
end.names <- which(regexpr("tree gen", tre.in) != -1)[1] - 1#
names.on.tree <- sapply(tre.in[start.names:end.names], function(xx) {#
xx <- gsub(",", "", tail(strsplit(xx, " ")[[1]], 1))#
xx <- gsub(";", "", xx)#
}#
)#
clock.rate.in <- unlist(gregexpr("&clockrate", tre.in))#
clock.rate.loc <- which(clock.rate.in != -1)#
ceiling.rm <- ceiling(length(tre.in.ape) * burn.in)#
tre.in.ape <- tre.in.ape[-c(1:ceiling.rm)]#
clock.rate.loc <- clock.rate.loc[-c(1:ceiling.rm)]#
for(y in 1:length(tre.in.ape)) {#
tree.here <- tre.in[clock.rate.loc[y]]#
new.string <- substring(tree.here, regexpr("&clockrate", tree.here) + 1, ) #
new.string <- substring(new.string, 1, regexpr("[]]", new.string) - 1) #
clock.rate <- as.numeric(strsplit(new.string, "=")[[1]][2])#
tree.in <- read.tree(text=tree.here)#
tree.in$tip.label <- as.character(names.on.tree[as.numeric(tree.in$tip.label)])#
tree.edge.unique <- unique(tree.in$edge[,1])#
node.times.now <- nodeTimes(tree.in) / clock.rate#
parts <- prop.part(tree.in, phy)#
match.on.tree <- which(attributes(parts)$number == 2)#
for(ii in 1:length(match.on.tree)) {#
name.now <- parts[[match.on.tree[ii]]]#
five <- match(tree.edge.unique[match.on.tree[ii]], tree.in$edge[,1])#
node.time.add <- node.times.now[five, 1]#
names.now <- attributes(parts)$labels[name.now]#
mrca.now <- getMRCA(phy, names.now)#
mrca.store <- match(mrca.now, internal.nodes)#
node.times[[mrca.store]] <- c(node.times[[mrca.store]], node.time.add)#
}#
}#
}#
names(node.times) <- internal.nodes#
all.nodes <- (Ntip(phy) + 1 ):(Nnode(phy) + Ntip(phy))#
int <- last.plot.coord$yy[last.plot.coord$edge[,1]]#
ext <- last.plot.coord$yy[last.plot.coord$edge[,2]]#
len.all.nd <- length(all.nodes)#
if(length(density.col) < len.all.nd) density.col <- rep(density.col, len.all.nd)#
if(length(density.border.col) < len.all.nd) density.border.col <- rep(density.border.col, len.all.nd)#
for(k in 1:len.all.nd) {#
location.here <- match(as.numeric(names(node.times)[k]), node.in.ape)#
lower.hpd <- hpd.ages[1, location.here]#
upper.hpd <- hpd.ages[2, location.here]#
mcmc.hpd <- node.times[[k]][intersect(which(node.times[[k]] >= lower.hpd), which(node.times[[k]] <= upper.hpd))]#
mcmc.den <- density(mcmc.hpd)#
xx <- last.plot.coord$xx[1] - mcmc.den$x#
lower.y <- last.plot.coord$yy[all.nodes[k]]#
upper.y <- max(ext[which(all.nodes[k] == last.plot.coord$edge[,1])]) #
scale.den <- (upper.y - lower.y) * distribution.height#
y.scale <- mcmc.den$y * (scale.den / max(mcmc.den$y))#
yy <- y.scale + lower.y#
polygon(xx, yy, col=density.col[k], border=density.border.col[k])#
}#
}#
if(analysis.type == "revbayes") {#
internal.nodes <- unique(phy$edge[,1])#
node.times <- vector("list", length(internal.nodes))#
slash <- max(gregexpr("/", directory.files)[[1]])#
path.to.files <- substring(directory.files, 1, slash)#
log.files <- list.files(path=path.to.files, pattern=".trees")#
n.trees <- length(log.files)#
for(kk in 1:n.trees) {#
tre.in.ape <- read.tree(paste0(path.to.files, "/", log.files[kk]))#
ceiling.rm <- ceiling(length(tre.in.ape) * burn.in)#
tre.in.ape <- tre.in.ape[-c(1:ceiling.rm)]#
for(y in 1:length(tre.in.ape)) {#
tree.in <- tre.in.ape[[y]]#
tree.edge.unique <- unique(tree.in$edge[,1])#
node.times.now <- nodeTimes(tree.in)#
parts <- prop.part(tree.in, phy)#
match.on.tree <- which(attributes(parts)$number == 2)#
for(ii in 1:length(match.on.tree)) {#
name.now <- parts[[match.on.tree[ii]]]#
five <- match(tree.edge.unique[match.on.tree[ii]], tree.in$edge[,1])#
node.time.add <- node.times.now[five, 1]#
names.now <- attributes(parts)$labels[name.now]#
mrca.now <- getMRCA(phy, names.now)#
mrca.store <- match(mrca.now, internal.nodes)#
node.times[[mrca.store]] <- c(node.times[[mrca.store]], node.time.add)#
}#
}#
}#
names(node.times) <- internal.nodes#
all.nodes <- (Ntip(phy) + 1 ):(Nnode(phy) + Ntip(phy))#
int <- last.plot.coord$yy[last.plot.coord$edge[,1]]#
ext <- last.plot.coord$yy[last.plot.coord$edge[,2]] #
len.all.nd <- length(all.nodes)#
if(length(density.col) < len.all.nd) density.col <- rep(density.col, len.all.nd)#
if(length(density.border.col) < len.all.nd) density.border.col <- rep(density.border.col, len.all.nd)#
for(k in 1:len.all.nd) {#
location.here <- match(as.numeric(names(node.times)[k]), node.in.ape)#
lower.hpd <- hpd.ages[1, location.here]#
upper.hpd <- hpd.ages[2, location.here]#
mcmc.hpd <- node.times[[k]][intersect(which(node.times[[k]] >= lower.hpd), which(node.times[[k]] <= upper.hpd))]#
mcmc.den <- density(mcmc.hpd)#
xx <- last.plot.coord$xx[1] - mcmc.den$x#
lower.y <- last.plot.coord$yy[all.nodes[k]]#
upper.y <- max(ext[which(all.nodes[k] == last.plot.coord$edge[,1])]) #
scale.den <- (upper.y - lower.y) * distribution.height#
y.scale <- mcmc.den$y * (scale.den / max(mcmc.den$y))#
yy <- y.scale + lower.y#
polygon(xx, yy, col=density.col[k], border=density.border.col[k])#
}#
} #
#
if(analysis.type == "mcmctree") {#
all.nodes <- (Ntip(phy) + 1 ):(Nnode(phy) + Ntip(phy))#
int <- last.plot.coord$yy[last.plot.coord$edge[,1]]#
ext <- last.plot.coord$yy[last.plot.coord$edge[,2]] #
len.all.nd <- length(all.nodes)#
if(length(density.col) < len.all.nd) density.col <- rep(density.col, len.all.nd)#
if(length(density.border.col) < len.all.nd) density.border.col <- rep(density.border.col, len.all.nd)#
for(k in 1:len.all.nd) {#
lower.hpd <- hpd.ages[1, k]#
upper.hpd <- hpd.ages[2, k]#
mcmc.hpd <- node.estimates[,k][intersect(which(node.estimates[,k] >= lower.hpd), which(node.estimates[,k] <= upper.hpd))]#
mcmc.den <- density(mcmc.hpd)#
xx <- last.plot.coord$xx[1] - mcmc.den$x#
lower.y <- last.plot.coord$yy[all.nodes[k]]#
upper.y <- max(ext[which(all.nodes[k] == last.plot.coord$edge[,1])]) #
scale.den <- (upper.y - lower.y) * distribution.height#
y.scale <- mcmc.den$y * (scale.den / max(mcmc.den$y))#
yy <- y.scale + lower.y#
polygon(xx, yy, col=density.col[k], border=density.border.col[k])#
}#
}#
if(analysis.type == "user") {#
all.nodes <- (Ntip(phy) + 1 ):(Nnode(phy) + Ntip(phy))#
int <- last.plot.coord$yy[last.plot.coord$edge[,1]]#
ext <- last.plot.coord$yy[last.plot.coord$edge[,2]]#
len.all.nd <- length(all.nodes)#
if(length(density.col) < len.all.nd) density.col <- rep(density.col, len.all.nd)#
if(length(density.border.col) < len.all.nd) density.border.col <- rep(density.border.col, len.all.nd)#
for(k in 1:len.all.nd) {#
lower.hpd <- hpd.ages[1, k]#
upper.hpd <- hpd.ages[2, k]#
node.now <- node.estimates[[k]]#
mcmc.hpd <- node.now[intersect(which(node.now >= lower.hpd), which(node.now <= upper.hpd))]#
mcmc.den <- density(mcmc.hpd)#
xx <- last.plot.coord$xx[1] - mcmc.den$x#
lower.y <- last.plot.coord$yy[all.nodes[k]]#
upper.y <- max(ext[which(last.plot.coord$edge[,1] == all.nodes[k])])#
scale.den <- (upper.y - lower.y) * distribution.height#
y.scale <- mcmc.den$y * (scale.den / max(mcmc.den$y))#
yy <- y.scale + lower.y#
polygon(xx, yy, col=density.col[k], border=density.border.col[k])#
}#
}#
}#
###########################################
############ add time scale ###############
###########################################
#
if(add.time.scale) {#
xlimit <- last.plot.coord$xx#
total.length <- max.xlim <- max(xlimit)#
eon.bin <- .bincode(max.xlim, Eon)#
period.bin <- .bincode(max.xlim, Period)#
epoch.bin <- .bincode(max.xlim, Epoch)#
if(is.na(epoch.bin)) epoch.bin <- length(Epoch)#
age.bin <- .bincode(max.xlim, Age)#
if(is.na(age.bin)) age.bin <- length(Age)#
bin.eon <- max.xlim - c(Eon[1:eon.bin], max.xlim)#
bin.period <- max.xlim - c(Period[1:period.bin], max.xlim)#
bin.epoch <- max.xlim - c(Epoch[1:epoch.bin], max.xlim)#
bin.age <- max.xlim - c(Age[1:age.bin], max.xlim)#
plot.bar <- function(bin.data, data.colour, name.data, ht, ht.pl, grey.line=FALSE, cex.labels.int) {#
names.in <- name.data[1:(length(bin.data) - 1)]#
for(u in 2:length(bin.data)) {#
polygon(rep(bin.data[(u):(u-1)], each=2),c(heights[ht:ht.pl], rev(heights[ht:ht.pl])), xpd=TRUE, col=data.colour[c(u-1)], border=FALSE)#
bin.length <- abs(diff(bin.data[(u):(u-1)]))#
percent <- bin.length / total.length#
if(percent >= 0.4) {#
text(mean(bin.data[(u):(u-1)]), mean(heights[ht:ht.pl]), names.in[c(u-1)], cex=cex.labels.int)#
} #
if(percent >= 0.1 && percent < 0.4) {#
name.in <- paste0(paste0(strsplit(names.in[c(u-1)], "")[[1]][1:2], collapse=""), ".")#
text(mean(bin.data[(u):(u-1)]), mean(heights[ht:ht.pl]), name.in, cex=cex.labels.int)#
}#
if(percent < 0.1 && percent >= 0.04) {#
name.in <- paste0(paste0(strsplit(names.in[c(u-1)], "")[[1]][1], collapse=""), ".")#
text(mean(bin.data[(u):(u-1)]), mean(heights[ht:ht.pl]), name.in, cex=cex.labels.int)#
} #
if(grey.line) {#
if(u %% 2 == 0) polygon(rep(bin.data[(u):(u-1)], each=2),c(0.5, Ntip(phy), Ntip(phy), 0.5), xpd=TRUE, col="#00000020", border=FALSE)#
} #
}#
}#
plot.time.scale <- function(level, start.one, end.one, time.check=FALSE, grey.now=FALSE, cex.age.int) {#
if(level == "Eon") {#
plot.bar(bin.eon, Eon.colour, names(Eon.colour),start.one,end.one, grey.line=grey.now, cex.labels.int=cex.labels)#
if(label.timescale.names) text(total.length, mean(heights[start.one:end.one]), "Eon", pos=4, font=3, cex=cex.labels)#
if(time.check) {#
if(is.null(pos.age)) pos.age <- Ntip(phy) + (Ntip(phy) / 100)#
axis(3, at=bin.eon, labels=signif(total.length-bin.eon, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age, tck=tck.abs.age, ...)#
if(label.timescale.names) text(total.length, Ntip(phy) + (Ntip(phy) / 50), "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,1), xpd=TRUE)#
}#
}#
if(level == "Period") {#
plot.bar(bin.period, Period.colour, names(Period.colour),start.one,end.one, grey.line=grey.now, cex.labels.int=cex.labels)#
if(label.timescale.names) text(total.length, mean(heights[start.one:end.one]), "Period", pos=4, font=3, cex=cex.labels)#
if(time.check) {#
if(is.null(pos.age)) pos.age <- Ntip(phy) + (Ntip(phy) / 100)#
axis(3, at=bin.period, labels=signif(total.length-bin.period, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age, tck=tck.abs.age, ...)#
if(label.timescale.names) text(total.length, Ntip(phy) + (Ntip(phy) / 50), "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,1), xpd=TRUE)#
}#
}#
if(level == "Epoch") {#
plot.bar(bin.epoch, Epoch.colour, names(Epoch.colour),start.one,end.one, grey.line=grey.now, cex.labels.int=cex.labels)#
if(label.timescale.names) text(total.length, mean(heights[start.one:end.one]), "Epoch", pos=4, font=3, cex=cex.labels)#
if(time.check) {#
if(is.null(pos.age)) pos.age <- Ntip(phy) + (Ntip(phy) / 100)#
axis(3, at=bin.epoch, labels=signif(total.length-bin.epoch, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age, tck=tck.abs.age, ...)#
if(label.timescale.names) text(total.length, Ntip(phy) + (Ntip(phy) / 50), "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,1), xpd=TRUE)#
}#
}#
if(level == "Age") {#
plot.bar(bin.age, Age.colour, names(Age.colour),start.one,end.one, grey.line=grey.now, cex.labels.int=cex.labels)#
if(label.timescale.names) text(total.length, mean(heights[start.one:end.one]), "Age", pos=4, font=3, cex=cex.labels)#
if(time.check) {#
if(is.null(pos.age)) pos.age <- Ntip(phy) + (Ntip(phy) / 100)#
axis(3, at=bin.age, labels=signif(total.length-bin.age, 2), line=abs.age.line, lwd=abs.age.lwd, lwd.ticks=abs.age.lwd.ticks, mgp=abs.age.mgp, cex.axis=cex.age.int, pos=pos.age, tck=tck.abs.age, ...)#
if(label.timescale.names) text(total.length, Ntip(phy) + (Ntip(phy) / 50), "Ma", pos=4, font=4, cex=cex.labels, adj=c(0,1), xpd=TRUE)#
}#
}#
}#
start.one <- 1#
end.one <- 2#
if(length(add.abs.time) == 1 && add.abs.time[1] == TRUE) {#
add.abs.time <- rep(FALSE, length(scale.res))#
add.abs.time[length(add.abs.time)] <- TRUE#
}#
if(length(add.abs.time) == 1 && add.abs.time[1] == FALSE) {#
add.abs.time <- rep(FALSE, length(scale.res))#
}#
if(grey.bars) {#
grey.now.int <- rep(FALSE, length(scale.res))#
if(!any(add.abs.time)) {#
add.time <- length(scale.res)#
} else {#
add.time <- which(add.abs.time) #
}#
grey.now.int[add.time] <- TRUE#
}#
for(uu in 1:length(scale.res)) {#
plot.time.scale(scale.res[uu], time.check=add.abs.time[uu], start.one=start.one, end.one=end.one, grey.now=grey.now.int[uu], cex.age.int=cex.age)#
start.one <- start.one + 1#
end.one <- end.one + 1#
}#
}#
}
rm(list=ls(all=T))#
library("devtools")#
library(roxygen2)#
setwd("~/Documents/MCMCTreeR_parent/MCMCTreeR")#
document()#
setwd("..")#
install("MCMCTreeR")
rm(list=ls(all=TRUE))#
library(MCMCTreeR)#
library(coda)#
library(motmot.2.0)#
#
# phy <- readMCMCTree("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre")#
# mcmctree.posterior <- read.table("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/mcmc.out", header=TRUE)#
# mcmctreer.phy <- scan("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre", what="", sep="\t", quiet=TRUE)#
# mcmctree.output <- list(mcmctreer.phy, mcmctree.posterior)#
# names(mcmctree.output) <- c("mcmctreer.phy", "mcmctree.posterior")#
# save(mcmctree.output, file='~/Documents/MCMCTreeR_parent/MCMCTreeR/data/mcmctree.output.RData')#
#
data(mcmctree.output)#
attach(mcmctree.output)#
names(mcmctree.output)
phy <- readMCMCTree(mcmctreer.phy, from.file=FALSE)
node.colours <- rep("#ff008050", Nnode(phy$apePhy))#
node.colours.border <- rep("#ff008090", Nnode(phy$apePhy))#
node.colours[1] <- "#00008050"#
node.colours.border[1] <- "#00008090"#
#
mcmc.tree.plot(phy, mcmc.chain=mcmctree.output$mcmctree.posterior, cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period", "Epoch", "Age"), plot.type="distributions", cex.age=0.4, cex.labels=0.5, relative.height=0.08, build.tree=FALSE, col.tree="grey40", col.age="black", density.col=node.colours, density.border.col=node.colours.border)
mcmc.tree.plot(phy, analysis.type="mcmctree", cex.tips=0.33, time.correction=100, plot.type="phylogram", lwd.bar=2, add.time.scale=FALSE, node.method="bar", col.age="navy", no.margin=TRUE)
rm(list=ls(all=T))#
library("devtools")#
library(roxygen2)#
setwd("~/Documents/MCMCTreeR_parent/MCMCTreeR")#
document()#
setwd("..")#
install("MCMCTreeR")
rm(list=ls(all=TRUE))#
library(MCMCTreeR)#
library(coda)#
library(motmot.2.0)
data(mcmctree.output)#
attach(mcmctree.output)#
names(mcmctree.output)#
##### sort up top of plot t depth#
#
phy <- readMCMCTree(mcmctreer.phy, from.file=FALSE)
?mcmc.tree.plot
args(mcmc.tree.plot)
mcmc.tree.plot(phy, mcmc.chain=mcmctree.posterior, cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period", "Epoch", "Age"), plot.type="distributions", cex.age=0.4, cex.labels=0.5, relative.height=0.08, build.tree=FALSE, col.tree="grey40", col.age="black", density.col=node.colours, density.border.col=node.colours.border, label.offset=4, no.margin=TRUE)
node.colours <- rep("#ff008050", Nnode(phy$apePhy))#
node.colours.border <- rep("#ff008090", Nnode(phy$apePhy))#
mcmc.tree.plot(phy, mcmc.chain=mcmctree.output$mcmctree.posterior, cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period"), plot.type="distributions", cex.age=0.4, cex.labels=0.5, relative.height=0.08, build.tree=FALSE, col.tree="grey40", col.age="black", density.col=node.colours, density.border.col=node.colours.border)
mcmc.tree.plot(phy, mcmc.chain=mcmctree.posterior, cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period", "Epoch", "Age"), plot.type="distributions", cex.age=0.4, cex.labels=0.5, relative.height=0.08, build.tree=FALSE, col.tree="grey40", label.offset=4, no.margin=TRUE)
mcmc.tree.plot(phy, mcmc.chain=mcmctree.posterior, cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period"), plot.type="distributions", cex.age=0.4, cex.labels=0.5, relative.height=0.08, build.tree=FALSE, col.tree="grey40", label.offset=4, no.margin=TRUE)
## set up node ages manually#
all.nodes <- as.numeric(gsub("t_n", "", colnames(mcmc.chain)[2:Ntip(phy)]))#
mcmc.chain.age <- mcmc.chain[ , 2:Ntip(phy)]#
node.ages <- vector("list", Nnode(phy))#
names(node.ages)[1:Nnode(phy)] <- c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])#
for(uu in 1:length(all.nodes)) node.ages[[uu]] <- mcmc.chain.age[,uu]
phy.edge <- phy$apePhy$edge#
phy <- phy$apePhy#
mcmc.chain <- mcmctree.output$mcmctree.posterior#
#
## set up node ages manually#
all.nodes <- as.numeric(gsub("t_n", "", colnames(mcmc.chain)[2:Ntip(phy)]))#
mcmc.chain.age <- mcmc.chain[ , 2:Ntip(phy)]#
node.ages <- vector("list", Nnode(phy))#
names(node.ages)[1:Nnode(phy)] <- c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])#
for(uu in 1:length(all.nodes)) node.ages[[uu]] <- mcmc.chain.age[,uu]
mcmc.tree.plot(phy=phy, node.ages=node.ages, analysis.type="user", cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period"), plot.type="phylogram", cex.age=0.4, cex.labels=0.5, relative.height=0.08, build.tree=FALSE, col.tree="grey40", col.age="black", density.col=node.colours, density.border.col=node.colours.border)
mcmc.tree.plot(phy=phy, node.ages=node.ages, analysis.type="user", cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period"), plot.type="distributions", cex.age=0.4, cex.labels=0.5, relative.height=0.08, build.tree=FALSE, col.tree="grey40", col.age="black", density.col=node.colours, density.border.col=node.colours.border)
## set up node ages manually#
all.nodes <- as.numeric(gsub("t_n", "", colnames(mcmc.chain)[2:Ntip(phy)]))#
mcmc.chain.age <- mcmc.chain[ , 2:Ntip(phy)]#
node.ages <- vector("list", Nnode(phy))#
names(node.ages)[1:Nnode(phy)] <- c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])#
for(uu in 1:length(all.nodes)) node.ages[[uu]] <- mcmc.chain.age[,uu]#
#
mcmc.tree.plot(phy=phy, node.ages=node.ages, analysis.type="user", cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period"), plot.type="distributions", cex.age=0.4, cex.labels=0.5, relative.height=0.08, col.tree="grey40")
all.nodes
mcmc.chain[ , 2:Ntip(phy)]
c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])
all.nodes <- as.numeric(gsub("t_n", "", colnames(mcmc.chain)[2:Ntip(phy)]))
mcmctreer.phy
phy
phy.edge <- phy$edge
readMCMCTree(mcmctreer.phy, from.file=FALSE)
phy <- readMCMCTree(mcmctreer.phy, from.file=FALSE)
phy$apePhy
names(node.ages)[1:Nnode(phy)] <- c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])
names(node.ages) <- c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])
phy <- phy$apePhy#
phy.edge <- phy$edge#
mcmc.chain <- mcmctree.posterior#
#
## set up node ages manually#
all.nodes <- as.numeric(gsub("t_n", "", colnames(mcmc.chain)[2:Ntip(phy)]))#
mcmc.chain.age <- mcmc.chain[ , 2:Ntip(phy)]#
node.ages <- vector("list", Nnode(phy))#
names(node.ages) <- c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])
names(node.ages)
match(all.nodes, as.numeric(names(node.ages)))
diff(match(all.nodes, as.numeric(names(node.ages))))
is.unsorted(match(all.nodes, as.numeric(names(node.ages))))
!is.unsorted(match(all.nodes, as.numeric(names(node.ages))))
match(all.nodes, as.numeric(names(node.ages)))
match.nodes <- match(all.nodes, as.numeric(names(node.ages)))
!is.unsorted(match.nodes)
all.nodes <- all.nodes[,match.nodes]
## set up node ages manually#
all.nodes <- as.numeric(gsub("t_n", "", colnames(mcmc.chain)[2:Ntip(phy)]))#
mcmc.chain.age <- mcmc.chain[ , 2:Ntip(phy)]#
node.ages <- vector("list", Nnode(phy))#
names(node.ages) <- c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])#
#
match.nodes <- match(all.nodes, as.numeric(names(node.ages)))#
!is.unsorted(match.nodes)#
all.nodes <- all.nodes[,match.nodes]
all.nodes <- mcmc.chain.age[,match.nodes]
sapply(match.nodes)
node.ages <- lapply(match.nodes, function(uu) mcmc.chain.age[,uu])
## set up node ages manually#
all.nodes <- as.numeric(gsub("t_n", "", colnames(mcmc.chain)[2:Ntip(phy)]))#
mcmc.chain.age <- mcmc.chain[ , 2:Ntip(phy)]#
node.ages.names <- c(Ntip(phy) +1, phy.edge[which(phy.edge[,2] > Ntip(phy)),2])#
#
match.nodes <- match(all.nodes, as.numeric(node.ages.names))#
!is.unsorted(match.nodes)#
#
node.ages <- lapply(match.nodes, function(uu) mcmc.chain.age[,uu])
names(node.ages) <- node.ages.names
node.ages
mcmc.tree.plot(phy=phy, node.ages=node.ages, analysis.type="user", cex.tips=0.33, time.correction=100, scale.res=c("Eon", "Period"), plot.type="distributions", cex.age=0.4, cex.labels=0.5, relative.height=0.08, col.tree="grey40")
3463.56 * 0.25
3463.56 * 0.2
3463.56 * 0.
865.89 + 3463.56
26000-10000
4329.45 + 16000
4329.45 + 16000 + 501
26501
26501 + 4329.45
26501 + 4329.45 + 9000
26501 + 4329.45 + 900
14.999 * 2.6050
4329.45 + 16000 + 501 + 915
rm(list=ls(all=TRUE))#
library(MCMCTreeR)#
library(coda)#
library(motmot.2.0)#
#
# phy <- readMCMCTree("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre")#
# mcmctree.posterior <- read.table("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/mcmc.out", header=TRUE)#
# mcmctreer.phy <- scan("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre", what="", sep="\t", quiet=TRUE)#
# mcmctree.output <- list(mcmctreer.phy, mcmctree.posterior)#
# names(mcmctree.output) <- c("mcmctreer.phy", "mcmctree.posterior")#
# save(mcmctree.output, file='~/Documents/MCMCTreeR_parent/MCMCTreeR/data/mcmctree.output.RData')#
#
data(mcmctree.output)#
attach(mcmctree.output)#
names(mcmctree.output)
phy <- readMCMCTree(mcmctreer.phy, from.file=FALSE)#
#
mcmc.tree.plot(phy, analysis.type="mcmctree", cex.tips=0.33, time.correction=100, plot.type="phylogram", lwd.bar=2, add.time.scale=FALSE, node.method="bar", col.age="navy", no.margin=TRUE)
mcmc.tree.plot(phy, analysis.type="mcmctree", cex.tips=0.33, time.correction=100, plot.type="phylogram", lwd.bar=2, add.time.scale=FALSE, node.method="node.length", col.age="#008b0040")
mcmc.tree.plot(phy, analysis.type="mcmctree", cex.tips=0.33, time.correction=100, plot.type="phylogram", lwd.bar=2, add.time.scale=FALSE, node.method="full.length", all.nodes=110, col.age="#ff000040")
mcmc.tree.plot(phy, analysis.type="mcmctree", cex.tips=0.33, time.correction=100, plot.type="cladograme", lwd.bar=2, add.time.scale=FALSE, node.method="node.length", col.age="#008b0040")
mcmc.tree.plot(phy, analysis.type="mcmctree", cex.tips=0.33, time.correction=100, plot.type="cladogram", lwd.bar=2, add.time.scale=FALSE, node.method="node.length", col.age="#008b0040")
?mcmc.tree.plot
rm(list=ls(all=T))#
library("devtools")#
library(roxygen2)#
setwd("~/Documents/MCMCTreeR_parent/MCMCTreeR")#
document()#
setwd("..")#
install("MCMCTreeR")
rm(list=ls(all=TRUE))#
library(MCMCTreeR)#
library(coda)#
library(motmot.2.0)#
#
# phy <- readMCMCTree("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre")#
# mcmctree.posterior <- read.table("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/mcmc.out", header=TRUE)#
# mcmctreer.phy <- scan("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre", what="", sep="\t", quiet=TRUE)#
# mcmctree.output <- list(mcmctreer.phy, mcmctree.posterior)#
# names(mcmctree.output) <- c("mcmctreer.phy", "mcmctree.posterior")#
# save(mcmctree.output, file='~/Documents/MCMCTreeR_parent/MCMCTreeR/data/mcmctree.output.RData')#
#
data(mcmctree.output)#
attach(mcmctree.output)#
names(mcmctree.output)#
phy <- readMCMCTree(mcmctreer.phy, from.file=FALSE)
dev.new()
mcmc.tree.plot(phy, mcmc.chain=mcmctree.posterior, cex.tips=0.35, time.correction=100, plot.type="phylogram", cex.age=0.7, cex.labels=0.7, relative.height=0.08, col.tree="black", scale.res=c("Eon", "Period"), no.margin=TRUE, label.offset=4, col.age = "#00008098", label.timescale.names=TRUE, lwd.bar=2)
rm(list=ls(all=T))#
library("devtools")#
library(roxygen2)#
setwd("~/Documents/MCMCTreeR_parent/MCMCTreeR")#
document()#
setwd("..")#
install("MCMCTreeR")
rm(list=ls(all=TRUE))#
library(MCMCTreeR)#
library(coda)#
library(motmot.2.0)#
#
# phy <- readMCMCTree("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre")#
# mcmctree.posterior <- read.table("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/mcmc.out", header=TRUE)#
# mcmctreer.phy <- scan("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre", what="", sep="\t", quiet=TRUE)#
# mcmctree.output <- list(mcmctreer.phy, mcmctree.posterior)#
# names(mcmctree.output) <- c("mcmctreer.phy", "mcmctree.posterior")#
# save(mcmctree.output, file='~/Documents/MCMCTreeR_parent/MCMCTreeR/data/mcmctree.output.RData')#
#
data(mcmctree.output)#
attach(mcmctree.output)#
names(mcmctree.output)#
phy <- readMCMCTree(mcmctreer.phy, from.file=FALSE)
mcmc.tree.plot(phy, mcmc.chain=mcmctree.posterior, cex.tips=0.35, time.correction=100, plot.type="phylogram", cex.age=0.7, cex.labels=0.7, relative.height=0.08, col.tree="black", scale.res=c("Eon", "Period"), no.margin=TRUE, label.offset=4, col.age = "#00008098", label.timescale.names=TRUE, lwd.bar=2)
rm(list=ls(all=T))#
library("devtools")#
library(roxygen2)#
setwd("~/Documents/MCMCTreeR_parent/MCMCTreeR")#
document()#
setwd("..")#
install("MCMCTreeR")
rm(list=ls(all=TRUE))#
library(MCMCTreeR)#
library(coda)#
library(motmot.2.0)#
#
# phy <- readMCMCTree("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre")#
# mcmctree.posterior <- read.table("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/mcmc.out", header=TRUE)#
# mcmctreer.phy <- scan("~/Documents/uob/Postdoc/WickettTrees/datingAnalyses/results/alternativeTopology1701170/monophyletic/FigTree.tre", what="", sep="\t", quiet=TRUE)#
# mcmctree.output <- list(mcmctreer.phy, mcmctree.posterior)#
# names(mcmctree.output) <- c("mcmctreer.phy", "mcmctree.posterior")#
# save(mcmctree.output, file='~/Documents/MCMCTreeR_parent/MCMCTreeR/data/mcmctree.output.RData')#
#
data(mcmctree.output)#
attach(mcmctree.output)#
names(mcmctree.output)#
phy <- readMCMCTree(mcmctreer.phy, from.file=FALSE)
dev.new()
mcmc.tree.plot(phy, mcmc.chain=mcmctree.posterior, cex.tips=0.35, time.correction=100, plot.type="phylogram", cex.age=0.7, cex.labels=0.7, relative.height=0.08, col.tree="black", scale.res=c("Eon", "Period"), no.margin=TRUE, label.offset=4, col.age = "#00008098", label.timescale.names=TRUE, lwd.bar=2)
#' Plot time-scaled phylogenies#
#'#
#' Plot time-scaled phylogenies with node uncertainty and timescale#
#' @param phy A timescaled phylogeny, unless analysis.type="mcmctree" and build.tree=TRUE#
#' @param analysis.type The method used to generate the time-scale tree, one of mcmctree, MrBayes, RevBayes, or User.#
#' @param mcmc.chain The full posterior of age estimates for all nodes (default NULL)#
#' @param node.ages User-supplied node ages applicable for analysis.type user#
#' @param directory.files The directory for files to summarise for MrBayes and RevBayes analyses#
#' @param plot.type The plotting method for the phylogram corresponding to the APE definition. Phylogram is available for all analysis types, but cladogram is only avilable for mcmctree analyses at present. Type distributions plots a phylogram with density distributions on each of the nodes.#
#' @param build.tree Logical. Only applicable to mcmctree analyses, whether to timescale the phylogeny based on the full mcmc chain#
#' @param node.method For plot.type phylogram the method to dispay age uncertainty on each node, either bar, node.length, or full.length. If 'none' is supplied plotting node uncertainty is suppressed. #
#' @param all.nodes If NULL (default) node uncertainty is plotted on all nodes. If node numbers are supplied, only these nodes will be labelled with uncertainty.#
#' @param add.time.scale Logical. Adds a timescale to the plotted phylogeny.#
#' @param add.abs.time Logical. Adds an absolute timescale alongside the geological timescale. Only applicable if add.time.scale is TRUE#
#' @param scale.res The geological age designation to add to the plot can be one or a combination of Eon, Period, Epoch, Age. The order of plot (from bottom to top) is the same as the supplied order. Subsequent arguments add.abs.time and grey.bars are based on the last supplied age designation.#
#' @param label.timescale.names Logical. Add names (Eon, Period, Epoch, Age, Ma) to axis if timescale used#
#' @param time.correction Number to place branch lengths and age estimates in absolute time, deafult to one.#
#' @param col.age The colouring of the node.method bars to summarise node age uncertainty#
#' @param tip.lengths Logical. If the tree contains non-present tip ages, the age uncertainty associated with them will be added to the plot#
#' @param density.col Colour of the node distributions (if analysis.type=distributions)#
#' @param density.border.col Colour of the node distributions (if analysis.type=distributions) borders#
#' @param cex.tips Size of the phylogeny tip labels#
#' @param show.tip.label Logical. Should the tree tip labels be displayed#
#' @param col.tree Colour of the phylogeny edges#
#' @param tip.color Colour of the phylogeny tip labels#
#' @param lwd.bar Width of the bar to summarise age uncertainty, applicable only if plot.type is phylogram and node.method is bar#
#' @param grey.bars Logical. Should grey bars be used to signify time bins, applicable only if add.time.scale is TRUE#
#' @param cex.age Size of the labels for the absolute timescale#
#' @param cex.labels Size of the labels for the geological timescale#
#' @param relative.height the relative height of the timescale labels#
#' @param ntrees For analysis.type mrbayes, the number of independent chains to summarise#
#' @param tip.bar.col The colour of uncertainty around non-contemporary tips#
#' @param burn.in The number of points in the chain to discard for MrBayes and RevBayes analyses#
#' @param distribution.height The relative height of node distributions when plot.type is distributions measured as the relative height of the descendent node#
#' @param abs.age.mgp mgp values for the absolute age axis, only applicable if add.abs.time is TRUE#
#' @param abs.age.lwd.ticks lwd values for the absolute age axis ticks, only applicable if add.abs.time is TRUE#
#' @param abs.age.lwd lwd values for the absolute age axis horizontal line, only applicable if add.abs.time is TRUE#
#' @param tck.abs.age tck values for the absolute age axis tick height, only applicable if add.abs.time is TRUE#
#' @param abs.age.line line correction for the absolute age axis tick, only applicable if add.abs.time is TRUE#
#' @param pos.age position position of absolute age axis#
#' @param ... further arguments to be used in plot.phylo#
#' @return If plot=TRUE plot of distributions in file 'pdfOutput' written to current working directory#
#' @export#
#' @examples#
#' # example#
#
mcmc.tree.plot <- function(phy=NULL, analysis.type="mcmctree", mcmc.chain=NULL, node.ages=NULL, directory.files=NULL, plot.type="phylogram", build.tree=FALSE, node.method="bar", all.nodes=NULL, add.time.scale=TRUE, add.abs.time=TRUE, scale.res="Epoch", label.timescale.names=FALSE, time.correction=1, col.age="blue", tip.lengths=FALSE, density.col="#00000050", density.border.col="#00000080", cex.tips=1, show.tip.label=TRUE, col.tree="black", tip.color="black", lwd.bar=1, grey.bars=TRUE, cex.age=1, cex.labels=1, relative.height=0.08, n.trees=2, tip.bar.col="#ff000050", burn.in=0.25, distribution.height=0.8, abs.age.mgp=c(3, 0.35, 0), abs.age.lwd.ticks=0.7, abs.age.lwd=0, tck.abs.age=-0.01, abs.age.line=-0.4, pos.age=NULL, ...) {#
if(is.na(match(tolower(analysis.type), c("mcmctree", "mrbayes", "revbayes", "user")))) {#
stop("analysis.type must be one of 'mcmctree', 'mrbayes', 'revbayes', or 'user'")#
}#
if(any(is.na(match(scale.res, c("Eon", "Period", "Epoch", "Age"))))) {#
stop("scale.res must be one/combination of 'Eon', 'Period', 'Epoch', or 'Age'")#
}#
mb.tree <- TRUE#
if(is.numeric(match(tolower(analysis.type), "mrbayes"))) mb.tree <- FALSE#
if(mb.tree && plot.type == "distributions" && is.null(mcmc.chain)) stop("The distribution plot.type options needs the full mcmc.chain")#
if(analysis.type == "mcmctree") {#
phy.in <- phy#
phy <- phy.in[[1]]#
}#
if(analysis.type == "mrbayes") {#
phy.data <- read.in.mrbayes(directory.files)#
t.name <- gsub(".con.tre", ".run1.t", directory.files)#
phy <- phy.data$phy#
if(plot.type == "cladogram") stop("sorry plot type 'cladogram' only avilable for mcmctree trees")#
}#
if(analysis.type == "revbayes") {#
phy.data <- read.in.revbayes(directory.files)#
phy <- phy.data$phy#
if(plot.type == "cladogram") stop("sorry plot type 'cladogram' only avilable for mcmctree trees")#
}#
if(analysis.type == "user") {#
if(plot.type == "cladogram") stop("sorry plot type 'cladogram' only avilable for mcmctree trees")#
} #
if(add.time.scale) {#
highest.level <- length(scale.res)#
check.level <- any(is.na(match(scale.res, c("Eon", "Period", "Epoch", "Age"))))#
if(is.na(check.level)) stop("scale.res must be one of Eon, Period, Epoch, Age")#
each.height <- -(relative.height * Ntip(phy)) / highest.level#
t.depth <- (each.height * highest.level)#
t.height <- Ntip(phy) + (relative.height * Ntip(phy))#
heights <- seq(from=t.depth, to=0.5, length.out=highest.level + 1)#
} else {#
t.depth <- 0#
t.height <- Ntip(phy)#
}#
if(analysis.type == "mcmctree") {#
if(!is.null(mcmc.chain)) {#
internal.node.local <- which(regexpr("t_n", names(mcmc.chain)) != -1)#
node.estimates <- mcmc.chain[,internal.node.local] * time.correction#
phy.matrix <- matrix(NA, ncol=2, nrow=Ntip(phy) * 2 - 2)#
node.in.ape <- as.numeric(gsub("t_n", "", names(node.estimates)))#
hpd.ages <- apply(node.estimates, 2, function(x) HPDinterval(as.mcmc(x))) #
mean.ages <- apply(node.estimates, 2, function(x) mean(x))#
max.ages <- apply(node.estimates, 2, function(x) max(x))#
end.age <- hpd.ages[2,1]#
if(build.tree) {#
for(ii in 1:length(mean.ages)) phy.matrix[which(node.in.ape[ii] == phy$edge[,1]), 1] <- mean.ages[ii]#
ext.node <- which(phy$edge[,2] > Ntip(phy))#
phy.matrix[ext.node, 2] <- mean.ages[-1]#
phy.matrix[-ext.node, 2] <- 0#
b.lengths <- phy.matrix[,1] - phy.matrix[,2]#
phy$edge.length <- b.lengths#
phy$root.edge <- end.age - phy.matrix[1,1]#
} else {#
phy$edge.length <- phy$edge.length * time.correction#
phy$root.edge <- end.age - nodeTimes(phy)[1,1]#
}#
tip.range <- matrix(0, nrow=2, ncol=Ntip(phy))#
} else {#
phy$edge.length <- phy$edge.length * time.correction#
phy.in$nodeAges <- phy.in$nodeAges * time.correction#
hpd.ages <- t(phy.in$nodeAges[,-1])#
mean.ages <- phy.in$nodeAges[,1]#
end.age <- hpd.ages[2,1]#
node.in.ape <- as.numeric(rownames(phy.in$nodeAges))#
ext.node <- which(phy$edge[,2] > Ntip(phy))#
phy$root.edge <- end.age - mean.ages[1]#
tip.range <- matrix(0, nrow=2, ncol=Ntip(phy))#
}#
}#
if(analysis.type == "mrbayes") {#
node.hpd <- phy.data#
hpd.ages <- sapply(phy.data$node.ages[,"age_95%HPD"], function(xx) as.numeric(strsplit(xx, ",")[[1]]))#
end.age <- hpd.ages[2,1]#
node.in.ape <- as.numeric(phy.data$node.ages[,"node.name"])#
phy$root.edge <- end.age - nodeTimes(phy)[1,1]#
tip.range <- sapply(phy.data$tip.ages[,"age_95%HPD"], function(xx) as.numeric(strsplit(xx, ",")[[1]]))#
}#
if(analysis.type == "revbayes") {#
node.hpd <- phy.data#
hpd.ages <- sapply(phy.data$node.ages[,"age_95%_HPD"], function(xx) as.numeric(strsplit(xx, ",")[[1]]))#
end.age <- hpd.ages[2,1]#
node.in.ape <- as.numeric(phy.data$node.ages[,"node.name"])#
phy$root.edge <- end.age - nodeTimes(phy)[1,1]#
if(colnames(phy.data$tip.ages) == "age_95%_HPD") {#
tip.range <- sapply(phy.data$tip.ages[,"age_95%_HPD"], function(xx) as.numeric(strsplit(xx, ",")[[1]]))#