Browse code

Commit made by the Bioconductor Git-SVN bridge.

Commit id: 339117c7b83dac1233cfef7ef7f73dba23c8de66

collapse, expand, hilight and phylopic



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

Guangchuang Yu authored on 11/03/2015 06:13:40
Showing 12 changed files

... ...
@@ -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.22
4
+Version: 0.99.23
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
... ...
@@ -12,13 +12,15 @@ S3method(fortify,phylo)
12 12
 S3method(fortify,phylo4)
13 13
 export("%<%")
14 14
 export("%<+%")
15
+export("%>%")
15 16
 export(.)
16 17
 export(aes)
17 18
 export(as.binary)
19
+export(collapse_clade)
18 20
 export(download.phylopic)
21
+export(expand_clade)
19 22
 export(geom_aline)
20 23
 export(geom_hilight)
21
-export(geom_phylopic)
22 24
 export(geom_text)
23 25
 export(geom_tiplab)
24 26
 export(geom_tippoint)
... ...
@@ -40,6 +42,8 @@ export(ggtree)
40 42
 export(gplot)
41 43
 export(groupOTU)
42 44
 export(gzoom)
45
+export(hilight)
46
+export(phylopic)
43 47
 export(plot)
44 48
 export(read.baseml)
45 49
 export(read.beast)
... ...
@@ -1,3 +1,9 @@
1
+CHANGES IN VERSION 0.99.23
2
+------------------------
3
+ o mv geom_hilight to hilight <2015-03-11, Wed>
4
+ o mv geom_phylopic to phylopic <2015-03-11, Wed>
5
+ o implement collapse_clade and expand_clade for collapse and expand a selected clade <2015-03-11, Wed> 
6
+ 
1 7
 CHANGES IN VERSION 0.99.22
2 8
 ------------------------
3 9
  o remove quote in beast tip/node labels <2015-03-10, Tue>
... ...
@@ -208,43 +208,6 @@ geom_tippoint <- function(...) {
208 208
     geom_point(subset=.(isTip), ...)
209 209
 }
210 210
 
211
-##' add phylopic layer
212
-##'
213
-##' 
214
-##' @title geom_phylopic
215
-##' @param phylopic_id phylopic id
216
-##' @param x x position
217
-##' @param y y position
218
-##' @param width width of phylopic
219
-##' @param size size of phylopic to download
220
-##' @param color color
221
-##' @param alpha alpha
222
-##' @return phylopic layer
223
-##' @export
224
-##' @importFrom ggplot2 annotation_custom
225
-##' @importFrom grid rasterGrob
226
-##' @author Guangchuang Yu
227
-geom_phylopic <- function(phylopic_id, x=NULL, y=NULL, width=NULL,
228
-                          size=512, color="black", alpha=1) {
229
-    img <- download.phylopic(phylopic_id, size, color, alpha)
230
-    if ( is.null(x) || is.null(y) || is.null(width) ) {
231
-        xmin <- ymin <- -Inf
232
-        xmax <- ymax <- Inf
233
-    } else {
234
-        dims <- dim(img)[1:2]
235
-        AR <- dims[1]/dims[2]
236
-        xmin <- x - width/2
237
-        xmax <- x + width/2
238
-        ymin <- y - AR * width/2
239
-        ymax <- y + AR * width/2
240
-    }
241
-    
242
-    annotation_custom(xmin=xmin, ymin=ymin,
243
-                      xmax=xmax, ymax=ymax,
244
-                      rasterGrob(img))
245
-}
246
-
247
-
248 211
 
249 212
 ##' tree theme
250 213
 ##'
... ...
@@ -305,7 +268,39 @@ theme_tree2 <- function(bgcolor="white", fgcolor="black") {
305 268
           )
306 269
 }
307 270
 
308
-collapse <- function(tree_view, node) {
271
+##' hilight clade with rectangle
272
+##'
273
+##' 
274
+##' @title hilight
275
+##' @param tree_view tree view 
276
+##' @param node clade node
277
+##' @param fill fill color
278
+##' @param alpha alpha
279
+##' @param ... additional parameter
280
+##' @return tree view
281
+##' @export
282
+##' @author Guangchuang Yu
283
+hilight <- function(tree_view, node, fill="steelblue", alpha=0.5, ...) {
284
+    df <- tree_view$data
285
+    sp <- get.offspring.df(df, node)
286
+    sp.df <- df[c(sp, node),]
287
+    x <- sp.df$x
288
+    y <- sp.df$y
289
+    tree_view + annotate("rect", xmin=min(x)-df[node, "branch.length"]/2,
290
+                         xmax=max(x), ymin=min(y)-0.5, ymax=max(y)+0.5,
291
+                         fill = fill, alpha = alpha, ...)
292
+}
293
+
294
+##' collapse a clade
295
+##'
296
+##' 
297
+##' @title collapse_clade
298
+##' @param tree_view tree view 
299
+##' @param node clade node
300
+##' @return tree view
301
+##' @export
302
+##' @author Guangchuang Yu
303
+collapse_clade <- function(tree_view, node) {
309 304
     df <- tree_view$data
310 305
     sp <- get.offspring.df(df, node)
311 306
     sp.df <- df[sp,]
... ...
@@ -313,7 +308,7 @@ collapse <- function(tree_view, node) {
313 308
     sp_y <- range(sp.df$y)
314 309
     ii <- which(df$y > max(sp_y))
315 310
     if (length(ii)) {
316
-        df$y[ii] <- df$y[ii] - (max(sp_y) - min(sp_y))
311
+        df$y[ii] <- df$y[ii] - diff(sp_y)
317 312
     }
318 313
     df$y[node] <- min(sp_y)
319 314
 
... ...
@@ -326,8 +321,49 @@ collapse <- function(tree_view, node) {
326 321
         df[pp, "y"] <- mean(df[getChild.df(df, pp), "y"])
327 322
         pp <- df[pp, "parent"]
328 323
     }
329
-    df[pp, "y"] <- mean(df[getChild.df(df, pp), "y"])
324
+    j <- getChild.df(df, pp)
325
+    j <- j[j!=pp]
326
+    df[pp, "y"] <- mean(df[j, "y"])
327
+    
328
+    tree_view$data <- df
329
+    clade <- paste0("clade_", node)
330
+    attr(tree_view, clade) <- sp.df
331
+    tree_view
332
+}
333
+
334
+##' expand collased clade
335
+##'
336
+##' 
337
+##' @title expand_clade
338
+##' @param tree_view tree view
339
+##' @param node clade node
340
+##' @return tree view
341
+##' @export
342
+##' @author Guangchuang Yu
343
+expand_clade <- function(tree_view, node) {
344
+    clade <- paste0("clade_", node)
345
+    sp.df <- attr(tree_view, clade)
346
+    if (is.null(sp.df)) {
347
+        return(tree_view)
348
+    }
349
+    df <- tree_view$data
350
+    df[node, "isTip"] <- FALSE
351
+    sp_y <- range(sp.df$y)
352
+    ii <- which(df$y > df$y[node])
353
+    df[ii, "y"] <- df[ii, "y"] + diff(sp_y)
354
+    df[sp.df$node,] <- sp.df
355
+
356
+    root <- which(df$node == df$parent)
357
+    pp <- node
358
+    while(any(pp != root)) {
359
+        df[pp, "y"] <- mean(df[getChild.df(df, pp), "y"])
360
+        pp <- df[pp, "parent"]
361
+    }
362
+    j <- getChild.df(df, pp)
363
+    j <- j[j!=pp]
364
+    df[pp, "y"] <- mean(df[j, "y"])
330 365
     
331 366
     tree_view$data <- df
367
+    attr(tree_view, clade) <- NULL
332 368
     tree_view
333 369
 }
... ...
@@ -52,3 +52,53 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) {
52 52
     return(img)
53 53
 }
54 54
 
55
+##' add phylopic layer
56
+##'
57
+##' 
58
+##' @title phylopic
59
+##' @param tree_view tree view
60
+##' @param phylopic_id phylopic id
61
+##' @param size size of phylopic to download
62
+##' @param color color
63
+##' @param alpha alpha
64
+##' @param node selected node
65
+##' @param x x position
66
+##' @param y y position
67
+##' @param width width of phylopic
68
+##' @return phylopic layer
69
+##' @export
70
+##' @importFrom ggplot2 annotation_custom
71
+##' @importFrom grid rasterGrob
72
+##' @author Guangchuang Yu
73
+phylopic <- function(tree_view, phylopic_id,
74
+                     size=512, color="black", alpha=0.5,
75
+                     node=NULL, x=NULL, y=NULL, width=NULL) {
76
+    img <- download.phylopic(phylopic_id, size, color, alpha)
77
+    if ( is.null(node) ) {
78
+        xmin <- ymin <- -Inf
79
+        xmax <- ymax <- Inf
80
+    } else {
81
+        if (is.null(x) || is.null(y)) {
82
+            if (is.null(node)) {
83
+                stop("node or x and y should not be NULL...")
84
+            }
85
+            x <- tree_view$data[node, "x"]
86
+            y <- tree_view$data[node, "y"]
87
+        }
88
+        if (is.null(width)) {
89
+            width <- 5
90
+        }
91
+        
92
+        dims <- dim(img)[1:2]
93
+        AR <- dims[1]/dims[2]
94
+        xmin <- x - width/2
95
+        xmax <- x + width/2
96
+        ymin <- y - AR * width/2
97
+        ymax <- y + AR * width/2
98
+    }
99
+    
100
+    tree_view + annotation_custom(xmin=xmin, ymin=ymin,
101
+                                  xmax=xmax, ymax=ymax,
102
+                                  rasterGrob(img))
103
+}
104
+
... ...
@@ -321,3 +321,15 @@ roundDigit <- function(d) {
321 321
     structure(as.list(match.call()[-1]), env = .env, class = "quoted")
322 322
 }
323 323
 
324
+
325
+##' pipe
326
+##' @export
327
+##' @rdname pipe
328
+##' @param lhs left hand side
329
+##' @param rhs right hand side
330
+##' @source
331
+##' This is just the imported function
332
+##' from the marittr package. The documentation you should
333
+##' read for %>% function can be found here: \link[magrittr]{%>%}
334
+`%>%` <- magrittr::`%>%`
335
+
324 336
new file mode 100644
... ...
@@ -0,0 +1,23 @@
1
+% Generated by roxygen2 (4.1.0): do not edit by hand
2
+% Please edit documentation in R/ggtree.R
3
+\name{collapse_clade}
4
+\alias{collapse_clade}
5
+\title{collapse_clade}
6
+\usage{
7
+collapse_clade(tree_view, node)
8
+}
9
+\arguments{
10
+\item{tree_view}{tree view}
11
+
12
+\item{node}{clade node}
13
+}
14
+\value{
15
+tree view
16
+}
17
+\description{
18
+collapse a clade
19
+}
20
+\author{
21
+Guangchuang Yu
22
+}
23
+
0 24
new file mode 100644
... ...
@@ -0,0 +1,23 @@
1
+% Generated by roxygen2 (4.1.0): do not edit by hand
2
+% Please edit documentation in R/ggtree.R
3
+\name{expand_clade}
4
+\alias{expand_clade}
5
+\title{expand_clade}
6
+\usage{
7
+expand_clade(tree_view, node)
8
+}
9
+\arguments{
10
+\item{tree_view}{tree view}
11
+
12
+\item{node}{clade node}
13
+}
14
+\value{
15
+tree view
16
+}
17
+\description{
18
+expand collased clade
19
+}
20
+\author{
21
+Guangchuang Yu
22
+}
23
+
0 24
new file mode 100644
... ...
@@ -0,0 +1,29 @@
1
+% Generated by roxygen2 (4.1.0): do not edit by hand
2
+% Please edit documentation in R/ggtree.R
3
+\name{hilight}
4
+\alias{hilight}
5
+\title{hilight}
6
+\usage{
7
+hilight(tree_view, node, fill = "steelblue", alpha = 0.5, ...)
8
+}
9
+\arguments{
10
+\item{tree_view}{tree view}
11
+
12
+\item{node}{clade node}
13
+
14
+\item{fill}{fill color}
15
+
16
+\item{alpha}{alpha}
17
+
18
+\item{...}{additional parameter}
19
+}
20
+\value{
21
+tree view
22
+}
23
+\description{
24
+hilight clade with rectangle
25
+}
26
+\author{
27
+Guangchuang Yu
28
+}
29
+
0 30
similarity index 56%
1 31
rename from man/geom_phylopic.Rd
2 32
rename to man/phylopic.Rd
... ...
@@ -1,26 +1,30 @@
1 1
 % Generated by roxygen2 (4.1.0): do not edit by hand
2
-% Please edit documentation in R/ggtree.R
3
-\name{geom_phylopic}
4
-\alias{geom_phylopic}
5
-\title{geom_phylopic}
2
+% Please edit documentation in R/phylopic.R
3
+\name{phylopic}
4
+\alias{phylopic}
5
+\title{phylopic}
6 6
 \usage{
7
-geom_phylopic(phylopic_id, x = NULL, y = NULL, width = NULL, size = 512,
8
-  color = "black", alpha = 1)
7
+phylopic(tree_view, phylopic_id, size = 512, color = "black", alpha = 0.5,
8
+  node = NULL, x = NULL, y = NULL, width = NULL)
9 9
 }
10 10
 \arguments{
11
-\item{phylopic_id}{phylopic id}
12
-
13
-\item{x}{x position}
11
+\item{tree_view}{tree view}
14 12
 
15
-\item{y}{y position}
16
-
17
-\item{width}{width of phylopic}
13
+\item{phylopic_id}{phylopic id}
18 14
 
19 15
 \item{size}{size of phylopic to download}
20 16
 
21 17
 \item{color}{color}
22 18
 
23 19
 \item{alpha}{alpha}
20
+
21
+\item{node}{selected node}
22
+
23
+\item{x}{x position}
24
+
25
+\item{y}{y position}
26
+
27
+\item{width}{width of phylopic}
24 28
 }
25 29
 \value{
26 30
 phylopic layer
27 31
new file mode 100644
... ...
@@ -0,0 +1,22 @@
1
+% Generated by roxygen2 (4.1.0): do not edit by hand
2
+% Please edit documentation in R/utilities.R
3
+\name{\%>\%}
4
+\alias{\%>\%}
5
+\title{pipe}
6
+\source{
7
+This is just the imported function
8
+from the marittr package. The documentation you should
9
+read for %>% function can be found here: \link[magrittr]{%>%}
10
+}
11
+\usage{
12
+lhs \%>\% rhs
13
+}
14
+\arguments{
15
+\item{lhs}{left hand side}
16
+
17
+\item{rhs}{right hand side}
18
+}
19
+\description{
20
+pipe
21
+}
22
+
... ...
@@ -518,7 +518,7 @@ gzoom(chiroptera, grep("Plecotus", chiroptera$tip.label))
518 518
 
519 519
 ## highlight clades
520 520
 
521
-`r Githubpkg("GuangchuangYu/ggtree")` implements _`geom_hilight`_ layer, that accepts tree object and internal node number and add a layer of rectangle to highlight the selected clade.
521
+`r Githubpkg("GuangchuangYu/ggtree")` implements _`hilight`_ function, that accepts tree view and internal node number and add a layer of rectangle to highlight the selected clade.
522 522
 
523 523
 ```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
524 524
 nwk <- system.file("extdata", "sample.nwk", package="ggtree")
... ...
@@ -530,14 +530,45 @@ ggtree(tree) + geom_text(aes(label=node))
530 530
 User can use _`geom_text`_ to display all the node numbers, and select interesting clade to highlight.
531 531
 
532 532
 ```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
533
-ggtree(tree) + geom_hilight(tree, node=21, fill="steelblue", alpha=.6) +
534
-     geom_hilight(tree, node=17, fill="darkgreen", alpha=.6)
533
+ggtree(tree) %>% hilight(node=21, fill="steelblue", alpha=.6) %>%
534
+    hilight(node=17, fill="darkgreen", alpha=.6)
535 535
 ```
536 536
 
537 537
 
538 538
 ```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
539
-ggtree(tree, layout="fan") + geom_hilight(tree, node=21, fill="steelblue", alpha=.6) +
540
-     geom_hilight(tree, node=17, fill="darkgreen", alpha=.6)
539
+ggtree(tree, layout="fan") %>% hilight(node=21, fill="steelblue", alpha=.6) %>%
540
+     hilight(node=17, fill="darkgreen", alpha=.6)
541
+```
542
+
543
+## collapse clade
544
+
545
+With _`collapse_clade`_ function, user can collapse a selected clade.
546
+
547
+```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
548
+ggtree(tree) %>% collapse_clade(node=17)
549
+```
550
+
551
+## expand collapsed clade
552
+
553
+The collapsed clade can be expanded via _`expand_clade`_ function.
554
+
555
+```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
556
+ggtree(tree) %>% collapse_clade(node=17) %>% expand_clade(node=17)
557
+```
558
+
559
+## phylopic
560
+
561
+[PhyloPic](http://phylopic.org/) is a database that stores reusable silhouette images of organisms. `r Githubpkg("GuangchuangYu/ggtree")` supports downloading images from [PhyloPic](http://phylopic.org/) and annotating phylogenetic tree with the downloaded images.
562
+
563
+```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
564
+cols <- rainbow_hcl(2)
565
+pp <- ggtree(tree) %>% phylopic("3318565d-7531-4a8c-8b84-2e87dfae36ab", color=cols[1], alpha = .3)
566
+pp
567
+```
568
+
569
+```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
570
+pp %>% phylopic("d3563b54-780f-4711-a49a-7ea051e9dacc", color=cols[2], alpha=.8, node=21) %>%
571
+    phylopic("d3563b54-780f-4711-a49a-7ea051e9dacc", color="darkgreen", alpha=.8, node=17, width=10)
541 572
 ```
542 573
 
543 574
 ## scale color based on numerical attribute
... ...
@@ -648,17 +679,6 @@ ggtree(tree_iris, color=cols[cls_ind]) %<+% species +
648 679
 
649 680
 This example demonstrates how the separation of the _`bionj`_ is very good with the _`setosa`_ species, but misses in labeling several _`versicolor`_ and _`virginica`_ species.
650 681
 
651
-## phylopic
652
-
653
-[PhyloPic](http://phylopic.org/) is a database that stores reusable silhouette images of organisms. `r Githubpkg("GuangchuangYu/ggtree")` supports downloading images from [PhyloPic](http://phylopic.org/) and annotating phylogenetic tree with the downloaded images.
654
-
655
-```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
656
-cols <- rainbow_hcl(2)
657
-x <- get.phylopic("d3563b54-780f-4711-a49a-7ea051e9dacc", size=512, color=cols[1], alpha=.3)
658
-y <- get.phylopic("3318565d-7531-4a8c-8b84-2e87dfae36ab", size=512, color=cols[2], alpha=.8)
659
-ggtree(tree) + annotation_custom(xmin=-Inf, ymin=-Inf, xmax=Inf, ymax=Inf, x) +
660
-       annotation_custom(xmin=15, ymin=2, xmax=25, ymax=4, y) 
661
-```
662 682
 
663 683
 ## visualize tree with associated matrix
664 684
 ```{r}