Browse code

update geom_hilight

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

g.yu authored on 10/05/2016 10:16:07
Showing 4 changed files

... ...
@@ -1,5 +1,8 @@
1 1
 CHANGES IN VERSION 1.5.2
2 2
 ------------------------
3
+ o add extend, extendto parameter in geom_hilight <2016-05-10, Tue>
4
+ o geom_hilight now supports hilight tips <2016-05-10, Tue>
5
+   + https://github.com/GuangchuangYu/ggtree/issues/53
3 6
  o more accurate ylim & angle for circular layout <2016-05-10, Tue>
4 7
    + https://github.com/GuangchuangYu/ggtree/issues/40
5 8
  o supports phylo4d object <2016-05-10, Tue>
... ...
@@ -5,12 +5,14 @@
5 5
 ##' @param node selected node to hilight
6 6
 ##' @param fill color fill
7 7
 ##' @param alpha alpha (transparency)
8
+##' @param extend extend xmax of the rectangle
9
+##' @param extendto extend xmax to extendto
8 10
 ##' @return ggplot2
9 11
 ##' @export
10 12
 ##' @importFrom ggplot2 aes_
11 13
 ##' @importFrom ggplot2 GeomRect
12 14
 ##' @author Guangchuang Yu
13
-geom_hilight <- function(node, fill="steelblue", alpha=.5) {
15
+geom_hilight <- function(node, fill="steelblue", alpha=.5, extend=0, extendto=NULL) {
14 16
                          
15 17
     
16 18
     data = NULL
... ...
@@ -33,7 +35,10 @@ geom_hilight <- function(node, fill="steelblue", alpha=.5) {
33 35
         show.legend=show.legend,
34 36
         inherit.aes = inherit.aes,
35 37
         params = list(node=node,
36
-                      fill=fill, alpha=alpha,
38
+                      fill=fill,
39
+                      alpha=alpha,
40
+                      extend=extend,
41
+                      extendto=extendto,
37 42
                       na.rm = na.rm)
38 43
     )
39 44
 }
... ...
@@ -51,6 +56,8 @@ geom_hilight <- function(node, fill="steelblue", alpha=.5) {
51 56
 ##' @param inherit.aes logical
52 57
 ##' @param fill fill color
53 58
 ##' @param alpha transparency
59
+##' @param extend extend xmax of the rectangle
60
+##' @param extendto extend xmax to extendto
54 61
 ##' @param ... additional parameter
55 62
 ##' @return layer
56 63
 ##' @importFrom ggplot2 layer
... ...
@@ -58,7 +65,7 @@ geom_hilight <- function(node, fill="steelblue", alpha=.5) {
58 65
 stat_hilight <- function(mapping=NULL, data=NULL, geom="rect",
59 66
                          position="identity",  node, 
60 67
                          show.legend=NA, inherit.aes=FALSE,
61
-                        fill, alpha,
68
+                        fill, alpha, extend=0, xmax=NULL,
62 69
                          ...) {
63 70
     default_aes <- aes_(x=~x, y=~y, node=~node, parent=~parent, branch.length=~branch.length)
64 71
     if (is.null(mapping)) {
... ...
@@ -76,8 +83,9 @@ stat_hilight <- function(mapping=NULL, data=NULL, geom="rect",
76 83
         show.legend=show.legend,
77 84
         inherit.aes = inherit.aes,
78 85
         params = list(node=node,
79
-            fill=fill, alpha=alpha,
80
-            ...)
86
+                      fill=fill, alpha=alpha,
87
+                      extend=extend, extendto=extendto,
88
+                      ...)
81 89
         )
82 90
 }
83 91
 
... ...
@@ -88,8 +96,17 @@ stat_hilight <- function(mapping=NULL, data=NULL, geom="rect",
88 96
 ##' @importFrom ggplot2 Stat
89 97
 ##' @export
90 98
 StatHilight <- ggproto("StatHilight", Stat,
91
-                       compute_group = function(self, data, scales, params, node) {
92
-                           get_clade_position_(data, node)
99
+                       compute_group = function(self, data, scales, params, node, extend, extendto) {
100
+                           df <- get_clade_position_(data, node)
101
+                           df$xmax <- df$xmax + extend
102
+                           if (!is.null(extendto) && !is.na(extendto)) {
103
+                               if (extendto < df$xmax) {
104
+                                   warning("extendto is too small, keep the original xmax value...")
105
+                               } else {
106
+                                   df$xmax <- extendto
107
+                               }
108
+                           }
109
+                           return(df)
93 110
                        },
94 111
                        required_aes = c("x", "y", "branch.length")
95 112
                        )
... ...
@@ -109,14 +126,22 @@ get_clade_position <- function(treeview, node) {
109 126
 }
110 127
 
111 128
 get_clade_position_ <- function(data, node) {
112
-    sp <- get.offspring.df(data, node)
113
-    ## sp.df <- data[c(sp, node),]
114
-    sp <- c(sp, node)
115
-    sp.df <- data[match(sp, data$node),]
129
+    sp <- tryCatch(get.offspring.df(data, node), error=function(e) NULL)
130
+
131
+    i <- match(node, data$node)
132
+    if (is.null(sp)) {
133
+        ## tip
134
+        sp.df <- data[i,]
135
+    } else {
136
+        sp <- c(sp, node)
137
+        sp.df <- data[match(sp, data$node),]
138
+    }
139
+
116 140
     x <- sp.df$x
117 141
     y <- sp.df$y
142
+    
118 143
     if ("branch.length" %in% colnames(data)) {
119
-        xmin <- min(x)-data[match(node, data$node), "branch.length"]/2
144
+        xmin <- min(x)-data[i, "branch.length"]/2
120 145
     } else {
121 146
         xmin <- min(sp.df$branch)
122 147
     }
... ...
@@ -4,7 +4,8 @@
4 4
 \alias{geom_hilight}
5 5
 \title{geom_hilight}
6 6
 \usage{
7
-geom_hilight(node, fill = "steelblue", alpha = 0.5)
7
+geom_hilight(node, fill = "steelblue", alpha = 0.5, extend = 0,
8
+  extendto = NULL)
8 9
 }
9 10
 \arguments{
10 11
 \item{node}{selected node to hilight}
... ...
@@ -12,6 +13,10 @@ geom_hilight(node, fill = "steelblue", alpha = 0.5)
12 13
 \item{fill}{color fill}
13 14
 
14 15
 \item{alpha}{alpha (transparency)}
16
+
17
+\item{extend}{extend xmax of the rectangle}
18
+
19
+\item{extendto}{extend xmax to extendto}
15 20
 }
16 21
 \value{
17 22
 ggplot2
... ...
@@ -6,7 +6,7 @@
6 6
 \usage{
7 7
 stat_hilight(mapping = NULL, data = NULL, geom = "rect",
8 8
   position = "identity", node, show.legend = NA, inherit.aes = FALSE,
9
-  fill, alpha, ...)
9
+  fill, alpha, extend = 0, xmax = NULL, ...)
10 10
 }
11 11
 \arguments{
12 12
 \item{mapping}{aes mapping}
... ...
@@ -27,7 +27,11 @@ stat_hilight(mapping = NULL, data = NULL, geom = "rect",
27 27
 
28 28
 \item{alpha}{transparency}
29 29
 
30
+\item{extend}{extend xmax of the rectangle}
31
+
30 32
 \item{...}{additional parameter}
33
+
34
+\item{extendto}{extend xmax to extendto}
31 35
 }
32 36
 \value{
33 37
 layer