Browse code

rename size aesthetic according to ggplot2 (3.4.0)

xiangpin authored on 07/07/2022 12:57:13
Showing 1 changed files
... ...
@@ -17,7 +17,7 @@
17 17
 ##' to the left side of tip labels, defaults to "FALSE"
18 18
 ##' with a line connecting each tip and its corresponding label, defaults to "FALSE"
19 19
 ##' @param linetype set linetype of the line if align = TRUE, defaults to "dotted"
20
-##' @param linesize set line size of the line if align = TRUE, defaults to 0.5
20
+##' @param linesize set line width if align = TRUE, defaults to 0.5
21 21
 ##' @param geom one of 'text', 'label', 'shadowtext', 'image' and 'phylopic'
22 22
 ##' @param as_ylab display tip labels as y-axis label, 
23 23
 ##' only works for rectangular and dendrogram layouts, defaults to "FALSE"
... ...
@@ -207,7 +207,7 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
207 207
                                     "bg.colour", "bg.r"))
208 208
     list(
209 209
         if (show_segment){
210
-            lineparams <- list(mapping = segment_mapping, linetype=linetype, nudge_x = offset, size = linesize, stat = StatTreeData)
210
+            lineparams <- list(mapping = segment_mapping, linetype=linetype, nudge_x = offset, linewidth = linesize, stat = StatTreeData)
211 211
             lineparams <- extract_params(lineparams, params, c("data", "color", "colour", "alpha", "show.legend", "na.rm",
212 212
                                                                "inherit.aes", "arrow", "arrow.fill", "lineend")) 
213 213
             do.call("geom_segment2", lineparams)
Browse code

Document update

Document update

William Lee authored on 22/03/2022 14:46:29
Showing 1 changed files
... ...
@@ -1,33 +1,43 @@
1
-##' add tip label layer
1
+##' add tip label layer for a tree
2
+##'
3
+##' 'geom_tiplab' not only supports using text or label geom to display tip labels, 
4
+##' but also supports image geom to label tip with image files or phylopics.
5
+##'     
6
+##' For adding tip labels to a tree with circular layout, 'geom_tiplab' will 
7
+##' automatically adjust the angle of the tip labels to the tree by 
8
+##' internally calling 'geom_tiplab2'.
2 9
 ##'
3 10
 ##'
4 11
 ##' @title geom_tiplab
5 12
 ##' @param mapping aes mapping
6
-##' @param hjust horizontal adjustment
13
+##' @param hjust horizontal adjustment, defaults to 0
7 14
 ##' @param offset tiplab offset, horizontal 
8
-##' adjustment to nudge tip labels, default is 0.
9
-##' @param align align tip lab or not, logical
10
-##' @param linetype linetype for adding line if align = TRUE
11
-##' @param linesize line size of line if align = TRUE
15
+##' adjustment to nudge tip labels, defaults to 0
16
+##' @param align if TRUE, align all tip labels to the longest tip by adding padding characters 
17
+##' to the left side of tip labels, defaults to "FALSE"
18
+##' with a line connecting each tip and its corresponding label, defaults to "FALSE"
19
+##' @param linetype set linetype of the line if align = TRUE, defaults to "dotted"
20
+##' @param linesize set line size of the line if align = TRUE, defaults to 0.5
12 21
 ##' @param geom one of 'text', 'label', 'shadowtext', 'image' and 'phylopic'
13
-##' @param as_ylab display tip labels as y-axis label, only works for rectangular and dendrogram layouts
22
+##' @param as_ylab display tip labels as y-axis label, 
23
+##' only works for rectangular and dendrogram layouts, defaults to "FALSE"
14 24
 ##' @param ... additional parameter
15 25
 ##'
16 26
 ##' additional parameters can refer the following parameters. 
17 27
 ##'
18 28
 ##' The following parameters for geom="text".
19 29
 ##' \itemize{
20
-##'     \item \code{size} control the size of tip labels, default is 3.88.
21
-##'     \item \code{colour} control the colour of tip labels, default is "black".
22
-##'     \item \code{angle} control the angle of tip labels, default is 0.
23
-##'     \item \code{vjust} A numeric vector specifying vertical justification, default is 0.5.
24
-##'     \item \code{alpha} the transparency of text, default is NA.
25
-##'     \item \code{family} the family of text, default is 'sans'.
26
-##'     \item \code{fontface} the font face of text, default is 1 (plain), others are 
30
+##'     \item \code{size} control the size of tip labels, defaults to 3.88.
31
+##'     \item \code{colour} control the colour of tip labels, defaults to "black".
32
+##'     \item \code{angle} control the angle of tip labels, defaults to 0.
33
+##'     \item \code{vjust} A numeric vector specifying vertical justification, defaults to 0.5.
34
+##'     \item \code{alpha} the transparency of text, defaults to NA.
35
+##'     \item \code{family} the family of text, defaults to 'sans'.
36
+##'     \item \code{fontface} the font face of text, defaults to 1 (plain), others are 
27 37
 ##'      2 (bold), 3 (italic), 4 (bold.italic).
28
-##'     \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2 .
29
-##'     \item \code{nudge_x} horizontal adjustment to nudge labels, default is 0. 
30
-##'     \item \code{nudge_y}  vertical adjustment to nudge labels, default is 0.
38
+##'     \item \code{lineheight} The height of a line as a multiple of the size of text, defaults to 1.2 .
39
+##'     \item \code{nudge_x} horizontal adjustment to nudge labels, defaults to 0. 
40
+##'     \item \code{nudge_y}  vertical adjustment to nudge labels, defaults to 0.
31 41
 ##'     \item \code{check.overlap} if TRUE, text that overlaps previous text in the same layer 
32 42
 ##'      will not be plotted.
33 43
 ##'     \item \code{parse} if TRUE, the labels will be parsed into expressions, if it is 'emoji', the labels
... ...
@@ -36,49 +46,49 @@
36 46
 ##'
37 47
 ##' The following parameters for geom="label".
38 48
 ##' \itemize{
39
-##'     \item \code{size} the size of tip labels, default is 3.88.
40
-##'     \item \code{colour} the colour of tip labels, default is "black".
41
-##'     \item \code{fill} the colour of rectangular box of labels, default is "white".
42
-##'     \item \code{vjust} numeric vector specifying vertical justification, default is 0.5.
43
-##'     \item \code{alpha} the transparency of labels, default is NA.
44
-##'     \item \code{family} the family of text, default is 'sans'.
45
-##'     \item \code{fontface} the font face of text, default is 1 (plain), others are
49
+##'     \item \code{size} the size of tip labels, defaults to 3.88.
50
+##'     \item \code{colour} the colour of tip labels, defaults to "black".
51
+##'     \item \code{fill} the colour of rectangular box of labels, defaults to "white".
52
+##'     \item \code{vjust} numeric vector specifying vertical justification, defaults to 0.5.
53
+##'     \item \code{alpha} the transparency of labels, defaults to NA.
54
+##'     \item \code{family} the family of text, defaults to 'sans'.
55
+##'     \item \code{fontface} the font face of text, defaults to 1 (plain), others are
46 56
 ##'     2 (bold), 3 (italic), 4 (bold.italic).
47
-##'     \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2.
48
-##'     \item \code{nudge_x} horizontal adjustment to nudge labels, default is 0.
49
-##'     \item \code{nudge_y}  vertical adjustment, default is 0.
57
+##'     \item \code{lineheight} The height of a line as a multiple of the size of text, defaults to 1.2.
58
+##'     \item \code{nudge_x} horizontal adjustment to nudge labels, defaults to 0.
59
+##'     \item \code{nudge_y}  vertical adjustment, defaults to 0.
50 60
 ##'     \item \code{check.overlap} if TRUE, text that overlaps previous text in the same layer
51 61
 ##'      will not be plotted.
52 62
 ##'     \item \code{parse} if TRUE, the labels will be parsed into expressions, if it is 'emoji', the labels
53 63
 ##'      will be parsed into emojifont.
54
-##'     \item \code{label.padding} Amount of padding around label, default is 'unit(0.25, "lines")'.
55
-##'     \item \code{label.r} Radius of rounded corners, default is 'unit(0.15, "lines")'.
56
-##'     \item \code{label.size} Size of label border, in mm, default is 0.25.
64
+##'     \item \code{label.padding} Amount of padding around label, defaults to 'unit(0.25, "lines")'.
65
+##'     \item \code{label.r} Radius of rounded corners, defaults to 'unit(0.15, "lines")'.
66
+##'     \item \code{label.size} Size of label border, in mm, defaults to 0.25.
57 67
 ##' }
58 68
 ##'
59 69
 ##' The following parameters for geom="shadowtext", some parameters are like to geom="text".
60 70
 ##' \itemize{
61
-##'     \item \code{bg.colour} the background colour of text, default is "black".
62
-##'     \item \code{bg.r} the width of background of text, default is 0.1 .
71
+##'     \item \code{bg.colour} the background colour of text, defaults to "black".
72
+##'     \item \code{bg.r} the width of background of text, defaults to 0.1 .
63 73
 ##' }
64 74
 ##'
65 75
 ##' The following parameters for geom="image" or geom="phylopic".
66 76
 ##' \itemize{
67 77
 ##'     \item \code{image} the image file path for geom='image', but when geom='phylopic',
68 78
 ##'      it should be the uid of phylopic databases.
69
-##'     \item \code{size} the image size, default is 0.05.
70
-##'     \item \code{colour} the color of image, default is NULL.
71
-##'     \item \code{alpha} the transparency of image, default is 0.8.
79
+##'     \item \code{size} the image size, defaults to 0.05.
80
+##'     \item \code{colour} the color of image, defaults to NULL.
81
+##'     \item \code{alpha} the transparency of image, defaults to 0.8.
72 82
 ##' }
73 83
 ##'
74 84
 ##' The following parameters for the line when align = TRUE.
75 85
 ##' \itemize{
76
-##'     \item \code{colour} the colour of line, default is 'black'.
77
-##'     \item \code{alpha} the transparency of line, default is NA.
86
+##'     \item \code{colour} the colour of line, defaults to 'black'.
87
+##'     \item \code{alpha} the transparency of line, defaults to NA.
78 88
 ##'     \item \code{arrow} specification for arrow heads, 
79
-##'     as created by arrow(), default is NULL.
89
+##'     as created by arrow(), defaults to NULL.
80 90
 ##'     \item \code{arrow.fill} fill color to usse for the arrow head (if closed), 
81
-##'     default is 'NULL', meaning use 'colour' aesthetic.
91
+##'     defaults to 'NULL', meaning use 'colour' aesthetic.
82 92
 ##' }
83 93
 ##' @return tip label layer
84 94
 ##' @importFrom ggplot2 geom_text
... ...
@@ -89,6 +99,10 @@
89 99
 ##' require(ape)
90 100
 ##' tr <- rtree(10)
91 101
 ##' ggtree(tr) + geom_tiplab()
102
+##' @references 
103
+##'  For more detailed demonstration, please refer to chapter 4.3.3 of 
104
+##' *Data Integration, Manipulation and Visualization of Phylogenetic Trees*
105
+##' <http://yulab-smu.top/treedata-book/index.html> by Guangchuang Yu.
92 106
 geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dotted",
93 107
                         linesize=0.5, geom="text",  offset=0, as_ylab = FALSE, ...) {
94 108
     structure(list(mapping = mapping,
... ...
@@ -210,14 +224,21 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
210 224
 
211 225
 ##' add tip label for circular layout
212 226
 ##'
227
+##' 'geom_tiplab2' will automatically adjust the angle of the tip labels 
228
+##' to the tree with circular layout
213 229
 ##'
214 230
 ##' @title geom_tiplab2
215 231
 ##' @param mapping aes mapping
216
-##' @param hjust horizontal adjustment
232
+##' @param hjust horizontal adjustment, defaults to 0
217 233
 ##' @param ... additional parameter, see geom_tiplab
218 234
 ##' @return tip label layer
219 235
 ##' @export
220 236
 ##' @author Guangchuang Yu
237
+##' @examples 
238
+##' library(ggtree)
239
+##' set.seed(123)
240
+##' tr <- rtree(10)
241
+##' ggtree(tr, layout = "circular") + geom_tiplab2()
221 242
 ##' @references <https://groups.google.com/forum/#!topic/bioc-ggtree/o35PV3iHO-0>
222 243
 ##' @seealso [geom_tiplab]
223 244
 geom_tiplab2 <- function(mapping=NULL, hjust=0, ...) {
... ...
@@ -263,10 +284,10 @@ geom_tiplab_circular <- geom_tiplab2
263 284
 
264 285
 #' Padding taxa labels
265 286
 #'
266
-#' This function add padding character to the left side of taxa labels.
287
+#' This function adds padding characters to the left side of taxa labels, adjust their length to the longest label.
267 288
 #' @param label taxa label 
268
-#' @param justify should a character vector be left-justified, right-justified (default), centred or left alone.
269
-#' @param pad padding character (default is a dot)
289
+#' @param justify should a character vector be right-justified (default), left-justified, centred or left alone.
290
+#' @param pad padding character (defaults to dots)
270 291
 #'
271 292
 #' @return Taxa labels with padding characters added
272 293
 #' @export
Browse code

fix the mapping of line of geom_tiplab

xiangpin authored on 12/11/2021 12:55:33
Showing 1 changed files
... ...
@@ -178,8 +178,8 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
178 178
                                node = node,
179 179
                                label = label,
180 180
                                subset = isTip)
181
-        if (!is.null(mapping))
182
-            segment_mapping <- modifyList(segment_mapping, mapping)
181
+        if (!is.null(text_mapping))
182
+            segment_mapping <- modifyList(segment_mapping, text_mapping)
183 183
     }
184 184
     imageparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData)
185 185
     imageparams <- extract_params(imageparams, params, c("data", "size", "alpha", "color", "colour", "image", 
... ...
@@ -194,7 +194,7 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
194 194
     list(
195 195
         if (show_segment){
196 196
             lineparams <- list(mapping = segment_mapping, linetype=linetype, nudge_x = offset, size = linesize, stat = StatTreeData)
197
-            lineparams <- extract_params(lineparams, params, c("data", "colour", "alpha", "show.legend", "na.rm",
197
+            lineparams <- extract_params(lineparams, params, c("data", "color", "colour", "alpha", "show.legend", "na.rm",
198 198
                                                                "inherit.aes", "arrow", "arrow.fill", "lineend")) 
199 199
             do.call("geom_segment2", lineparams)
200 200
         }
Browse code

fix the data argument of geom_tiplab

xiangpin authored on 10/08/2021 07:31:19
Showing 1 changed files
... ...
@@ -182,19 +182,19 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
182 182
             segment_mapping <- modifyList(segment_mapping, mapping)
183 183
     }
184 184
     imageparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData)
185
-    imageparams <- extract_params(imageparams, params, c("size", "alpha", "color", "colour", "image", 
185
+    imageparams <- extract_params(imageparams, params, c("data", "size", "alpha", "color", "colour", "image", 
186 186
                                                          "angle", "position", "inherit.aes", "by", "show.legend",
187 187
                                                          "image_fun", ".fun", "asp", "nudge_y", "height", "na.rm")) 
188 188
     labelparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData)
189 189
     labelparams <- extract_params(labelparams, params, 
190
-                                  c("size", "alpha", "vjust", "color", "colour", "angle", "alpha", "family", "fontface",
190
+                                  c("data", "size", "alpha", "vjust", "color", "colour", "angle", "alpha", "family", "fontface",
191 191
                                     "lineheight", "fill", "position", "nudge_y", "show.legend", "check_overlap",
192 192
                                     "parse", "inherit.aes", "na.rm", "label.r", "label.size", "label.padding",
193 193
                                     "bg.colour", "bg.r"))
194 194
     list(
195 195
         if (show_segment){
196 196
             lineparams <- list(mapping = segment_mapping, linetype=linetype, nudge_x = offset, size = linesize, stat = StatTreeData)
197
-            lineparams <- extract_params(lineparams, params, c("colour", "alpha", "show.legend",  "na.rm",
197
+            lineparams <- extract_params(lineparams, params, c("data", "colour", "alpha", "show.legend", "na.rm",
198 198
                                                                "inherit.aes", "arrow", "arrow.fill", "lineend")) 
199 199
             do.call("geom_segment2", lineparams)
200 200
         }
Browse code

fixed fontface of aes

xiangpin authored on 06/07/2021 07:53:54
Showing 1 changed files
... ...
@@ -114,7 +114,7 @@ geom_tiplab_as_ylab <- function(hjust = 0, position = "right", ...) {
114 114
 
115 115
 geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE, 
116 116
                                     linetype = "dotted", linesize=0.5, geom="text",  
117
-                                    offset=0, family = "", fontface = "plain", 
117
+                                    offset=0, #family = "", fontface = "plain", 
118 118
                                     node="external", ...) {
119 119
     params <- list(...)
120 120
     if ("nudge_x" %in% names(params)){
... ...
@@ -185,9 +185,9 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
185 185
     imageparams <- extract_params(imageparams, params, c("size", "alpha", "color", "colour", "image", 
186 186
                                                          "angle", "position", "inherit.aes", "by", "show.legend",
187 187
                                                          "image_fun", ".fun", "asp", "nudge_y", "height", "na.rm")) 
188
-    labelparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData, family = family, fontface = fontface)
188
+    labelparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData)
189 189
     labelparams <- extract_params(labelparams, params, 
190
-                                  c("size", "alpha", "vjust", "color", "colour", "angle", "alpha", 
190
+                                  c("size", "alpha", "vjust", "color", "colour", "angle", "alpha", "family", "fontface",
191 191
                                     "lineheight", "fill", "position", "nudge_y", "show.legend", "check_overlap",
192 192
                                     "parse", "inherit.aes", "na.rm", "label.r", "label.size", "label.padding",
193 193
                                     "bg.colour", "bg.r"))
Browse code

new geom_nodelab and geom_tiplab

xiangpin authored on 29/05/2021 07:25:13
Showing 1 changed files
... ...
@@ -91,10 +91,6 @@
91 91
 ##' ggtree(tr) + geom_tiplab()
92 92
 geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dotted",
93 93
                         linesize=0.5, geom="text",  offset=0, as_ylab = FALSE, ...) {
94
-    #####in order to check whether it is geom_nodelab
95
-    #.call <- match.call(call = sys.call(sys.parent(1)))
96
-    #nodelab <- ifelse(as.list(.call)[[1]]=="geom_nodelab", TRUE, FALSE)
97
-    #####
98 94
     structure(list(mapping = mapping,
99 95
                    hjust = hjust,
100 96
                    align = align,
... ...
@@ -103,7 +99,7 @@ geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dot
103 99
                    geom = geom,
104 100
                    offset = offset,
105 101
                    as_ylab = as_ylab,
106
-                   #nodelab = nodelab,
102
+				   node = "external",
107 103
                    ...),
108 104
               class = "tiplab")
109 105
 }
... ...
@@ -118,7 +114,8 @@ geom_tiplab_as_ylab <- function(hjust = 0, position = "right", ...) {
118 114
 
119 115
 geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE, 
120 116
                                     linetype = "dotted", linesize=0.5, geom="text",  
121
-                                    offset=0, family = "", fontface = "plain", ...) {
117
+                                    offset=0, family = "", fontface = "plain", 
118
+                                    node="external", ...) {
122 119
     params <- list(...)
123 120
     if ("nudge_x" %in% names(params)){
124 121
         if (offset != 0){
... ...
@@ -143,28 +140,26 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
143 140
         label_geom <- get_fun_from_pkg("ggimage", "geom_phylopic")
144 141
     }
145 142
 
146
-
143
+    nodelab <- node
147 144
     x <- y <- label <- isTip <- node <- NULL
148 145
     if (align == TRUE) {
149 146
         self_mapping <- aes(x = max(x, na.rm=TRUE) + diff(range(x, na.rm=TRUE))/200, y = y,
150
-                            label = label, node = node, subset = isTip)
147
+                            label = label, node = node)#, subset = isTip)
151 148
     }
152 149
     else {
153 150
         self_mapping <- aes(x = x + diff(range(x, na.rm=TRUE))/200, y= y,
154
-                            label = label, node = node, subset = isTip)
155
-    }
156
-    if ("nodelab" %in% names(params) && params[["nodelab"]]){
157
-        # for node label
158
-        subset <- aes_string(subset="!isTip")
159
-    }else{
160
-        # for tip label
161
-        subset <- aes_string(subset="isTip")
151
+                            label = label, node = node)#, subset = isTip)
162 152
     }
153
+    subset <- switch(nodelab,
154
+					 internal = aes_string(subset="!isTip"),
155
+					 external = aes_string(subset="isTip"),
156
+					 all = aes_string(subset=NULL)
157
+					 )
163 158
     self_mapping <- modifyList(self_mapping, subset)
164 159
     if (is.null(mapping)) {
165 160
         text_mapping <- self_mapping
166 161
     } else {
167
-        if (!is.null(mapping$subset)){
162
+        if (!is.null(mapping$subset) && nodelab != "all"){
168 163
             newsubset <- aes_string(subset=paste0(as.expression(get_aes_var(mapping, "subset")), 
169 164
                                                   '&', 
170 165
                                                   as.expression(get_aes_var(subset, "subset")))
... ...
@@ -186,7 +181,6 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
186 181
         if (!is.null(mapping))
187 182
             segment_mapping <- modifyList(segment_mapping, mapping)
188 183
     }
189
-    params[["nodelab"]] <- NULL
190 184
     imageparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData)
191 185
     imageparams <- extract_params(imageparams, params, c("size", "alpha", "color", "colour", "image", 
192 186
                                                          "angle", "position", "inherit.aes", "by", "show.legend",
Browse code

remove match.call

xiangpin authored on 27/05/2021 12:01:21
Showing 1 changed files
... ...
@@ -92,8 +92,8 @@
92 92
 geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dotted",
93 93
                         linesize=0.5, geom="text",  offset=0, as_ylab = FALSE, ...) {
94 94
     #####in order to check whether it is geom_nodelab
95
-    .call <- match.call(call = sys.call(sys.parent(1)))
96
-    nodelab <- ifelse(as.list(.call)[[1]]=="geom_nodelab", TRUE, FALSE)
95
+    #.call <- match.call(call = sys.call(sys.parent(1)))
96
+    #nodelab <- ifelse(as.list(.call)[[1]]=="geom_nodelab", TRUE, FALSE)
97 97
     #####
98 98
     structure(list(mapping = mapping,
99 99
                    hjust = hjust,
... ...
@@ -103,7 +103,7 @@ geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dot
103 103
                    geom = geom,
104 104
                    offset = offset,
105 105
                    as_ylab = as_ylab,
106
-                   nodelab = nodelab,
106
+                   #nodelab = nodelab,
107 107
                    ...),
108 108
               class = "tiplab")
109 109
 }
Browse code

Update geom_tiplab.R

Guangchuang Yu authored on 19/04/2021 02:45:58 • GitHub committed on 19/04/2021 02:45:58
Showing 1 changed files
... ...
@@ -122,10 +122,10 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
122 122
     params <- list(...)
123 123
     if ("nudge_x" %in% names(params)){
124 124
         if (offset != 0){
125
-            warning_wrap("The nudge_x and offset argument both was provided.
125
+            warning_wrap("Both nudge_x and offset arguments are provided.
126 126
                          Because they all adjust the horizontal offset of labels,
127
-                         and the 'nudge_x' is consistency with 'ggplot2'. The
128
-                         'nudge_x' will be predetermined, 'offset' will be deprecated.")
127
+                         and the 'nudge_x' is consistent with 'ggplot2'. The
128
+                         'offset' will be deprecated here and only the 'nudge_x' will be used.")
129 129
         }
130 130
         offset <- params$nudge_x
131 131
         params$nudge_x <- NULL
Browse code

nudge_x can also work like offset

xiangpin authored on 19/04/2021 01:50:12
Showing 1 changed files
... ...
@@ -4,7 +4,8 @@
4 4
 ##' @title geom_tiplab
5 5
 ##' @param mapping aes mapping
6 6
 ##' @param hjust horizontal adjustment
7
-##' @param offset tiplab offset
7
+##' @param offset tiplab offset, horizontal 
8
+##' adjustment to nudge tip labels, default is 0.
8 9
 ##' @param align align tip lab or not, logical
9 10
 ##' @param linetype linetype for adding line if align = TRUE
10 11
 ##' @param linesize line size of line if align = TRUE
... ...
@@ -25,8 +26,8 @@
25 26
 ##'     \item \code{fontface} the font face of text, default is 1 (plain), others are 
26 27
 ##'      2 (bold), 3 (italic), 4 (bold.italic).
27 28
 ##'     \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2 .
28
-##'     \item \code{nudge_x} horizontal adjustment, default is 0.
29
-##'     \item \code{nudge_y}  vertical adjustment, default is 0.
29
+##'     \item \code{nudge_x} horizontal adjustment to nudge labels, default is 0. 
30
+##'     \item \code{nudge_y}  vertical adjustment to nudge labels, default is 0.
30 31
 ##'     \item \code{check.overlap} if TRUE, text that overlaps previous text in the same layer 
31 32
 ##'      will not be plotted.
32 33
 ##'     \item \code{parse} if TRUE, the labels will be parsed into expressions, if it is 'emoji', the labels
... ...
@@ -44,7 +45,7 @@
44 45
 ##'     \item \code{fontface} the font face of text, default is 1 (plain), others are
45 46
 ##'     2 (bold), 3 (italic), 4 (bold.italic).
46 47
 ##'     \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2.
47
-##'     \item \code{nudge_x} horizontal adjustment, default is 0.
48
+##'     \item \code{nudge_x} horizontal adjustment to nudge labels, default is 0.
48 49
 ##'     \item \code{nudge_y}  vertical adjustment, default is 0.
49 50
 ##'     \item \code{check.overlap} if TRUE, text that overlaps previous text in the same layer
50 51
 ##'      will not be plotted.
... ...
@@ -119,6 +120,16 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
119 120
                                     linetype = "dotted", linesize=0.5, geom="text",  
120 121
                                     offset=0, family = "", fontface = "plain", ...) {
121 122
     params <- list(...)
123
+    if ("nudge_x" %in% names(params)){
124
+        if (offset != 0){
125
+            warning_wrap("The nudge_x and offset argument both was provided.
126
+                         Because they all adjust the horizontal offset of labels,
127
+                         and the 'nudge_x' is consistency with 'ggplot2'. The
128
+                         'nudge_x' will be predetermined, 'offset' will be deprecated.")
129
+        }
130
+        offset <- params$nudge_x
131
+        params$nudge_x <- NULL
132
+    }
122 133
     geom <- match.arg(geom, c("text", "label", "shadowtext", "image", "phylopic"))
123 134
     if (geom == "text") {
124 135
         label_geom <- geom_text2
... ...
@@ -178,12 +189,12 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
178 189
     params[["nodelab"]] <- NULL
179 190
     imageparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData)
180 191
     imageparams <- extract_params(imageparams, params, c("size", "alpha", "color", "colour", "image", 
181
-                                                         "angle", "nudge_x", "inherit.aes", "by", "show.legend",
192
+                                                         "angle", "position", "inherit.aes", "by", "show.legend",
182 193
                                                          "image_fun", ".fun", "asp", "nudge_y", "height", "na.rm")) 
183 194
     labelparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData, family = family, fontface = fontface)
184 195
     labelparams <- extract_params(labelparams, params, 
185 196
                                   c("size", "alpha", "vjust", "color", "colour", "angle", "alpha", 
186
-                                    "lineheight", "fill", "nudge_x", "nudge_y", "show.legend", "check_overlap",
197
+                                    "lineheight", "fill", "position", "nudge_y", "show.legend", "check_overlap",
187 198
                                     "parse", "inherit.aes", "na.rm", "label.r", "label.size", "label.padding",
188 199
                                     "bg.colour", "bg.r"))
189 200
     list(
Browse code

optimization geom_tiplab and geom_nodelab

xiangpin authored on 14/04/2021 04:23:50
Showing 1 changed files
... ...
@@ -11,6 +11,74 @@
11 11
 ##' @param geom one of 'text', 'label', 'shadowtext', 'image' and 'phylopic'
12 12
 ##' @param as_ylab display tip labels as y-axis label, only works for rectangular and dendrogram layouts
13 13
 ##' @param ... additional parameter
14
+##'
15
+##' additional parameters can refer the following parameters. 
16
+##'
17
+##' The following parameters for geom="text".
18
+##' \itemize{
19
+##'     \item \code{size} control the size of tip labels, default is 3.88.
20
+##'     \item \code{colour} control the colour of tip labels, default is "black".
21
+##'     \item \code{angle} control the angle of tip labels, default is 0.
22
+##'     \item \code{vjust} A numeric vector specifying vertical justification, default is 0.5.
23
+##'     \item \code{alpha} the transparency of text, default is NA.
24
+##'     \item \code{family} the family of text, default is 'sans'.
25
+##'     \item \code{fontface} the font face of text, default is 1 (plain), others are 
26
+##'      2 (bold), 3 (italic), 4 (bold.italic).
27
+##'     \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2 .
28
+##'     \item \code{nudge_x} horizontal adjustment, default is 0.
29
+##'     \item \code{nudge_y}  vertical adjustment, default is 0.
30
+##'     \item \code{check.overlap} if TRUE, text that overlaps previous text in the same layer 
31
+##'      will not be plotted.
32
+##'     \item \code{parse} if TRUE, the labels will be parsed into expressions, if it is 'emoji', the labels
33
+##'      will be parsed into emojifont.
34
+##' }
35
+##'
36
+##' The following parameters for geom="label".
37
+##' \itemize{
38
+##'     \item \code{size} the size of tip labels, default is 3.88.
39
+##'     \item \code{colour} the colour of tip labels, default is "black".
40
+##'     \item \code{fill} the colour of rectangular box of labels, default is "white".
41
+##'     \item \code{vjust} numeric vector specifying vertical justification, default is 0.5.
42
+##'     \item \code{alpha} the transparency of labels, default is NA.
43
+##'     \item \code{family} the family of text, default is 'sans'.
44
+##'     \item \code{fontface} the font face of text, default is 1 (plain), others are
45
+##'     2 (bold), 3 (italic), 4 (bold.italic).
46
+##'     \item \code{lineheight} The height of a line as a multiple of the size of text, default is 1.2.
47
+##'     \item \code{nudge_x} horizontal adjustment, default is 0.
48
+##'     \item \code{nudge_y}  vertical adjustment, default is 0.
49
+##'     \item \code{check.overlap} if TRUE, text that overlaps previous text in the same layer
50
+##'      will not be plotted.
51
+##'     \item \code{parse} if TRUE, the labels will be parsed into expressions, if it is 'emoji', the labels
52
+##'      will be parsed into emojifont.
53
+##'     \item \code{label.padding} Amount of padding around label, default is 'unit(0.25, "lines")'.
54
+##'     \item \code{label.r} Radius of rounded corners, default is 'unit(0.15, "lines")'.
55
+##'     \item \code{label.size} Size of label border, in mm, default is 0.25.
56
+##' }
57
+##'
58
+##' The following parameters for geom="shadowtext", some parameters are like to geom="text".
59
+##' \itemize{
60
+##'     \item \code{bg.colour} the background colour of text, default is "black".
61
+##'     \item \code{bg.r} the width of background of text, default is 0.1 .
62
+##' }
63
+##'
64
+##' The following parameters for geom="image" or geom="phylopic".
65
+##' \itemize{
66
+##'     \item \code{image} the image file path for geom='image', but when geom='phylopic',
67
+##'      it should be the uid of phylopic databases.
68
+##'     \item \code{size} the image size, default is 0.05.
69
+##'     \item \code{colour} the color of image, default is NULL.
70
+##'     \item \code{alpha} the transparency of image, default is 0.8.
71
+##' }
72
+##'
73
+##' The following parameters for the line when align = TRUE.
74
+##' \itemize{
75
+##'     \item \code{colour} the colour of line, default is 'black'.
76
+##'     \item \code{alpha} the transparency of line, default is NA.
77
+##'     \item \code{arrow} specification for arrow heads, 
78
+##'     as created by arrow(), default is NULL.
79
+##'     \item \code{arrow.fill} fill color to usse for the arrow head (if closed), 
80
+##'     default is 'NULL', meaning use 'colour' aesthetic.
81
+##' }
14 82
 ##' @return tip label layer
15 83
 ##' @importFrom ggplot2 geom_text
16 84
 ##' @importFrom utils modifyList
... ...
@@ -50,6 +118,7 @@ geom_tiplab_as_ylab <- function(hjust = 0, position = "right", ...) {
50 118
 geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE, 
51 119
                                     linetype = "dotted", linesize=0.5, geom="text",  
52 120
                                     offset=0, family = "", fontface = "plain", ...) {
121
+    params <- list(...)
53 122
     geom <- match.arg(geom, c("text", "label", "shadowtext", "image", "phylopic"))
54 123
     if (geom == "text") {
55 124
         label_geom <- geom_text2
... ...
@@ -73,14 +142,27 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
73 142
         self_mapping <- aes(x = x + diff(range(x, na.rm=TRUE))/200, y= y,
74 143
                             label = label, node = node, subset = isTip)
75 144
     }
76
-
145
+    if ("nodelab" %in% names(params) && params[["nodelab"]]){
146
+        # for node label
147
+        subset <- aes_string(subset="!isTip")
148
+    }else{
149
+        # for tip label
150
+        subset <- aes_string(subset="isTip")
151
+    }
152
+    self_mapping <- modifyList(self_mapping, subset)
77 153
     if (is.null(mapping)) {
78 154
         text_mapping <- self_mapping
79 155
     } else {
156
+        if (!is.null(mapping$subset)){
157
+            newsubset <- aes_string(subset=paste0(as.expression(get_aes_var(mapping, "subset")), 
158
+                                                  '&', 
159
+                                                  as.expression(get_aes_var(subset, "subset")))
160
+                                    )
161
+            self_mapping <- modifyList(self_mapping, newsubset)
162
+            mapping$subset <- NULL
163
+        }
80 164
         text_mapping <- modifyList(self_mapping, mapping)
81 165
     }
82
-
83
-
84 166
     show_segment <- FALSE
85 167
     if (align && (!is.na(linetype) && !is.null(linetype))) {
86 168
         show_segment <- TRUE
... ...
@@ -93,20 +175,29 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
93 175
         if (!is.null(mapping))
94 176
             segment_mapping <- modifyList(segment_mapping, mapping)
95 177
     }
96
-
178
+    params[["nodelab"]] <- NULL
179
+    imageparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData)
180
+    imageparams <- extract_params(imageparams, params, c("size", "alpha", "color", "colour", "image", 
181
+                                                         "angle", "nudge_x", "inherit.aes", "by", "show.legend",
182
+                                                         "image_fun", ".fun", "asp", "nudge_y", "height", "na.rm")) 
183
+    labelparams <- list(mapping=text_mapping, hjust = hjust, nudge_x = offset, stat = StatTreeData, family = family, fontface = fontface)
184
+    labelparams <- extract_params(labelparams, params, 
185
+                                  c("size", "alpha", "vjust", "color", "colour", "angle", "alpha", 
186
+                                    "lineheight", "fill", "nudge_x", "nudge_y", "show.legend", "check_overlap",
187
+                                    "parse", "inherit.aes", "na.rm", "label.r", "label.size", "label.padding",
188
+                                    "bg.colour", "bg.r"))
97 189
     list(
98
-        if (show_segment)
99
-            geom_segment2(mapping = segment_mapping,
100
-                          linetype = linetype, nudge_x = offset,
101
-                          size = linesize, stat = StatTreeData, ...)
190
+        if (show_segment){
191
+            lineparams <- list(mapping = segment_mapping, linetype=linetype, nudge_x = offset, size = linesize, stat = StatTreeData)
192
+            lineparams <- extract_params(lineparams, params, c("colour", "alpha", "show.legend",  "na.rm",
193
+                                                               "inherit.aes", "arrow", "arrow.fill", "lineend")) 
194
+            do.call("geom_segment2", lineparams)
195
+        }
102 196
        ,
103 197
         if (geom %in% c("image", "phylopic")) {
104
-            label_geom(mapping=text_mapping,
105
-                       hjust = hjust, nudge_x = offset, stat = StatTreeData, ...)            
198
+            do.call("label_geom", imageparams)
106 199
         } else {
107
-            label_geom(mapping=text_mapping,
108
-                       hjust = hjust, nudge_x = offset, stat = StatTreeData, 
109
-                       family = family, fontface = fontface, ...)
200
+            do.call("label_geom", labelparams)
110 201
         }
111 202
     )
112 203
 }
... ...
@@ -126,15 +217,17 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
126 217
 ##' @seealso [geom_tiplab]
127 218
 geom_tiplab2 <- function(mapping=NULL, hjust=0, ...) {
128 219
     params <- list(...)
129
-    if ("nodelab" %in% names(params) && params[["nodelab"]]){
130
-        # for geom_nodelab
131
-        subset1 <- "(!isTip & (angle < 90 | angle > 270))"
132
-        subset2 <- "(!isTip & (angle >= 90 & angle <= 270))"
133
-    }else{
134
-        # for geom_tiplab
135
-        subset1 <- "(isTip & (angle < 90 | angle > 270))"
136
-        subset2 <- "(isTip & (angle >= 90 & angle <=270))"
137
-    }
220
+    #if ("nodelab" %in% names(params) && params[["nodelab"]]){
221
+    #    # for geom_nodelab
222
+    #    subset1 <- "(!isTip & (angle < 90 | angle > 270))"
223
+    #    subset2 <- "(!isTip & (angle >= 90 & angle <= 270))"
224
+    #}else{
225
+    #    # for geom_tiplab
226
+    #    subset1 <- "(isTip & (angle < 90 | angle > 270))"
227
+    #    subset2 <- "(isTip & (angle >= 90 & angle <=270))"
228
+    #}
229
+    subset1 <- "(angle < 90 | angle > 270)"
230
+    subset2 <- "(angle >= 90 & angle <=270)"
138 231
     m1 <- aes_string(subset=subset1, angle="angle", node = "node")
139 232
     m2 <- aes_string(subset=subset2, angle="angle+180", node = "node")
140 233
 
... ...
@@ -148,7 +241,7 @@ geom_tiplab2 <- function(mapping=NULL, hjust=0, ...) {
148 241
         m1 <- modifyList(mapping, m1)
149 242
         m2 <- modifyList(mapping, m2)
150 243
     }
151
-    params[["nodelab"]] <- NULL
244
+    #params[["nodelab"]] <- NULL
152 245
     params1 <- params2 <- params
153 246
     params1[["mapping"]] <- m1
154 247
     params1[["hjust"]] <- hjust
... ...
@@ -195,3 +288,13 @@ label_pad <- function(label, justify = "right", pad = "\u00B7") {
195 288
     paste0(y, label)
196 289
 }
197 290
 
291
+
292
+extract_params <- function(originparam, inputparam, defaultparam){
293
+    if (any(defaultparam %in% names(inputparam))){
294
+        args <- intersect(defaultparam, names(inputparam))
295
+        originparam <- c(originparam, inputparam[names(inputparam) %in% args])
296
+    }
297
+    
298
+    return (originparam)
299
+
300
+}
Browse code

fix geom_tiplab2

xiangpin authored on 26/11/2020 07:12:12
Showing 1 changed files
... ...
@@ -126,7 +126,7 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
126 126
 ##' @seealso [geom_tiplab]
127 127
 geom_tiplab2 <- function(mapping=NULL, hjust=0, ...) {
128 128
     params <- list(...)
129
-    if (params[["nodelab"]]){
129
+    if ("nodelab" %in% names(params) && params[["nodelab"]]){
130 130
         # for geom_nodelab
131 131
         subset1 <- "(!isTip & (angle < 90 | angle > 270))"
132 132
         subset2 <- "(!isTip & (angle >= 90 & angle <= 270))"
Browse code

add match.call to extract geom_nodelab

xiangpin authored on 25/11/2020 12:41:44
Showing 1 changed files
... ...
@@ -22,6 +22,10 @@
22 22
 ##' ggtree(tr) + geom_tiplab()
23 23
 geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dotted",
24 24
                         linesize=0.5, geom="text",  offset=0, as_ylab = FALSE, ...) {
25
+    #####in order to check whether it is geom_nodelab
26
+    .call <- match.call(call = sys.call(sys.parent(1)))
27
+    nodelab <- ifelse(as.list(.call)[[1]]=="geom_nodelab", TRUE, FALSE)
28
+    #####
25 29
     structure(list(mapping = mapping,
26 30
                    hjust = hjust,
27 31
                    align = align,
... ...
@@ -30,6 +34,7 @@ geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dot
30 34
                    geom = geom,
31 35
                    offset = offset,
32 36
                    as_ylab = as_ylab,
37
+                   nodelab = nodelab,
33 38
                    ...),
34 39
               class = "tiplab")
35 40
 }
... ...
@@ -121,11 +126,12 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
121 126
 ##' @seealso [geom_tiplab]
122 127
 geom_tiplab2 <- function(mapping=NULL, hjust=0, ...) {
123 128
     params <- list(...)
124
-    nodelab <- ifelse("nodelab" %in% names(params), TRUE, FALSE)
125
-    if (nodelab){
129
+    if (params[["nodelab"]]){
130
+        # for geom_nodelab
126 131
         subset1 <- "(!isTip & (angle < 90 | angle > 270))"
127 132
         subset2 <- "(!isTip & (angle >= 90 & angle <= 270))"
128 133
     }else{
134
+        # for geom_tiplab
129 135
         subset1 <- "(isTip & (angle < 90 | angle > 270))"
130 136
         subset2 <- "(isTip & (angle >= 90 & angle <=270))"
131 137
     }
... ...
@@ -134,13 +140,8 @@ geom_tiplab2 <- function(mapping=NULL, hjust=0, ...) {
134 140
 
135 141
     if (!is.null(mapping)) {
136 142
         if (!is.null(mapping$subset)) {
137
-            if (nodelab){
138
-                newsubset1 <- paste0(as.expression(get_aes_var(mapping, "subset")), '& (angle < 90 | angle > 270)')
139
-                newsubset2 <- paste0(as.expression(get_aes_var(mapping, "subset")), '& (angle >= 90 & angle <= 270)')
140
-            }else{
141
-                newsubset1 <- paste0(as.expression(get_aes_var(mapping, "subset")), '& (isTip & (angle < 90 | angle > 270))')
142
-                newsubset2 <- paste0(as.expression(get_aes_var(mapping, "subset")), '& (isTip & (angle >= 90 & angle <= 270))')
143
-            }
143
+            newsubset1 <- paste0(as.expression(get_aes_var(mapping, "subset")), '&', subset1)
144
+            newsubset2 <- paste0(as.expression(get_aes_var(mapping, "subset")), '&', subset2)
144 145
             m1 <- aes_string(angle = "angle", node = "node", subset = newsubset1)
145 146
             m2 <- aes_string(angle = "angle+180", node = "node", subset = newsubset2)
146 147
         }
Browse code

fix geom_nodelab to support circular layout

xiangpin authored on 25/11/2020 04:14:05
Showing 1 changed files
... ...
@@ -120,23 +120,41 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE,
120 120
 ##' @references <https://groups.google.com/forum/#!topic/bioc-ggtree/o35PV3iHO-0>
121 121
 ##' @seealso [geom_tiplab]
122 122
 geom_tiplab2 <- function(mapping=NULL, hjust=0, ...) {
123
-    angle <- isTip <- node <- NULL
124
-    m1 <- aes(subset=(isTip & (angle < 90 | angle > 270)), angle=angle, node = node)
125
-    m2 <- aes(subset=(isTip & (angle >= 90 & angle <=270)), angle=angle+180, node = node)
123
+    params <- list(...)
124
+    nodelab <- ifelse("nodelab" %in% names(params), TRUE, FALSE)
125
+    if (nodelab){
126
+        subset1 <- "(!isTip & (angle < 90 | angle > 270))"
127
+        subset2 <- "(!isTip & (angle >= 90 & angle <= 270))"
128
+    }else{
129
+        subset1 <- "(isTip & (angle < 90 | angle > 270))"
130
+        subset2 <- "(isTip & (angle >= 90 & angle <=270))"
131
+    }
132
+    m1 <- aes_string(subset=subset1, angle="angle", node = "node")
133
+    m2 <- aes_string(subset=subset2, angle="angle+180", node = "node")
126 134
 
127 135
     if (!is.null(mapping)) {
128 136
         if (!is.null(mapping$subset)) {
129
-            m1 <- aes_string(angle = "angle", node = "node",
130
-                             subset = paste0(as.expression(get_aes_var(mapping, "subset")), '& (isTip & (angle < 90 | angle > 270))'))
131
-            m2 <- aes_string(angle = "angle+180", node = "node",
132
-                             subset = paste0(as.expression(get_aes_var(mapping, "subset")), '& (isTip & (angle >= 90 & angle <= 270))'))
137
+            if (nodelab){
138
+                newsubset1 <- paste0(as.expression(get_aes_var(mapping, "subset")), '& (angle < 90 | angle > 270)')
139
+                newsubset2 <- paste0(as.expression(get_aes_var(mapping, "subset")), '& (angle >= 90 & angle <= 270)')
140
+            }else{
141
+                newsubset1 <- paste0(as.expression(get_aes_var(mapping, "subset")), '& (isTip & (angle < 90 | angle > 270))')
142
+                newsubset2 <- paste0(as.expression(get_aes_var(mapping, "subset")), '& (isTip & (angle >= 90 & angle <= 270))')
143
+            }
144
+            m1 <- aes_string(angle = "angle", node = "node", subset = newsubset1)
145
+            m2 <- aes_string(angle = "angle+180", node = "node", subset = newsubset2)
133 146
         }
134 147
         m1 <- modifyList(mapping, m1)
135 148
         m2 <- modifyList(mapping, m2)
136 149
     }
137
-
138
-    list(geom_tiplab_rectangular(m1, hjust=hjust, ...),
139
-         geom_tiplab_rectangular(m2, hjust=1-hjust, ...)
150
+    params[["nodelab"]] <- NULL
151
+    params1 <- params2 <- params
152
+    params1[["mapping"]] <- m1
153
+    params1[["hjust"]] <- hjust
154
+    params2[["mapping"]] <- m2
155
+    params2[["hjust"]] <- 1-hjust
156
+    list(do.call("geom_tiplab_rectangular", params1),
157
+         do.call("geom_tiplab_rectangular", params2)
140 158
          )
141 159
 }
142 160
 
Browse code

fixed r check

Guangchuang Yu authored on 10/10/2020 07:10:00
Showing 1 changed files
... ...
@@ -161,7 +161,7 @@ geom_tiplab_circular <- geom_tiplab2
161 161
 #' tree <- rtree(5)
162 162
 #' tree$tip.label[2] <- "long string for test"
163 163
 #' label_pad(tree$tip.label)
164
-label_pad <- function(label, justify = "right", pad = "·") {
164
+label_pad <- function(label, justify = "right", pad = "\u00B7") {
165 165
     x <- format(label, 
166 166
                 width = max(nchar(label)),
167 167
                 justify = justify)
Browse code

label_pad

Guangchuang Yu authored on 09/10/2020 15:27:38
Showing 1 changed files
... ...
@@ -42,7 +42,9 @@ geom_tiplab_as_ylab <- function(hjust = 0, position = "right", ...) {
42 42
               )
43 43
 }
44 44
 
45
-geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dotted", linesize=0.5, geom="text",  offset=0, fontface = "plain", ...) {
45
+geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE, 
46
+                                    linetype = "dotted", linesize=0.5, geom="text",  
47
+                                    offset=0, family = "", fontface = "plain", ...) {
46 48
     geom <- match.arg(geom, c("text", "label", "shadowtext", "image", "phylopic"))
47 49
     if (geom == "text") {
48 50
         label_geom <- geom_text2
... ...
@@ -98,7 +100,8 @@ geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE, lin
98 100
                        hjust = hjust, nudge_x = offset, stat = StatTreeData, ...)            
99 101
         } else {
100 102
             label_geom(mapping=text_mapping,
101
-                       hjust = hjust, nudge_x = offset, stat = StatTreeData, fontface = fontface, ...)
103
+                       hjust = hjust, nudge_x = offset, stat = StatTreeData, 
104
+                       family = family, fontface = fontface, ...)
102 105
         }
103 106
     )
104 107
 }
... ...
@@ -139,3 +142,37 @@ geom_tiplab2 <- function(mapping=NULL, hjust=0, ...) {
139 142
 
140 143
 geom_tiplab_circular <- geom_tiplab2
141 144
 
145
+
146
+
147
+#' Padding taxa labels
148
+#'
149
+#' This function add padding character to the left side of taxa labels.
150
+#' @param label taxa label 
151
+#' @param justify should a character vector be left-justified, right-justified (default), centred or left alone.
152
+#' @param pad padding character (default is a dot)
153
+#'
154
+#' @return Taxa labels with padding characters added
155
+#' @export
156
+#' @author Guangchuang Yu and Yonghe Xia
157
+#' @references <https://groups.google.com/g/bioc-ggtree/c/INJ0Nfkq3b0/m/lXefnfV5AQAJ>
158
+#' @examples
159
+#' library(ggtree)
160
+#' set.seed(2015-12-21)
161
+#' tree <- rtree(5)
162
+#' tree$tip.label[2] <- "long string for test"
163
+#' label_pad(tree$tip.label)
164
+label_pad <- function(label, justify = "right", pad = "·") {
165
+    x <- format(label, 
166
+                width = max(nchar(label)),
167
+                justify = justify)
168
+    len <- vapply(gregexpr("^\\s+", x),
169
+                  attr, "match.length",
170
+                  FUN.VALUE = numeric(1))
171
+    len[len<0] <- 0
172
+    
173
+    y <- vapply(len, 
174
+                function(i) paste0(rep(pad, each=i), collapse = ''),
175
+                FUN.VALUE = character(1))
176
+    paste0(y, label)
177
+}
178
+
Browse code

shadowtext

Guangchuang Yu authored on 02/09/2020 08:44:40
Showing 1 changed files
... ...
@@ -8,7 +8,7 @@
8 8
 ##' @param align align tip lab or not, logical
9 9
 ##' @param linetype linetype for adding line if align = TRUE
10 10
 ##' @param linesize line size of line if align = TRUE
11
-##' @param geom one of 'text', 'label', 'image' and 'phylopic'
11
+##' @param geom one of 'text', 'label', 'shadowtext', 'image' and 'phylopic'
12 12
 ##' @param as_ylab display tip labels as y-axis label, only works for rectangular and dendrogram layouts
13 13
 ##' @param ... additional parameter
14 14
 ##' @return tip label layer
... ...
@@ -43,11 +43,13 @@ geom_tiplab_as_ylab <- function(hjust = 0, position = "right", ...) {
43 43
 }
44 44
 
45 45
 geom_tiplab_rectangular <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dotted", linesize=0.5, geom="text",  offset=0, fontface = "plain", ...) {
46
-    geom <- match.arg(geom, c("text", "label", "image", "phylopic"))
46
+    geom <- match.arg(geom, c("text", "label", "shadowtext", "image", "phylopic"))
47 47
     if (geom == "text") {
48 48
         label_geom <- geom_text2
49 49
     } else if (geom == "label") {
50 50
         label_geom <- geom_label2
51
+    } else if (geom == 'shadowtext') {
52
+        label_geom <- get_fun_from_pkg("shadowtext", "geom_shadowtext")
51 53
     } else if (geom == "image") {
52 54
         label_geom <- get_fun_from_pkg("ggimage", "geom_image")
53 55
     } else if (geom == "phylopic") {
Browse code

as_ylab in geom_tiplab()

Guangchuang Yu authored on 28/07/2020 06:36:51
Showing 1 changed files
... ...
@@ -9,7 +9,7 @@
9 9
 ##' @param linetype linetype for adding line if align = TRUE
10 10
 ##' @param linesize line size of line if align = TRUE
11 11
 ##' @param geom one of 'text', 'label', 'image' and 'phylopic'
12
-##' @param as_ylab display tip labels as y-axis label, only works for rectangular layout
12
+##' @param as_ylab display tip labels as y-axis label, only works for rectangular and dendrogram layouts
13 13
 ##' @param ... additional parameter
14 14
 ##' @return tip label layer
15 15
 ##' @importFrom ggplot2 geom_text
Browse code

as_ylab in geom_tiplab()

Guangchuang Yu authored on 28/07/2020 06:04:40
Showing 1 changed files
... ...
@@ -9,6 +9,7 @@
9 9
 ##' @param linetype linetype for adding line if align = TRUE
10 10
 ##' @param linesize line size of line if align = TRUE
11 11
 ##' @param geom one of 'text', 'label', 'image' and 'phylopic'
12
+##' @param as_ylab display tip labels as y-axis label, only works for rectangular layout
12 13
 ##' @param ... additional parameter
13 14
 ##' @return tip label layer
14 15
 ##' @importFrom ggplot2 geom_text
... ...
@@ -19,7 +20,8 @@
19 20
 ##' require(ape)
20 21
 ##' tr <- rtree(10)
21 22
 ##' ggtree(tr) + geom_tiplab()
22
-geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dotted", linesize=0.5, geom="text",  offset=0, ...) {
23
+geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dotted",
24
+                        linesize=0.5, geom="text",  offset=0, as_ylab = FALSE, ...) {
23 25
     structure(list(mapping = mapping,
24 26
                    hjust = hjust,
25 27
                    align = align,
... ...
@@ -27,10 +29,19 @@ geom_tiplab <- function(mapping=NULL, hjust = 0,  align = FALSE, linetype = "dot
27 29
                    linesize = linesize,
28 30
                    geom = geom,
29 31
                    offset = offset,
32
+                   as_ylab = as_ylab,
30 33
                    ...),