Browse code

Commit made by the Bioconductor Git-SVN bridge. Consists of 1 commit.

Commit information:

Commit id: 736e5b62c468ce4119c9850e6d99e8d57e9206f2

angle and branch.y variable in cladogram layout

Committed by: GuangchuangYu
Author Name: GuangchuangYu
Commit date: 2015-02-10 17:36:04 +0800
Author date: 2015-02-10 17:36:04 +0800


git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@99312 bc3139a8-67e5-0310-9ffc-ced21a209358

g.yu authored on 10/02/2015 09:36:24
Showing 4 changed files

  • DESCRIPTION index f3c12d7a..17e6b14e 100644
  • NEWS index 8e55bf21..16284e5f 100644
  • R/tree.R index 5b414b89..ae14de25 100644
  • R/treeIO.R index b902f582..f6e04970 100644
... ...
@@ -1,7 +1,7 @@
1 1
 Package: ggtree
2 2
 Type: Package
3 3
 Title: a phylogenetic tree viewer for different types of tree annotations
4
-Version: 0.99.16
4
+Version: 0.99.17
5 5
 Author: Guangchuang Yu
6 6
 Maintainer: Guangchuang Yu <guangchuangyu@gmail.com>
7 7
 Description: ggtree extends the ggplot2 plotting system which implemented the
... ...
@@ -1,3 +1,7 @@
1
+CHANGES IN VERSION 0.99.17
2
+------------------------
3
+ o add angle and branch.y variable in cladogram layout <2015-02-10, Tue>
4
+ 
1 5
 CHANGES IN VERSION 0.99.16
2 6
 ------------------------
3 7
  o correct typo in vignette <2015-02-10, Tue>
... ...
@@ -653,3 +653,18 @@ getYcoord_scale_category <- function(tr, df, yscale, yscale_mapping=NULL, ...) {
653 653
     }
654 654
     return(y)
655 655
 }
656
+
657
+
658
+add_angle_cladogram <- function(res) {
659
+    dy <- (res[, "y"] - res[res$parent, "y"]) / diff(range(res[, "y"]))
660
+    dx <- (res[, "x"] - res[res$parent, "x"]) / diff(range(res[, "x"]))
661
+    theta <- atan(dy/dx)
662
+    theta[is.na(theta)] <- 0 ## root node
663
+    res$angle <- theta/pi * 180
664
+    branch.y <- (res[res$parent, "y"] + res[, "y"])/2
665
+    idx <- is.na(branch.y)
666
+    branch.y[idx] <- res[idx, "y"]
667
+    res[, "branch.y"] <- branch.y
668
+    return(res)
669
+}
670
+
... ...
@@ -185,7 +185,7 @@ fortify.beast <- function(model, data,
185 185
     stats <- stats[,colnames(stats) != "node"]
186 186
     
187 187
     df <- cbind(df, stats)
188
-    df <- scaleY(phylo, df, yscale, ...)
188
+    df <- scaleY(phylo, df, yscale, layout, ...)
189 189
 
190 190
     return(df)
191 191
 }
... ...
@@ -231,7 +231,7 @@ fortify.codeml <- function(model, data,
231 231
     
232 232
     res <- merge_phylo_anno.codeml_mlc(df, dNdS, ndigits)
233 233
     df <- merge_phylo_anno.paml_rst(res, model@rst)
234
-    scaleY(phylo, df, yscale, ...)
234
+    scaleY(phylo, df, yscale, layout, ...)
235 235
 }
236 236
 
237 237
 
... ...
@@ -254,7 +254,7 @@ fortify.codeml_mlc <- function(model, data,
254 254
     dNdS <- model@dNdS
255 255
 
256 256
     df <- merge_phylo_anno.codeml_mlc(df, dNdS, ndigits)
257
-    scaleY(phylo, df, yscale, ...)
257
+    scaleY(phylo, df, yscale, layout, ...)
258 258
 }
259 259
 
260 260
 merge_phylo_anno.codeml_mlc <- function(df, dNdS, ndigits = NULL) {
... ...
@@ -308,7 +308,7 @@ fortify.paml_rst <- function(model, data, layout = "phylogram", yscale="none",
308 308
                              ladderize=TRUE, right=FALSE, ...) {
309 309
     df <- fortify.phylo(model@phylo, data, layout, ladderize, right, ...)
310 310
     df <- merge_phylo_anno.paml_rst(df, model)
311
-    scaleY(model@phylo, df, yscale, ...)
311
+    scaleY(model@phylo, df, yscale, layout, ...)
312 312
 }
313 313
 
314 314
 merge_phylo_anno.paml_rst <- function(df, model) {
... ...
@@ -337,13 +337,13 @@ fortify.jplace <- function(model, data,
337 337
 
338 338
     df <- df %add2% place
339 339
 
340
-    scaleY(model@phylo, df, yscale, ...)
340
+    scaleY(model@phylo, df, yscale, layout, ...)
341 341
 }
342 342
 
343
-scaleY <- function(phylo, df, yscale, ...) {
343
+scaleY <- function(phylo, df, yscale, layout, ...) {
344 344
     if (yscale == "none") {
345 345
         return(df)
346
-    }
346
+    } 
347 347
     if (! yscale %in% colnames(df)) {
348 348
         warning("yscale is not available...\n")
349 349
         return(df)
... ...
@@ -360,6 +360,9 @@ scaleY <- function(phylo, df, yscale, ...) {
360 360
     }
361 361
     
362 362
     df[, "y"] <- y
363
+    if (layout == "cladogram") {
364
+        df <- add_angle_cladogram(df)
365
+    }
363 366
     return(df)
364 367
 }
365 368
 
... ...
@@ -371,7 +374,7 @@ fortify.phylo4 <- function(model, data, layout="phylogram", yscale="none",
371 374
     phylo <- as.phylo.phylo4(model)
372 375
     df <- fortify.phylo(phylo, data,
373 376
                         layout, ladderize, right, ...)
374
-    scaleY(phylo, df, yscale)
377
+    scaleY(phylo, df, yscale, layout, ...)
375 378
 }
376 379
 
377 380
 as.phylo.phylo4 <- function(phylo4) {
... ...
@@ -425,6 +428,9 @@ fortify.phylo <- function(model, data, layout="phylogram",
425 428
     rownames(df) <- df$node
426 429
     cn <- colnames(df)
427 430
     colnames(df)[grep("length", cn)] <- "branch.length"
431
+    if(layout == "cladogram") {
432
+        df <- add_angle_cladogram(df)
433
+    }
428 434
     return(df)
429 435
 }
430 436
 
... ...
@@ -489,18 +495,19 @@ as.data.frame.phylo_ <- function(x, layout="phylogram",
489 495
     isTip <- rep(FALSE, N)
490 496
     isTip[1:Ntip] <- TRUE
491 497
     res$isTip <- isTip
492
-    res$branch <- (res$x[res$parent] + res$x)/2
498
+    res$branch <- (res[res$parent, "x"] + res[, "x"])/2
493 499
     if (!is.null(res$length)) {
494 500
         res$length[is.na(res$length)] <- 0
495 501
     }
496 502
     res$branch[is.na(res$branch)] <- 0
497 503
     
498 504
     if (layout == "fan") {
499
-      idx <- match(1:N, order(res$y))
500
-      angle <- -360/N * 1:N
501
-      angle <- angle[idx]
502
-      res$angle <- angle
503
-    }
505
+        idx <- match(1:N, order(res$y))
506
+        angle <- -360/N * 1:N
507
+        angle <- angle[idx]
508
+        res$angle <- angle
509
+    } 
510
+    
504 511
     return(res)
505 512
 }
506 513