Browse code

update geom_striplab

xiangpin authored on 22/04/2022 08:27:22
Showing 1 changed files
... ...
@@ -59,6 +59,161 @@ geom_strip <- function(taxa1, taxa2, label, offset=0, offset.text=0,
59 59
               class = "striplabel")
60 60
 }
61 61
 
62
+#' annotate associated taxa (from taxa1 to taxa2, can be Monophyletic, Polyphyletic or Paraphyletc Taxa) with bar and (optional) text label or image
63
+#'
64
+#' @title geom_striplab
65
+#' @param taxa1 can be label or node number
66
+#' @param taxa2 can be label or node number
67
+#' @param label character, character to be showed, when data and mapping is NULL, it is required.
68
+#' @param data data.frame, the data to be displayed in the annotation, default is NULL.
69
+#' @param mapping Set of aesthetic mappings, default is NULL. The detail see the following explanation.
70
+#' @param geom character, one of 'text', 'label', 'shadowtext', 'image' and 'phylopic',
71
+#' default is 'text', and the parameter see the Aesthetics For Specified Geom.
72
+#' @param parse logical, whether parse label to emoji font, default is FALSE.
73
+#' @param ... additional parameters, see also following section.
74
+#'
75
+#' additional parameters can refer the following parameters.                                                                                                                                                      ##'     \itemize{
76
+#'        \item \code{offset} distance bar and tree, offset of bar and text from
77
+#'         the clade, default is 0.
78
+#'        \item \code{offset.text} distance bar and text, offset of text from bar,
79
+#'         default is 0.
80
+#'        \item \code{align} logical, whether align clade lab, default is FALSE.
81
+#'        \item \code{extend} numeric, extend the length of bar, default is 0.
82
+#'        \item \code{angle} numeric or 'auto', if angle is auto, the angle of text will
83
+#'         be calculated automatically, which is useful for the circular etc layout, default is 0.
84
+#'        \item \code{horizontal} logical, whether set label to horizontal, default is TRUE.
85
+#'        \item \code{barsize} the width of line, default is 0.5.
86
+#'        \item \code{barcolour} the colour of line, default is 'black'.
87
+#'        \item \code{fontsize} the size of text, default is 3.88.
88
+#'        \item \code{textcolour} the colour of text, default is 'black'.
89
+#'        \item \code{imagesize} the size of image, default is 0.05.
90
+#'        \item \code{imagecolor} the colour of image, default is NULL, when
91
+#'        geom="phylopic", it should be required.
92
+#'     }
93
+#' The parameters also can be set in mapping, when data is provided. Note: the barsize, barcolour,
94
+#' fontsize, textcolour, imagesize and imagecolor should not be set in mapping (aesthetics). When
95
+#' the color and size are not be set in mapping, user can modify them to adjust the attributes of
96
+#' specified geom.
97
+#'
98
+#' @section Aesthetics For Specified Geom:
99
+#' \code{geom_striplab()} understands the following aesthetics for geom="text"(required
100
+#' aesthetics are in bold):
101
+#'     \itemize{
102
+#'        \item \strong{\code{taxa1}} selected tip label or tip node, it is required.
103
+#'        \item \strong{\code{taxa2}} selected another tip label or tip node, it is required.
104
+#'        \item \strong{\code{label}} labels to be shown, it is required.
105
+#'        \item \code{colour} the colour of text, default is "black".
106
+#'        \item \code{size} the size of text, default is 3.88.
107
+#'        \item \code{angle} the angle of text, default is 0.
108
+#'        \item \code{hjust} A numeric vector specifying horizontal justification, default is 0.
109
+#'        \item \code{vjust} A numeric vector specifying vertical justification, default is 0.5.
110
+#'        \item \code{alpha} the transparency of text, default is NA.
111
+#'        \item \code{family} the family of text, default is 'sans'.
112
+#'        \item \code{fontface} the font face of text, default is 1 (plain), others are
113
+#'         2 (bold), 3 (italic), 4 (bold.italic).
114
+#'        \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2 .
115
+#'     }
116
+#'  when the colour, size are not be set in mapping, and user want to modify the colour of text,
117
+#'  they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
118
+#'
119
+#' \code{geom_striplab()} understands the following aesthethics for geom="label" (required
120
+#' aesthetics are in bold):
121
+#'     \itemize{
122
+#'        \item \strong{\code{taxa1}} selected node to hight light, it is required.
123
+#'        \item \strong{\code{taxa2}} selected another tip label or tip node, it is required.
124
+#'        \item \strong{\code{label}} labels to be shown, it is required.
125
+#'        \item \code{colour} the colour of text, default is "black".
126
+#'        \item \code{fill} the background colour of the label, default is "white".
127
+#'        \item \code{size} the size of text, default is 3.88.
128
+#'        \item \code{angle} the angle of text, default is 0.
129
+#'        \item \code{hjust} A numeric vector specifying horizontal justification, default is 0.
130
+#'        \item \code{vjust} A numeric vector specifying vertical justification, default is 0.5.
131
+#'        \item \code{alpha} the transparency of text, default is NA.
132
+#'        \item \code{family} the family of text, default is 'sans'.
133
+#'        \item \code{fontface} the font face of text, default is 1 (plain), others are
134
+#'         2 (bold), 3 (italic), 4 (bold.italic).
135
+#'        \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2 .
136
+#'     }
137
+#'  when the colour, size are not be set in mapping, and user want to modify the colour of text,
138
+#'  they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
139
+#'
140
+#' \code{geom_striplab()} understands the following aesthethics for geom="shadowtext" (required
141
+#' aesthetics are in bold):
142
+#'     \itemize{
143
+#'        \item \strong{\code{taxa1}} selected node to hight light, it is required.
144
+#'        \item \strong{\code{taxa2}} selected another tip label or tip node, it is required.
145
+#'        \item \strong{\code{label}} labels to be shown, it is required.
146
+#'        \item \code{colour} the colour of text, default is "black".
147
+#'        \item \code{bg.colour} the background colour of text, default is 'black'.
148
+#'        \item \code{bg.r} the width of background text, default is 0.1.
149
+#'        \item \code{size} the size of text, default is 3.88.
150
+#'        \item \code{angle} the angle of text, default is 0.
151
+#'        \item \code{hjust} A numeric vector specifying horizontal justification, default is 0.
152
+#'        \item \code{vjust} A numeric vector specifying vertical justification, default is 0.5.
153
+#'        \item \code{alpha} the transparency of text, default is NA.
154
+#'        \item \code{family} the family of text, default is 'sans'.
155
+#'        \item \code{fontface} the font face of text, default is 1 (plain), others are
156
+#'         2 (bold), 3 (italic), 4 (bold.italic).
157
+#'        \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2 .
158
+#'     }
159
+#'  when the colour, size are not be set in mapping, and user want to modify the colour of text,
160
+#'  they should use textcolour, fontsize to avoid the confusion with bar layer annotation.
161
+#'
162
+#' \code{geom_striplab()} understands the following aesthethics for geom="image" or geom="phylopic" (required
163
+#' aesthetics are in bold):
164
+#'     \itemize{
165
+#'        \item \strong{\code{taxa1}} selected node to hight light, it is required.
166
+#'        \item \strong{\code{taxa2}} selected another tip label or tip node, it is required.
167
+#'        \item \strong{\code{label}} labels to be shown, it is required.
168
+#'        \item \strong{\code{image}} the image to be annotated, when geom="phylopic",
169
+#'         the uid of phylopic databases, it is required.
170
+#'        \item \code{colour} the color of image, default is NULL.
171
+#'        \item \code{size} the size of image, default is 0.05.
172
+#'        \item \code{alpha} the alpha of image, default is 0.8.
173
+#'     }
174
+#'  when the colour, size are not be set in mapping, and user want to modify the colour of image,
175
+#'  they should use imagecolour, imagesize to avoid the confusion with bar layer annotation.
176
+#' @export
177
+#' @examples
178
+#' set.seed(123)
179
+#' tr <- rtree(10)
180
+#' dt <- data.frame(ta1=c("t5", "t1"), ta2=c("t6", "t3"), group=c("A", "B"))
181
+#' p <- ggtree(tr) + geom_tiplab()
182
+#' p2 <- p + 
183
+#'       geom_striplab(
184
+#'         data = dt,
185
+#'         mapping = aes(taxa1 = ta1, taxa2 = ta2, 
186
+#'                       label = group, color=group),
187
+#'         align = TRUE,
188
+#'         show.legend = FALSE
189
+#'       )
190
+#' p2
191
+geom_striplab <- function(
192
+               taxa1 = NULL, 
193
+               taxa2 = NULL,
194
+               label = NULL,
195
+               data = NULL,
196
+               mapping = NULL,
197
+               geom = "text",
198
+               parse = FALSE,
199
+               ...
200
+    ){
201
+
202
+    params <- list(inherit.aes = FALSE, ...)
203
+    structure(
204
+       list(
205
+         data = data, 
206
+         mapping = mapping, 
207
+         taxa1 = taxa1,
208
+         taxa2 = taxa2,
209
+         label = label, 
210
+         geom = geom, 
211
+         parse = parse, 
212
+         params = params
213
+       ),
214
+       class = "striplab")
215
+
216
+}
62 217
 
63 218
 ## geom_strip <- function(taxa1, taxa2, label, offset=0, offset.text=0,
64 219
 ##                            align=TRUE, barsize=0.5, extend=0, fontsize=3.88,
... ...
@@ -183,6 +338,44 @@ geom_strip <- function(taxa1, taxa2, label, offset=0, offset.text=0,
183 338
 ##                         required_aes = c("x", "y", "xend", "yend")
184 339
 ##                         )
185 340
 
341
+get_striplab_position <- function(data, taxa1, taxa2, offset, angle="auto", 
342
+                                  align = TRUE, extend = 0, adjustRatio = 1.02, 
343
+                                  horizontal = TRUE){
344
+    df <- get_striplab_position_(data = data, taxa1 = taxa1, taxa2 = taxa2, 
345
+                                 angle = angle, extend = extend, horizontal = horizontal)
346
+    if (align){
347
+        mx <- max(data$x, na.rm = TRUE)
348
+    }else{
349
+        mx <- df$x
350
+    }
351
+    angle <- df$angle
352
+    mx <- mx * adjustRatio + offset
353
+    data.frame(x = mx, xend = mx, y = df$y, yend=df$yend, angle = angle)
354
+}
355
+
356
+get_striplab_position_ <- function(data, taxa1, taxa2, angle, extend = 0, horizontal){
357
+    if (length(extend) == 1) {
358
+        extend <- rep(extend, 2)
359
+    }
360
+
361
+    node1 <- taxa2node(data, taxa1)
362
+    node2 <- taxa2node(data, taxa2)
363
+
364
+    xx <- with(data, c(x[node == node1], x[node == node2]))
365
+    yy <- with(data, c(y[node == node1], y[node == node2]))
366
+
367
+    d <- data.frame(x=max(xx), y=min(yy)-extend[2], yend=max(yy)+extend[1])
368
+    if (missing(angle))
369
+        return(d)
370
+    if (angle == "auto") {
371
+        anglerange <- with(data, c(angle[node == node1], angle[node == node2]))
372
+        d$angle <- mean(anglerange)
373
+        d$angle <- adjust_cladelabel_angle(angle=d$angle, horizontal=horizontal)
374
+    } else {
375
+        d$angle <- angle
376
+    }
377
+    return(d)    
378
+}
186 379
 
187 380
 get_striplabel_position <- function(data, taxa1, taxa2, offset, align, barextend, adjustRatio) {
188 381
     df <- get_striplabel_position_(data, taxa1, taxa2, barextend)
Browse code

Document update

Document update

William Lee authored on 22/03/2022 14:46:29
Showing 1 changed files
... ...
@@ -4,24 +4,34 @@
4 4
 ##' @title geom_strip
5 5
 ##' @param taxa1 taxa1
6 6
 ##' @param taxa2 taxa2
7
-##' @param label optional label
7
+##' @param label add label alongside the bar (optional)
8 8
 ##' @param offset offset of bar and text from the clade
9 9
 ##' @param offset.text offset of text from bar
10
-##' @param align logical
11
-##' @param barsize size of bar
12
-##' @param extend extend bar vertically
13
-##' @param fontsize size of text
14
-##' @param angle angle of text
10
+##' @param align logical, whether to align bars to the most distant bar ,defaults to "TRUE"
11
+##' Note that if "FALSE", the bars might cross the tree
12
+##' @param barsize set size of the bar
13
+##' @param extend extend bar length vertically
14
+##' @param fontsize set size of the text
15
+##' @param angle set the angle of text
15 16
 ##' @param geom one of 'text' or 'label'
16
-##' @param hjust hjust
17
-##' @param color color for bar and label
18
-##' @param fill fill label background, only work with geom='label'
19
-##' @param family sans by default, can be any supported font
20
-##' @param parse logical, whether parse label
17
+##' @param hjust adjust the horizonal position of the bar
18
+##' @param color set color for bar and label
19
+##' @param fill set color to fill label background, only work with geom='label'
20
+##' @param family "sans" by default, can be any supported font
21
+##' @param parse logical, whether to parse labels, if "TRUE", the labels will be parsed into expressions, defaults to "FALSE"
21 22
 ##' @param ... additional parameter
22 23
 ##' @return ggplot layers
23 24
 ##' @export
24 25
 ##' @author Guangchuang Yu
26
+##' @examples
27
+##' library(ggtree)
28
+##' tr<- rtree(15)
29
+##' x <- ggtree(tr)
30
+##' x + geom_strip(13, 1, color = "red") + geom_strip(3, 7, color = "blue")
31
+##' @references
32
+##' For more detailed demonstration of this function, please refer to chapter 5.2.1 of 
33
+##' *Data Integration, Manipulation and Visualization of Phylogenetic Trees*
34
+##' <http://yulab-smu.top/treedata-book/index.html> by Guangchuang Yu.
25 35
 geom_strip <- function(taxa1, taxa2, label, offset=0, offset.text=0,
26 36
                        align=TRUE, barsize=0.5, extend=0, fontsize=3.88,
27 37
                        angle=0, geom="text", hjust=0, color = 'black', fill=NA, family="sans",
Browse code

enable color parameter for geom_strip

Guangchuang Yu authored on 28/08/2019 03:56:03
Showing 1 changed files
... ...
@@ -14,6 +14,7 @@
14 14
 ##' @param angle angle of text
15 15
 ##' @param geom one of 'text' or 'label'
16 16
 ##' @param hjust hjust
17
+##' @param color color for bar and label
17 18
 ##' @param fill fill label background, only work with geom='label'
18 19
 ##' @param family sans by default, can be any supported font
19 20
 ##' @param parse logical, whether parse label
... ...
@@ -23,7 +24,7 @@
23 24
 ##' @author Guangchuang Yu
24 25
 geom_strip <- function(taxa1, taxa2, label, offset=0, offset.text=0,
25 26
                        align=TRUE, barsize=0.5, extend=0, fontsize=3.88,
26
-                       angle=0, geom="text", hjust=0, fill=NA, family="sans",
27
+                       angle=0, geom="text", hjust=0, color = 'black', fill=NA, family="sans",
27 28
                        parse=FALSE, ...) {
28 29
 
29 30
     if (missing(label)) label <- NA
... ...
@@ -40,6 +41,7 @@ geom_strip <- function(taxa1, taxa2, label, offset=0, offset.text=0,
40 41
                    angle = angle,
41 42
                    geom = geom,
42 43
                    hjust = hjust,
44
+                   color = color,
43 45
                    fill = fill,
44 46
                    family = family,
45 47
                    parse = parse,
Browse code

geom_strip

Guangchuang Yu authored on 27/08/2019 05:12:46
Showing 1 changed files
... ...
@@ -21,144 +21,155 @@
21 21
 ##' @return ggplot layers
22 22
 ##' @export
23 23
 ##' @author Guangchuang Yu
24
-geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
24
+geom_strip <- function(taxa1, taxa2, label, offset=0, offset.text=0,
25 25
                        align=TRUE, barsize=0.5, extend=0, fontsize=3.88,
26 26
                        angle=0, geom="text", hjust=0, fill=NA, family="sans",
27 27
                        parse=FALSE, ...) {
28
-    mapping <- NULL
29
-    data <- NULL
30
-    position <- "identity"
31
-    show.legend <- NA
32
-    na.rm <- TRUE
33
-    inherit.aes <- FALSE
34
-
35
-    layer_bar <- stat_stripBar(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset, align=align,
36
-                               size=barsize, barextend=extend,
37
-                               mapping=mapping, data=data,
38
-                               position=position, show.legend = show.legend,
39
-                               inherit.aes = inherit.aes, na.rm=na.rm, ...)
40
-
41
-    if (is.na(label) || is.null(label)) {
42
-        return(layer_bar)
43
-    }
44
-
45
-    if (geom == "text") {
46
-        ## no fill parameter
47
-        layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
48
-                                    align=align, size=fontsize, barextend=extend, angle=angle, family=family,
49
-                                    mapping=mapping, data=data, geom=geom, hjust=hjust,
50
-                                    position=position, show.legend = show.legend,
51
-                                    inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
52
-
53
-    } else {
54
-        layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
55
-                                    align=align, size=fontsize, barextend=extend, angle=angle, fill=fill,family=family,
56
-                                    mapping=mapping, data=data, geom=geom, hjust=hjust,
57
-                                    position=position, show.legend = show.legend,
58
-                                    inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
59
-    }
60 28
 
61
-    list(
62
-        layer_bar,
63
-        layer_text
64
-    )
29
+    if (missing(label)) label <- NA
30
+
31
+    structure(list(taxa1 = taxa1,
32
+                   taxa2 = taxa2,
33
+                   label = label,
34
+                   offset = offset,
35
+                   offset.text = offset.text,
36
+                   align = align,
37
+                   barsize = barsize,
38
+                   extend = extend,
39
+                   fontsize = fontsize,
40
+                   angle = angle,
41
+                   geom = geom,
42
+                   hjust = hjust,
43
+                   fill = fill,
44
+                   family = family,
45
+                   parse = parse,
46
+                   params = list(...)),
47
+              class = "striplabel")
65 48
 }
66 49
 
67 50
 
68
-stat_stripText <- function(mapping=NULL, data=NULL,
69
-                           geom="text", position="identity",
70
-                           taxa1, taxa2, label, offset, align, barextend, ...,
71
-                           show.legend=NA, inherit.aes=FALSE, na.rm=FALSE, parse=FALSE) {
72
-
73
-    if (is.null(label) || is.na(label)) {
74
-        default_aes <- aes_(x=~x, y=~y, node=~node, label=~label)
75
-    } else {
76
-        default_aes <- aes_(x=~x, y=~y, node=~node)
77
-    }
78
-
79
-    if (is.null(mapping)) {
80
-        mapping <- default_aes
81
-    } else {
82
-        mapping <- modifyList(mapping, default_aes)
83
-    }
84
-
85
-    layer(stat=StatStripText,
86
-          data=data,
87
-          mapping=mapping,
88
-          geom=geom,
89
-          position=position,
90
-          show.legend = show.legend,
91
-          inherit.aes = inherit.aes,
92
-          params=list(taxa1=taxa1,
93
-                      taxa2=taxa2,
94
-                      label=label,
95
-                      offset=offset,
96
-                      align=align,
97
-                      barextend=barextend,
98
-                      na.rm=na.rm,
99
-                      parse=parse,
100
-                      ...),
101
-          check.aes = FALSE
102
-          )
103
-
104
-}
105
-
106
-stat_stripBar <- function(mapping=NULL, data=NULL,
107
-                          geom="segment", position="identity",
108
-                          taxa1, taxa2, label=label, offset, align, barextend, ...,
109
-                          show.legend=NA, inherit.aes=FALSE, na.rm=FALSE) {
110
-
111
-    if (is.null(label) || is.na(label)) {
112
-        default_aes <- aes_(x=~x, y=~y, node=~node, label=~label, xend=~x, yend=~y)
113
-    } else {
114
-        default_aes <- aes_(x=~x, y=~y, node=~node, xend=~x, yend=~y)
115
-    }
116
-
117
-    if (is.null(mapping)) {
118
-        mapping <- default_aes
119
-    } else {
120
-        mapping <- modifyList(mapping, default_aes)
121
-    }
122
-
123
-    layer(stat=StatStripBar,
124
-          data=data,
125
-          mapping=mapping,
126
-          geom=geom,
127
-          position=position,
128
-          show.legend = show.legend,
129
-          inherit.aes = inherit.aes,
130
-          params=list(taxa1=taxa1,
131
-                      taxa2=taxa2,
132
-                      offset=offset,
133
-                      align=align,
134
-                      barextend=barextend,
135
-                      na.rm=na.rm,
136
-                      ...),
137
-          check.aes = FALSE
138
-          )
139
-
140
-}
141
-
142
-StatStripText <- ggproto("StatStripText", Stat,
143
-                         compute_group = function(self, data, scales, params, taxa1, taxa2,
144
-                                                  label, offset, align, barextend) {
145
-                             df <- get_striplabel_position(data, taxa1, taxa2, offset, align, barextend, adjustRatio = 1.03)
146
-                             df$y <- mean(c(df$y, df$yend))
147
-                             df$label <- label
148
-                             return(df)
149
-                         },
150
-                         required_aes = c("x", "y", "label")
151
-                         )
152
-
153
-
154
-
155
-StatStripBar <- ggproto("StatStripBar", Stat,
156
-                        compute_group = function(self, data, scales, params,
157
-                                                 taxa1, taxa2, offset, align, barextend) {
158
-                            get_striplabel_position(data, taxa1, taxa2, offset, align, barextend, adjustRatio=1.02)
159
-                        },
160
-                        required_aes = c("x", "y", "xend", "yend")
161
-                        )
51
+## geom_strip <- function(taxa1, taxa2, label, offset=0, offset.text=0,
52
+##                            align=TRUE, barsize=0.5, extend=0, fontsize=3.88,
53
+##                            angle=0, geom="text", hjust=0, fill=NA, family="sans",
54
+##                            parse=FALSE, ...) {
55
+
56
+##     mapping <- aes_(x=~x, y=~y, node=~node, label = ~label, xend=~x, yend=~y)
57
+##     data <- NULL
58
+##     position <- "identity"
59
+##     show.legend <- NA
60
+##     na.rm <- TRUE
61
+##     inherit.aes <- FALSE
62
+
63
+##     layer_bar <- stat_stripBar(taxa1=taxa1, taxa2=taxa2, offset=offset, align=align,
64
+##                                size=barsize, barextend=extend,
65
+##                                mapping=mapping, data=data,
66
+##                                position=position, show.legend = show.legend,
67
+##                                inherit.aes = inherit.aes, na.rm=na.rm, ...)
68
+
69
+##     if (missing(label) || is.na(label) || is.null(label)) {
70
+##         return(layer_bar)
71
+##     }
72
+
73
+##     if (geom == "text") {
74
+##         ## no fill parameter
75
+##         layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
76
+##                                      align=align, size=fontsize, barextend=extend, angle=angle, family=family,
77
+##                                      mapping=mapping, data=data, geom=geom, hjust=hjust,
78
+##                                      position=position, show.legend = show.legend,
79
+##                                      inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
80
+
81
+##     } else {
82
+##         layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
83
+##                                      align=align, size=fontsize, barextend=extend, angle=angle,
84
+##                                      fill=fill,family=family,
85
+##                                      mapping=mapping, data=data, geom=geom, hjust=hjust,
86
+##                                      position=position, show.legend = show.legend,
87
+##                                      inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
88
+##     }
89
+
90
+##     list(
91
+##         layer_bar,
92
+##         layer_text
93
+##     )
94
+## }
95
+
96
+
97
+## stat_stripText <- function(mapping=NULL, data=NULL,
98
+##                            geom="text", position="identity",
99
+##                            taxa1, taxa2, label, offset, align, barextend, ...,
100
+##                            show.legend=NA, inherit.aes=FALSE, na.rm=FALSE, parse=FALSE) {
101
+
102
+##     layer(stat=StatStripText,
103
+##           data=data,
104
+##           mapping=mapping,
105
+##           geom=geom,
106
+##           position=position,
107
+##           show.legend = show.legend,
108
+##           inherit.aes = inherit.aes,
109
+##           params=list(taxa1=taxa1,
110
+##                       taxa2=taxa2,
111
+##                       label=label,
112
+##                       offset=offset,
113
+##                       align=align,
114
+##                       barextend=barextend,
115
+##                       na.rm=na.rm,
116
+##                       parse=parse,
117
+##                       ...),
118
+##           check.aes = FALSE
119
+##           )
120
+
121
+## }
122
+
123
+## stat_stripBar <- function(mapping=NULL, data=NULL,
124
+##                           geom="segment", position="identity",
125
+##                           taxa1, taxa2, offset, align, barextend, ...,
126
+##                           show.legend=NA, inherit.aes=FALSE, na.rm=FALSE) {
127
+
128
+##     layer(stat=StatStripBar,
129
+##           data=data,
130
+##           mapping=mapping,
131
+##           geom=geom,
132
+##           position=position,
133
+##           show.legend = show.legend,
134
+##           inherit.aes = inherit.aes,
135
+##           params=list(taxa1=taxa1,
136
+##                       taxa2=taxa2,
137
+##                       offset=offset,
138
+##                       align=align,
139
+##                       barextend=barextend,
140
+##                       na.rm=na.rm,
141
+##                       ...),
142
+##           check.aes = FALSE
143
+##           )
144
+
145
+## }
146
+
147
+## StatStripText <- ggproto("StatStripText", Stat,
148
+##                          compute_group = function(self, data, scales, params, taxa1, taxa2,
149
+##                                                   label, offset, align, barextend) {
150
+##                              print('text' )
151
+##                              print(data )
152
+##                              df <- get_striplabel_position(data, taxa1, taxa2, offset, align,
153
+##                                                            barextend, adjustRatio = 1.03)
154
+##                              df$y <- mean(c(df$y, df$yend))
155
+##                              df$label <- label
156
+##                              return(df)
157
+##                          },
158
+##                          required_aes = c("x", "y", "label")
159
+##                          )
160
+
161
+
162
+
163
+## StatStripBar <- ggproto("StatStripBar", Stat,
164
+##                         compute_group = function(self, data, scales, params,
165
+##                                                  taxa1, taxa2, offset, align, barextend) {
166
+##                             print('bar' )
167
+##                             print(data )
168
+##                             get_striplabel_position(data, taxa1, taxa2, offset,
169
+##                                                     align, barextend, adjustRatio=1.02)
170
+##                         },
171
+##                         required_aes = c("x", "y", "xend", "yend")
172
+##                         )
162 173
 
163 174
 
164 175
 get_striplabel_position <- function(data, taxa1, taxa2, offset, align, barextend, adjustRatio) {
... ...
@@ -177,6 +188,7 @@ get_striplabel_position_ <- function(data, taxa1, taxa2, barextend=0) {
177 188
     if (length(barextend) == 1) {
178 189
         barextend <- rep(barextend, 2)
179 190
     }
191
+
180 192
     node1 <- taxa2node(data, taxa1)
181 193
     node2 <- taxa2node(data, taxa2)
182 194
 
... ...
@@ -188,15 +200,21 @@ get_striplabel_position_ <- function(data, taxa1, taxa2, barextend=0) {
188 200
 
189 201
 ## used in geom_strip, geom_taxalink
190 202
 taxa2node <- function(data, taxa) {
191
-    if (! 'label' %in% colnames(data))
192
-        data$label <- NA
203
+    ## if (! 'label' %in% colnames(data))
204
+    ##     data$label <- NA
193 205
 
194
-    idx <- with(data, which(taxa == label | taxa == node))
206
+    ## idx <- which(taxa == data$label | taxa == data$node)
195 207
 
196
-    if (length(idx) == 0) {
197
-        stop("input taxa is not valid...")
198
-    }
208
+    ## if (length(idx) == 0) {
209
+    ##     print(taxa )
210
+    ##     print(data )
211
+    ##     stop("input taxa is not valid...")
212
+    ## }
213
+
214
+    ## return(data$node[idx])
215
+    if (is.numeric(taxa))
216
+        return(taxa)
199 217
 
200
-    return(data$node[idx])
218
+    nodeid.tbl_tree(data, taxa)
201 219
 }
202 220
 
Browse code

extend parameter in geom_cladelabel and geom_strip

guangchuang yu authored on 06/07/2018 07:23:52
Showing 1 changed files
... ...
@@ -9,7 +9,7 @@
9 9
 ##' @param offset.text offset of text from bar
10 10
 ##' @param align logical
11 11
 ##' @param barsize size of bar
12
-##' @param barextend extend bar vertically
12
+##' @param extend extend bar vertically
13 13
 ##' @param fontsize size of text
14 14
 ##' @param angle angle of text
15 15
 ##' @param geom one of 'text' or 'label'
... ...
@@ -22,7 +22,7 @@
22 22
 ##' @export
23 23
 ##' @author Guangchuang Yu
24 24
 geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
25
-                       align=TRUE, barsize=0.5, barextend=0, fontsize=3.88,
25
+                       align=TRUE, barsize=0.5, extend=0, fontsize=3.88,
26 26
                        angle=0, geom="text", hjust=0, fill=NA, family="sans",
27 27
                        parse=FALSE, ...) {
28 28
     mapping <- NULL
... ...
@@ -33,7 +33,7 @@ geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
33 33
     inherit.aes <- FALSE
34 34
 
35 35
     layer_bar <- stat_stripBar(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset, align=align,
36
-                               size=barsize, barextend=barextend,
36
+                               size=barsize, barextend=extend,
37 37
                                mapping=mapping, data=data,
38 38
                                position=position, show.legend = show.legend,
39 39
                                inherit.aes = inherit.aes, na.rm=na.rm, ...)
... ...
@@ -45,14 +45,14 @@ geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
45 45
     if (geom == "text") {
46 46
         ## no fill parameter
47 47
         layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
48
-                                    align=align, size=fontsize, barextend=barextend, angle=angle, family=family,
48
+                                    align=align, size=fontsize, barextend=extend, angle=angle, family=family,
49 49
                                     mapping=mapping, data=data, geom=geom, hjust=hjust,
50 50
                                     position=position, show.legend = show.legend,
51 51
                                     inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
52 52
 
53 53
     } else {
54 54
         layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
55
-                                    align=align, size=fontsize, barextend=barextend, angle=angle, fill=fill,family=family,
55
+                                    align=align, size=fontsize, barextend=extend, angle=angle, fill=fill,family=family,
56 56
                                     mapping=mapping, data=data, geom=geom, hjust=hjust,
57 57
                                     position=position, show.legend = show.legend,
58 58
                                     inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
... ...
@@ -174,13 +174,16 @@ get_striplabel_position <- function(data, taxa1, taxa2, offset, align, barextend
174 174
 
175 175
 
176 176
 get_striplabel_position_ <- function(data, taxa1, taxa2, barextend=0) {
177
+    if (length(barextend) == 1) {
178
+        barextend <- rep(barextend, 2)
179
+    }
177 180
     node1 <- taxa2node(data, taxa1)
178 181
     node2 <- taxa2node(data, taxa2)
179 182
 
180 183
     xx <- with(data, c(x[node == node1], x[node == node2]))
181 184
     yy <- with(data, c(y[node == node1], y[node == node2]))
182 185
 
183
-    data.frame(x=max(xx), y=min(yy)-barextend, yend=max(yy)+barextend)
186
+    data.frame(x=max(xx), y=min(yy)-barextend[2], yend=max(yy)+barextend[1])
184 187
 }
185 188
 
186 189
 ## used in geom_strip, geom_taxalink
Browse code

ggplot2 2.2.0

guangchuang yu authored on 14/11/2016 04:41:23
Showing 1 changed files
... ...
@@ -98,7 +98,7 @@ stat_stripText <- function(mapping=NULL, data=NULL,
98 98
                       na.rm=na.rm,
99 99
                       parse=parse,
100 100
                       ...),
101
-          if (packageVersion('ggplot2') > '2.1.0') check.aes = FALSE
101
+          check.aes = FALSE
102 102
           )
103 103
 
104 104
 }
... ...
@@ -134,7 +134,7 @@ stat_stripBar <- function(mapping=NULL, data=NULL,
134 134
                       barextend=barextend,
135 135
                       na.rm=na.rm,
136 136
                       ...),
137
-          if (packageVersion('ggplot2') > '2.1.0') check.aes = FALSE
137
+          check.aes = FALSE
138 138
           )
139 139
 
140 140
 }
Browse code

fixed aes mapping in geom_strip <2016-10-11, Tue>

guangchuang yu authored on 11/10/2016 06:41:22
Showing 1 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 ##' annotate associated taxa (from taxa1 to taxa2, can be Monophyletic, Polyphyletic or Paraphyletc Taxa) with bar and (optional) text label
2 2
 ##'
3
-##' 
3
+##'
4 4
 ##' @title geom_strip
5 5
 ##' @param taxa1 taxa1
6 6
 ##' @param taxa2 taxa2
... ...
@@ -34,14 +34,14 @@ geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
34 34
 
35 35
     layer_bar <- stat_stripBar(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset, align=align,
36 36
                                size=barsize, barextend=barextend,
37
-                               mapping=mapping, data=data, 
37
+                               mapping=mapping, data=data,
38 38
                                position=position, show.legend = show.legend,
39 39
                                inherit.aes = inherit.aes, na.rm=na.rm, ...)
40 40
 
41 41
     if (is.na(label) || is.null(label)) {
42 42
         return(layer_bar)
43 43
     }
44
-    
44
+
45 45
     if (geom == "text") {
46 46
         ## no fill parameter
47 47
         layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
... ...
@@ -49,7 +49,7 @@ geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
49 49
                                     mapping=mapping, data=data, geom=geom, hjust=hjust,
50 50
                                     position=position, show.legend = show.legend,
51 51
                                     inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
52
-        
52
+
53 53
     } else {
54 54
         layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
55 55
                                     align=align, size=fontsize, barextend=barextend, angle=angle, fill=fill,family=family,
... ...
@@ -57,7 +57,7 @@ geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
57 57
                                     position=position, show.legend = show.legend,
58 58
                                     inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
59 59
     }
60
-    
60
+
61 61
     list(
62 62
         layer_bar,
63 63
         layer_text
... ...
@@ -75,13 +75,13 @@ stat_stripText <- function(mapping=NULL, data=NULL,
75 75
     } else {
76 76
         default_aes <- aes_(x=~x, y=~y, node=~node)
77 77
     }
78
-    
78
+
79 79
     if (is.null(mapping)) {
80 80
         mapping <- default_aes
81 81
     } else {
82 82
         mapping <- modifyList(mapping, default_aes)
83 83
     }
84
-    
84
+
85 85
     layer(stat=StatStripText,
86 86
           data=data,
87 87
           mapping=mapping,
... ...
@@ -100,7 +100,7 @@ stat_stripText <- function(mapping=NULL, data=NULL,
100 100
                       ...),
101 101
           if (packageVersion('ggplot2') > '2.1.0') check.aes = FALSE
102 102
           )
103
-    
103
+
104 104
 }
105 105
 
106 106
 stat_stripBar <- function(mapping=NULL, data=NULL,
... ...
@@ -111,7 +111,7 @@ stat_stripBar <- function(mapping=NULL, data=NULL,
111 111
     if (is.null(label) || is.na(label)) {
112 112
         default_aes <- aes_(x=~x, y=~y, node=~node, label=~label, xend=~x, yend=~y)
113 113
     } else {
114
-        default_aes <- aes_(x=~x, y=~y, node=~node)
114
+        default_aes <- aes_(x=~x, y=~y, node=~node, xend=~x, yend=~y)
115 115
     }
116 116
 
117 117
     if (is.null(mapping)) {
... ...
@@ -119,7 +119,7 @@ stat_stripBar <- function(mapping=NULL, data=NULL,
119 119
     } else {
120 120
         mapping <- modifyList(mapping, default_aes)
121 121
     }
122
-    
122
+
123 123
     layer(stat=StatStripBar,
124 124
           data=data,
125 125
           mapping=mapping,
... ...
@@ -150,8 +150,8 @@ StatStripText <- ggproto("StatStripText", Stat,
150 150
                          required_aes = c("x", "y", "label")
151 151
                          )
152 152
 
153
-                         
154
-                          
153
+
154
+
155 155
 StatStripBar <- ggproto("StatStripBar", Stat,
156 156
                         compute_group = function(self, data, scales, params,
157 157
                                                  taxa1, taxa2, offset, align, barextend) {
... ...
@@ -193,7 +193,7 @@ taxa2node <- function(data, taxa) {
193 193
     if (length(idx) == 0) {
194 194
         stop("input taxa is not valid...")
195 195
     }
196
-    
196
+
197 197
     return(data$node[idx])
198 198
 }
199 199
 
Browse code

fixed R check

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

g.yu authored on 11/10/2016 01:31:56
Showing 1 changed files
... ...
@@ -98,7 +98,7 @@ stat_stripText <- function(mapping=NULL, data=NULL,
98 98
                       na.rm=na.rm,
99 99
                       parse=parse,
100 100
                       ...),
101
-          check.aes = FALSE
101
+          if (packageVersion('ggplot2') > '2.1.0') check.aes = FALSE
102 102
           )
103 103
     
104 104
 }
... ...
@@ -134,7 +134,7 @@ stat_stripBar <- function(mapping=NULL, data=NULL,
134 134
                       barextend=barextend,
135 135
                       na.rm=na.rm,
136 136
                       ...),
137
-          check.aes = FALSE
137
+          if (packageVersion('ggplot2') > '2.1.0') check.aes = FALSE
138 138
           )
139 139
 
140 140
 }
Browse code

version 1.5.15

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

g.yu authored on 07/10/2016 05:18:29
Showing 1 changed files
... ...
@@ -97,7 +97,8 @@ stat_stripText <- function(mapping=NULL, data=NULL,
97 97
                       barextend=barextend,
98 98
                       na.rm=na.rm,
99 99
                       parse=parse,
100
-                      ...)
100
+                      ...),
101
+          check.aes = FALSE
101 102
           )
102 103
     
103 104
 }
... ...
@@ -132,7 +133,8 @@ stat_stripBar <- function(mapping=NULL, data=NULL,
132 133
                       align=align,
133 134
                       barextend=barextend,
134 135
                       na.rm=na.rm,
135
-                      ...)
136
+                      ...),
137
+          check.aes = FALSE
136 138
           )
137 139
 
138 140
 }
Browse code

update docs

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

g.yu authored on 31/08/2016 06:22:59
Showing 1 changed files
... ...
@@ -16,13 +16,15 @@
16 16
 ##' @param hjust hjust
17 17
 ##' @param fill fill label background, only work with geom='label'
18 18
 ##' @param family sans by default, can be any supported font
19
+##' @param parse logical, whether parse label
19 20
 ##' @param ... additional parameter
20 21
 ##' @return ggplot layers
21 22
 ##' @export
22 23
 ##' @author Guangchuang Yu
23 24
 geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
24 25
                        align=TRUE, barsize=0.5, barextend=0, fontsize=3.88,
25
-                       angle=0, geom="text", hjust=0, fill=NA, family="sans", ...) {
26
+                       angle=0, geom="text", hjust=0, fill=NA, family="sans",
27
+                       parse=FALSE, ...) {
26 28
     mapping <- NULL
27 29
     data <- NULL
28 30
     position <- "identity"
... ...
@@ -46,14 +48,14 @@ geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
46 48
                                     align=align, size=fontsize, barextend=barextend, angle=angle, family=family,
47 49
                                     mapping=mapping, data=data, geom=geom, hjust=hjust,
48 50
                                     position=position, show.legend = show.legend,
49
-                                    inherit.aes = inherit.aes, na.rm=na.rm, ...)
51
+                                    inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
50 52
         
51 53
     } else {
52 54
         layer_text <- stat_stripText(taxa1=taxa1, taxa2=taxa2, label=label, offset=offset+offset.text,
53 55
                                     align=align, size=fontsize, barextend=barextend, angle=angle, fill=fill,family=family,
54 56
                                     mapping=mapping, data=data, geom=geom, hjust=hjust,
55 57
                                     position=position, show.legend = show.legend,
56
-                                    inherit.aes = inherit.aes, na.rm=na.rm, ...)
58
+                                    inherit.aes = inherit.aes, na.rm=na.rm, parse=parse, ...)
57 59
     }
58 60
     
59 61
     list(
... ...
@@ -66,7 +68,7 @@ geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
66 68
 stat_stripText <- function(mapping=NULL, data=NULL,
67 69
                            geom="text", position="identity",
68 70
                            taxa1, taxa2, label, offset, align, barextend, ...,
69
-                           show.legend=NA, inherit.aes=FALSE, na.rm=FALSE) {
71
+                           show.legend=NA, inherit.aes=FALSE, na.rm=FALSE, parse=FALSE) {
70 72
 
71 73
     if (is.null(label) || is.na(label)) {
72 74
         default_aes <- aes_(x=~x, y=~y, node=~node, label=~label)
... ...
@@ -94,6 +96,7 @@ stat_stripText <- function(mapping=NULL, data=NULL,
94 96
                       align=align,
95 97
                       barextend=barextend,
96 98
                       na.rm=na.rm,
99
+                      parse=parse,
97 100
                       ...)
98 101
           )
99 102
     
Browse code

geom_cladelabel

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

g.yu authored on 11/08/2016 06:54:44
Showing 1 changed files
... ...
@@ -105,7 +105,7 @@ stat_stripBar <- function(mapping=NULL, data=NULL,
105 105
                           show.legend=NA, inherit.aes=FALSE, na.rm=FALSE) {
106 106
 
107 107
     if (is.null(label) || is.na(label)) {
108
-        default_aes <- aes_(x=~x, y=~y, node=~node, label=~label)
108
+        default_aes <- aes_(x=~x, y=~y, node=~node, label=~label, xend=~x, yend=~y)
109 109
     } else {
110 110
         default_aes <- aes_(x=~x, y=~y, node=~node)
111 111
     }
Browse code

update geom_strip

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

g.yu authored on 27/07/2016 05:17:32
Showing 1 changed files
... ...
@@ -30,7 +30,7 @@ geom_strip <- function(taxa1, taxa2, label=NA, offset=0, offset.text=0,
30 30
     na.rm <- TRUE
31 31
     inherit.aes <- FALSE
32 32
 
33 <