File: DrawingPhylogenies.R

package info (click to toggle)
r-cran-ape 5.8-1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,676 kB
  • sloc: ansic: 7,676; cpp: 116; sh: 17; makefile: 2
file content (498 lines) | stat: -rw-r--r-- 15,440 bytes parent folder | download | duplicates (2)
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
### R code from vignette source 'DrawingPhylogenies.Rnw'

###################################################
### code chunk number 1: DrawingPhylogenies.Rnw:16-17
###################################################
options(width = 80, prompt = "> ")


###################################################
### code chunk number 2: DrawingPhylogenies.Rnw:61-63
###################################################
library(ape)
mytr <- read.tree(text = "((Pan:5,Homo:5):2,Gorilla:7);")


###################################################
### code chunk number 3: DrawingPhylogenies.Rnw:69-75
###################################################
foo <- function() {
    col <- "green"
    for (i in 1:2)
        axis(i, col = col, col.ticks = col, col.axis = col, las = 1)
    box(lty = "19")
}


###################################################
### code chunk number 4: DrawingPhylogenies.Rnw:82-89
###################################################
layout(matrix(1:4, 2, 2, byrow = TRUE))
plot(mytr); foo()
plot(mytr, "c", FALSE); foo()
plot(mytr, "u"); foo()
par(xpd = TRUE)
plot(mytr, "f"); foo()
box("outer")


###################################################
### code chunk number 5: DrawingPhylogenies.Rnw:135-137
###################################################
tr <- compute.brlen(stree(8, "l"), 0.1)
tr$tip.label[] <- ""


###################################################
### code chunk number 6: DrawingPhylogenies.Rnw:144-150
###################################################
foo <- function() {
    col <- "green"
    axis(1, col = col, col.ticks = col, col.axis = col)
    axis(2, col = col, col.ticks = col, col.axis = col, at = 1:Ntip(tr), las = 1)
    box(lty = "19")
}


###################################################
### code chunk number 7: DrawingPhylogenies.Rnw:152-152
###################################################



###################################################
### code chunk number 8: DrawingPhylogenies.Rnw:155-165
###################################################
layout(matrix(1:12, 6, 2))
par(mar = c(2, 2, 0.3, 0))
for (type in c("p", "c")) {
   plot(tr, type); foo()
   plot(tr, type, node.pos = 2); foo()
   plot(tr, type, FALSE); foo()
   plot(tr, type, FALSE, node.pos = 1, node.depth = 2); foo()
   plot(tr, type, FALSE, node.pos = 2); foo()
   plot(tr, type, FALSE, node.pos = 2, node.depth = 2); foo()
}


###################################################
### code chunk number 9: DrawingPhylogenies.Rnw:173-178
###################################################
foo <- function() {
    col <- "green"
    for (i in 1:2) axis(i, col = col, col.ticks = col, col.axis = col, las = 1)
    box(lty = "19")
}


###################################################
### code chunk number 10: DrawingPhylogenies.Rnw:181-187
###################################################
layout(matrix(1:4, 2, 2))
par(las = 1)
plot(tr, "u"); foo()
plot(tr, "u", FALSE); foo()
plot(tr, "f"); foo()
plot(tr, "f", FALSE); foo()


###################################################
### code chunk number 11: DrawingPhylogenies.Rnw:257-259
###################################################
args(node.depth.edgelength)
args(node.depth)


###################################################
### code chunk number 12: DrawingPhylogenies.Rnw:271-272
###################################################
args(node.height)


###################################################
### code chunk number 13: DrawingPhylogenies.Rnw:288-289
###################################################
args(unrooted.xy)


###################################################
### code chunk number 14: DrawingPhylogenies.Rnw:296-299
###################################################
args(phylogram.plot)
args(cladogram.plot)
args(circular.plot)


###################################################
### code chunk number 15: DrawingPhylogenies.Rnw:315-316
###################################################
args(plot.phylo)


###################################################
### code chunk number 16: DrawingPhylogenies.Rnw:369-370
###################################################
geo <- factor(c("Africa", "World", "Africa"))


###################################################
### code chunk number 17: DrawingPhylogenies.Rnw:378-380
###################################################
(mycol <- c("blue", "red")[geo])
plot(mytr, tip.color = mycol)


###################################################
### code chunk number 18: DrawingPhylogenies.Rnw:392-394
###################################################
par(xpd = TRUE)
plot(mytr, tip.color = mycol, cex = c(1, 1, 1.5))


###################################################
### code chunk number 19: DrawingPhylogenies.Rnw:401-402
###################################################
args(def)


###################################################
### code chunk number 20: DrawingPhylogenies.Rnw:413-415
###################################################
mycol2 <- def(mytr$tip.label, Homo = "red", default = "blue")
identical(mycol, mycol2)


###################################################
### code chunk number 21: DrawingPhylogenies.Rnw:436-442
###################################################
(i <- which.edge(mytr, c("Homo", "Pan")))
co <- rep("black", Nedge(mytr))
co[i] <- "blue"
par(xpd = TRUE)
plot(mytr, edge.col = co)
edgelabels()


###################################################
### code chunk number 22: DrawingPhylogenies.Rnw:480-481
###################################################
args(axisPhylo)


###################################################
### code chunk number 23: DrawingPhylogenies.Rnw:493-494
###################################################
args(add.scale.bar)


###################################################
### code chunk number 24: DrawingPhylogenies.Rnw:506-510
###################################################
plot(mytr)
add.scale.bar()
axisPhylo()
axis(3)


###################################################
### code chunk number 25: DrawingPhylogenies.Rnw:520-523
###################################################
args(nodelabels)
args(tiplabels)
args(edgelabels)


###################################################
### code chunk number 26: DrawingPhylogenies.Rnw:535-540
###################################################
par(xpd = TRUE)
plot(mytr)
nodelabels()
tiplabels()
edgelabels()


###################################################
### code chunk number 27: DrawingPhylogenies.Rnw:563-566
###################################################
plot(rep(1:5, 5), rep(1:5, each = 5), pch = 1:25, xlim = c(1, 5.2),
     col = "blue", bg = "yellow", cex = 2)
text(rep(1:5, 5) + 0.2, rep(1:5, each = 5), 1:25)


###################################################
### code chunk number 28: DrawingPhylogenies.Rnw:583-593
###################################################
v <- c(0, 0.5, 1)
layout(matrix(1:9, 3, 3))
par(mar = c(3, 3, 3, 0), las = 1)
for (i in v) {
    for (j in v) {
        plot(0, 0, "n", main = paste0("adj = c(", i, ", ", j, ")"))
        abline(v = 0, h = 0, lty = 3)
        text(0, 0, "Gorilla", adj = c(i, j))
    }
}


###################################################
### code chunk number 29: DrawingPhylogenies.Rnw:601-605
###################################################
plot(rep(0, 3), 0:2, "n", las = 1)
abline(v = 0, h = 0:2, lty = 3)
text(0, 0:2, mytr$tip.label, adj = -1)
text(0, 0:2, mytr$tip.label, adj = 2, font = 2)


###################################################
### code chunk number 30: DrawingPhylogenies.Rnw:623-625
###################################################
args(phydataplot)
args(ring)


###################################################
### code chunk number 31: DrawingPhylogenies.Rnw:677-679
###################################################
x <- matrix(1:6, 3, 10)
dimnames(x) <- list(c("Homo", "Gorilla", "Pan"), LETTERS[1:ncol(x)])


###################################################
### code chunk number 32: DrawingPhylogenies.Rnw:685-687
###################################################
x
mytr$tip.label


###################################################
### code chunk number 33: DrawingPhylogenies.Rnw:699-705
###################################################
par(mar = c(10, 2, 5, 5))
plot(mytr, x.lim = 30)
phydataplot(x, mytr, "m", border = "white", offset = 2, width = 1)
phydataplot(x, mytr, "m", border = "white", offset = 15,
            width = 1, continuous = 2)



###################################################
### code chunk number 34: DrawingPhylogenies.Rnw:718-728
###################################################
n <- 100
p <- 3
set.seed(3)
tr <- rcoal(n)
x <- matrix(runif(p * n), n, p)
rownames(x) <- tr$tip.label
COL <- c("red", "green", "blue")
par(xpd = TRUE)
plot(tr, "f", x.lim = c(-5, 5), show.tip.label = FALSE, no.margin = TRUE)
for (j in 1:p) ring(x[, j], tr, offset = j - 1 + 0.1, col = COL[j])


###################################################
### code chunk number 35: DrawingPhylogenies.Rnw:754-759
###################################################
tree <- rtree(10)
tree$edge.length[1] <- 1000
layout(matrix(1:2, 1))
plot(tree); axisPhylo()
plotBreakLongEdges(tree); axisPhylo()


###################################################
### code chunk number 36: DrawingPhylogenies.Rnw:767-769
###################################################
el <- tree$edge.length
sum(el > mean(el) + 2 * sd(el))


###################################################
### code chunk number 37: DrawingPhylogenies.Rnw:774-780
###################################################
tree$edge.length[8] <- 1000
el <- tree$edge.length
sum(el > mean(el) + 2 * sd(el))
layout(matrix(1:2, 1))
plotBreakLongEdges(tree)
plotBreakLongEdges(tree, n = 2)


###################################################
### code chunk number 38: DrawingPhylogenies.Rnw:804-806
###################################################
TR <- replicate(10, rcoal(sample(11:20, size = 1)), simplify = FALSE)
kronoviz(TR, type = "c", show.tip.label = FALSE)


###################################################
### code chunk number 39: DrawingPhylogenies.Rnw:817-820
###################################################
dates <- as.Date(.leap.seconds)
tr <- rcoal(length(dates))
plotTreeTime(tr, dates)


###################################################
### code chunk number 40: DrawingPhylogenies.Rnw:833-840
###################################################
par(oma = rep(2, 4))
plot(mytr)
box(lty = 3)
box("figure", col = "blue")
for (i in 1:4)
    mtext("Outer margin", i, 0.5, outer = TRUE, font = 2, col = "blue")
box("outer")


###################################################
### code chunk number 41: DrawingPhylogenies.Rnw:856-867
###################################################
layout(matrix(1:4, 2, 2))
par(oma = rep(2, 4))
for (i in 1:4) {
    par(mar = rep(i, 4))
    plot(mytr)
    box(lty = 3)
    box("figure", col = "blue")
}
for (i in 1:4)
    mtext("Outer margin", i, 0.5, outer = TRUE, font = 2, col = "blue")
box("outer")


###################################################
### code chunk number 42: DrawingPhylogenies.Rnw:885-887
###################################################
layout(matrix(c(1, 2, 1, 3), 2, 2))
for (i in 1:3) plot(rtree(5))


###################################################
### code chunk number 43: DrawingPhylogenies.Rnw:906-907
###################################################
(pp <- get("last_plot.phylo", envir = .PlotPhyloEnv))


###################################################
### code chunk number 44: DrawingPhylogenies.Rnw:972-1029
###################################################
## split the device into 2:
layout(matrix(1:2, 2))
## plot the 1st tree
plot(rtree(4))
## get the parameters of this first plot:
pp <- get("last_plot.phylo", envir = .PlotPhyloEnv)
## keep the coordinates of the first tip:
x <- pp$xx[1]
y <- pp$yy[1]
## extremes of the coordinates on both axes:
(pu <- par("usr"))
## fraction of the plot region:
(x - pu[1]) / (pu[2] - pu[1])
(y - pu[3]) / (pu[4] - pu[3])
## get the dimensions of plotting region and margins in inches:
pi <- par("pin")
mi <- par("mai")
## convert the coordinates into inches:
xi1 <- (x - pu[1]) / (pu[2] - pu[1]) * pi[1] + mi[2]
yi1 <- (y - pu[3]) / (pu[4] - pu[3]) * pi[2] + mi[1]

## xi1 and yi1 are the final coordinates of this tip in inches

## plot the 2nd tree:
plot(rtree(4))
## same as above:
pp <- get("last_plot.phylo", envir = .PlotPhyloEnv)
## ... except we take the coordinates of the root:
x <- pp$xx[5]
y <- pp$yy[5]
pu <- par("usr")
pi <- par("pin")
mi <- par("mai")
xi2 <- (x - pu[1]) / (pu[2] - pu[1]) * pi[1] + mi[2]
yi2 <- (y - pu[3]) / (pu[4] - pu[3]) * pi[2] + mi[1]

## xi2 and yi2 are the final coordinates of this root in inches

## we add the height of this second plot to the 'y' coordinate
## of the first tip of the first tree which is above the second
## one according to layout()
yi1 <- yi1 + par("fin")[2]
## => this operation depends on the specific layout of plots

## get the dimension of the device in inches:
di <- par("din")

## reset the layout
layout(1)
## set new = TRUE and the margins to zero:
par(new = TRUE, mai = rep(0, 4))
## set the scales to be [0,1] on both axes (in user coordinates):
plot(NA, type = "n", ann = FALSE, axes = FALSE, xlim = 0:1,
     ylim = 0:1, xaxs = "i", yaxs = "i")
## graphical elements can now be added:
fancyarrows(xi1/di[1], yi1/di[2], xi2/di[1], yi2/di[2], 1,
            lwd = 10, col = rgb(1, .5, 0, .5), type = "h")


###################################################
### code chunk number 45: DrawingPhylogenies.Rnw:1039-1074
###################################################
foo <- function(phy1, phy2, from)
{
    layout(matrix(1:2, 2))
    plot(phy1, font = 1)
    pp <- get("last_plot.phylo", envir = .PlotPhyloEnv)
    from <- which(phy1$tip.label == from)
    x <- pp$xx[from]
    y <- pp$yy[from]
    ## fraction of the plot region:
    pu <- par("usr")
    ## convert into inches:
    pi <- par("pin")
    mi <- par("mai")
    xi1 <- (x - pu[1]) / (pu[2] - pu[1]) * pi[1] + mi[2]
    yi1 <- (y - pu[3]) / (pu[4] - pu[3]) * pi[2] + mi[1]
    plot(phy2)
    pp <- get("last_plot.phylo", envir = .PlotPhyloEnv)
    to <- Ntip(phy2) + 1
    x <- pp$xx[to]
    y <- pp$yy[to]
    ## same as above:
    pu <- par("usr")
    pi <- par("pin")
    mi <- par("mai")
    xi2 <- (x - pu[1]) / (pu[2] - pu[1]) * pi[1] + mi[2]
    yi2 <- (y - pu[3]) / (pu[4] - pu[3]) * pi[2] + mi[1]
    yi1 <- yi1 + par("fin")[2]
    di <- par("din")
    layout(1)
    par(new = TRUE, mai = rep(0, 4))
    plot(NA, type = "n", ann = FALSE, axes = FALSE, xlim = 0:1,
         ylim = 0:1, xaxs = "i", yaxs = "i")
    fancyarrows(xi1/di[1], yi1/di[2], xi2/di[1], yi2/di[2], 1,
                lwd = 10, col = rgb(1, .5, 0, .5), type = "h")
}


###################################################
### code chunk number 46: DrawingPhylogenies.Rnw:1080-1083
###################################################
trb <- read.tree(text = "((Primates,Carnivora),Monotremata);")
par(xpd = TRUE)
foo(trb, mytr, "Primates")


###################################################
### code chunk number 47: DrawingPhylogenies.Rnw:1100-1106
###################################################
plot(mytr, plot = FALSE)
pp <- get("last_plot.phylo", envir = .PlotPhyloEnv)
rect(pp$xx[5] - 0.1, pp$yy[1] - 0.1, pp$xx[1] + 2, pp$yy[2] + 0.1,
     col = "yellow", border = NA)
par(new = TRUE)
plot(mytr)