Browse code

Merge branch 'master' into dev_for_new_ggplot2

xiangpin authored on 23/08/2022 12:39:47
Showing 0 changed files
Browse code

roundrect and ellipse layout incorporate with coord_flip

xiangpin authored on 12/08/2022 12:08:10
Showing 1 changed files
... ...
@@ -204,6 +204,9 @@ GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
204 204
         trans$curvature <- curvature
205 205
     }else{
206 206
         trans <- coord$transform(data, panel_params)
207
+        if (inherits(coord, 'CoordFlip')){ 
208
+            trans$curvature <- -1 * trans$curvature
209
+        }
207 210
     }
208 211
     arrow.fill <- arrow.fill %|||% trans$colour
209 212
 
... ...
@@ -225,6 +228,7 @@ GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
225 228
   }
226 229
 )
227 230
 
231
+
228 232
 # for inward curve lines
229 233
 generate_curvature <- function(starttheta, endtheta, hratio, ncp){
230 234
     flag <- endtheta - starttheta
Browse code

rename size aesthetic according to ggplot2 (3.4.0)

xiangpin authored on 07/07/2022 12:57:13
Showing 1 changed files
... ...
@@ -1,7 +1,7 @@
1 1
 #' link between taxa 
2 2
 #'
3 3
 #' `geom_taxalink` supports data.frame as input,
4
-#' the `colour`, `size`, `linetype` and `alpha` can be mapped. When the `data` was provided, 
4
+#' the `colour`, `linewidth`, `linetype` and `alpha` can be mapped. When the `data` was provided, 
5 5
 #' the `mapping` should be also provided, which `taxa1` and `taxa2` should be mapped created 
6 6
 #' by `aes`, `aes_` or `aes_string`. In addition, the `hratio`, control the height of curve line, 
7 7
 #' when tree layout is `cirular`, default is 1. `ncp`, the number of control points used to draw the 
... ...
@@ -25,7 +25,7 @@
25 25
 #'        \item \code{group} group category of link.
26 26
 #'        \item \code{colour} control the color of line, default is black.
27 27
 #'        \item \code{linetype} control the type of line, default is 1 (solid).
28
-#'        \item \code{size} control the width of line, default is 0.5.
28
+#'        \item \code{linewidth} control the width of line, default is 0.5.
29 29
 #'        \item \code{curvature} control the curvature of line, default is 0.5, 
30 30
 #'        it will be created automatically in polar coordinate .
31 31
 #'        \item \code{hratio} control the height of curve line, default is 1.
... ...
@@ -175,7 +175,8 @@ geom_curvelink <- function(data=NULL,
175 175
 #' @importFrom scales alpha
176 176
 GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
177 177
   required_aes = c("x", "y", "xend", "yend"),
178
-  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5, hratio=1, ncp=1, curveangle=90, square=FALSE),
178
+  default_aes = aes(colour = "black", linewidth = 0.5, linetype = 1, alpha = NA, curvature=0.5, hratio=1, ncp=1, curveangle=90, square=FALSE),
179
+  rename_size = TRUE,
179 180
   draw_panel = function(data, panel_params, coord, shape=0.5, outward=TRUE,
180 181
                         arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
181 182
     if (!coord$is_linear()) {
... ...
@@ -215,7 +216,7 @@ GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
215 216
                               square = trans$square[i], squareShape = 1, inflect = FALSE, open = TRUE,
216 217
                               gp = gpar(col = alpha(trans$colour[i], trans$alpha[i]),
217 218
                                         fill = alpha(arrow.fill[i], trans$alpha[i]),
218
-                                        lwd = trans$size[i] * ggplot2::.pt,
219
+                                        lwd = trans$linewidth[i] * ggplot2::.pt,
219 220
                                         lty = trans$linetype[i],
220 221
                                         lineend = lineend),
221 222
                               arrow = arrow,
Browse code

don't inherit aes for geom_cladelab and geom_taxalink

xiangpin authored on 28/04/2021 07:38:27
Showing 1 changed files
... ...
@@ -53,7 +53,7 @@ geom_taxalink <- function(data=NULL,
53 53
     }
54 54
 
55 55
     
56
-    params <- list(...)
56
+    params <- list(inherit.aes=FALSE, ...)
57 57
     structure(list(data    = data,
58 58
                    mapping = mapping,
59 59
                    taxa1   = taxa1,
Browse code

import .pt

xiangpin authored on 10/10/2020 08:19:52
Showing 1 changed files
... ...
@@ -215,7 +215,7 @@ GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
215 215
                               square = trans$square[i], squareShape = 1, inflect = FALSE, open = TRUE,
216 216
                               gp = gpar(col = alpha(trans$colour[i], trans$alpha[i]),
217 217
                                         fill = alpha(arrow.fill[i], trans$alpha[i]),
218
-                                        lwd = trans$size[i] * .pt,
218
+                                        lwd = trans$size[i] * ggplot2::.pt,
219 219
                                         lty = trans$linetype[i],
220 220
                                         lineend = lineend),
221 221
                               arrow = arrow,
Browse code

add another new layout roundrect

xiangpin authored on 28/09/2020 09:02:12
Showing 1 changed files
... ...
@@ -175,7 +175,7 @@ geom_curvelink <- function(data=NULL,
175 175
 #' @importFrom scales alpha
176 176
 GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
177 177
   required_aes = c("x", "y", "xend", "yend"),
178
-  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5, hratio=1, ncp=1, curveangle=90),
178
+  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5, hratio=1, ncp=1, curveangle=90, square=FALSE),
179 179
   draw_panel = function(data, panel_params, coord, shape=0.5, outward=TRUE,
180 180
                         arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
181 181
     if (!coord$is_linear()) {
... ...
@@ -212,7 +212,7 @@ GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
212 212
                               trans$x[i], trans$y[i], trans$xend[i], trans$yend[i],
213 213
                               default.units = "native",
214 214
                               curvature = trans$curvature[i], angle = trans$curveangle[i], ncp = trans$ncp[i],
215
-                              square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
215
+                              square = trans$square[i], squareShape = 1, inflect = FALSE, open = TRUE,
216 216
                               gp = gpar(col = alpha(trans$colour[i], trans$alpha[i]),
217 217
                                         fill = alpha(arrow.fill[i], trans$alpha[i]),
218 218
                                         lwd = trans$size[i] * .pt,
Browse code

update curve to fit the ellipse layout

xiangpin authored on 28/09/2020 08:04:55
Showing 1 changed files
... ...
@@ -144,7 +144,6 @@ geom_curvelink <- function(data=NULL,
144 144
                            mapping=NULL, 
145 145
                            stat = "identity", 
146 146
                            position = "identity",
147
-                           angle = 90,
148 147
                            arrow = NULL,
149 148
                            arrow.fill = NULL,
150 149
                            lineend = "butt",
... ...
@@ -163,7 +162,6 @@ geom_curvelink <- function(data=NULL,
163 162
        params = list(
164 163
          arrow = arrow,
165 164
          arrow.fill = arrow.fill,
166
-         angle = angle,
167 165
          lineend = lineend,
168 166
          na.rm = na.rm,
169 167
          ...
... ...
@@ -177,8 +175,8 @@ geom_curvelink <- function(data=NULL,
177 175
 #' @importFrom scales alpha
178 176
 GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
179 177
   required_aes = c("x", "y", "xend", "yend"),
180
-  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5, hratio=1, ncp=1),
181
-  draw_panel = function(data, panel_params, coord, angle = 90, shape=0.5, outward=TRUE,
178
+  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5, hratio=1, ncp=1, curveangle=90),
179
+  draw_panel = function(data, panel_params, coord, shape=0.5, outward=TRUE,
182 180
                         arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
183 181
     if (!coord$is_linear()) {
184 182
         tmpgroup <- data$group
... ...
@@ -213,7 +211,7 @@ GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
213 211
                         curveGrob(
214 212
                               trans$x[i], trans$y[i], trans$xend[i], trans$yend[i],
215 213
                               default.units = "native",
216
-                              curvature = trans$curvature[i], angle = angle, ncp = trans$ncp[i],
214
+                              curvature = trans$curvature[i], angle = trans$curveangle[i], ncp = trans$ncp[i],
217 215
                               square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
218 216
                               gp = gpar(col = alpha(trans$colour[i], trans$alpha[i]),
219 217
                                         fill = alpha(arrow.fill[i], trans$alpha[i]),
Browse code

update rd of geom_taxalink

xiangpin authored on 23/07/2020 06:30:01
Showing 1 changed files
... ...
@@ -20,15 +20,16 @@
20 20
 #' @section Aesthetics:
21 21
 #' \code{geom_taxalink()} understands the following aesthethics (required aesthetics are in bold):
22 22
 #'     \itemize{
23
-#'        \item \strong{\code{taxa1}}
24
-#'        \item \strong{\code{taxa2}}
25
-#'        \item \code{group}
26
-#'        \item \code{colour}
27
-#'        \item \code{linetype}
28
-#'        \item \code{size}
29
-#'        \item \code{curvature}
30
-#'        \item \code{hratio}
31
-#'        \item \code{ncp}
23
+#'        \item \strong{\code{taxa1}} label or node number of tree.
24
+#'        \item \strong{\code{taxa2}} label or node number of tree.
25
+#'        \item \code{group} group category of link.
26
+#'        \item \code{colour} control the color of line, default is black.
27
+#'        \item \code{linetype} control the type of line, default is 1 (solid).
28
+#'        \item \code{size} control the width of line, default is 0.5.
29
+#'        \item \code{curvature} control the curvature of line, default is 0.5, 
30
+#'        it will be created automatically in polar coordinate .
31
+#'        \item \code{hratio} control the height of curve line, default is 1.
32
+#'        \item \code{ncp} control the smooth of curve line, default is 1.
32 33
 #'     }
33 34
 #' @return a list object.
34 35
 #' @export
Browse code

geom_taxalink supports aes mapping

Guangchuang Yu authored on 20/07/2020 14:59:17
Showing 1 changed files
... ...
@@ -1,76 +1,142 @@
1
-##' link between taxa
2
-##'
3
-##'
4
-##' @title geom_taxalink
5
-##' @param taxa1 taxa1, can be label or node number
6
-##' @param taxa2 taxa2, can be label or node number
7
-##' @param curvature A numeric value giving the amount of curvature.
8
-##' Negative values produce left-hand curves,
9
-##' positive values produce right-hand curves, and zero produces a straight line.
10
-##' @param arrow specification for arrow heads, as created by arrow().
11
-##' @param arrow.fill fill color to usse for the arrow head (if closed). `NULL` means use `colour` aesthetic.
12
-##' @param offset numeric, control the shift of curve line (the ratio of axis value, 
13
-##' range is "(0-1)"), default is NULL.
14
-##' @param hratio numeric, the height of curve line, default is 1.
15
-##' @param outward logical, control the orientation of curve when the layout of tree is circular, 
16
-##' fan or other layout in polar coordinate, default is TRUE.
17
-##' @param ... additional parameter.
18
-##' @return ggplot layer
19
-##' @export
20
-##' @author Guangchuang Yu
21
-geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, 
22
-                          arrow.fill = NULL, offset=NULL, hratio=1, 
23
-                          outward = TRUE, ...) {
24
-    position = "identity"
25
-    show.legend = NA
26
-    na.rm = TRUE
27
-    inherit.aes = FALSE
28
-
29
-    mapping <- aes_(x=~x, y=~y, node=~node, label=~label, xend=~x, yend=~y)
30
-
31
-    layer(stat=StatTaxalink,
32
-          mapping=mapping,
33
-          data = NULL,
34
-          geom=GeomCurvelink,
35
-          position='identity',
36
-          show.legend=show.legend,
37
-          inherit.aes = inherit.aes,
38
-          params = list(taxa1 = taxa1,
39
-                        taxa2 = taxa2,
40
-                        curvature = curvature,
41
-                        na.rm = na.rm,
42
-                        arrow = arrow,
43
-                        arrow.fill = arrow.fill,
44
-                        offset = offset,
45
-                        hratio = hratio,
46
-                        outward = outward,
47
-                        ...),
48
-          check.aes = FALSE
49
-          )
1
+#' link between taxa 
2
+#'
3
+#' `geom_taxalink` supports data.frame as input,
4
+#' the `colour`, `size`, `linetype` and `alpha` can be mapped. When the `data` was provided, 
5
+#' the `mapping` should be also provided, which `taxa1` and `taxa2` should be mapped created 
6
+#' by `aes`, `aes_` or `aes_string`. In addition, the `hratio`, control the height of curve line, 
7
+#' when tree layout is `cirular`, default is 1. `ncp`, the number of control points used to draw the 
8
+#' curve, more control points creates a smoother curve, default is 1. They also can be mapped to
9
+#' a column of data. 
10
+#' 
11
+#' @param data data.frame, The data to be displayed in this layer, default is NULL.
12
+#' @param mapping Set of aesthetic mappings, default is NULL.
13
+#' @param taxa1 can be label or node number.
14
+#' @param taxa2 can be label or node number.
15
+#' @param offset numeric, control the shift of curve line (the ratio of axis value,
16
+#' range is "(0-1)"), default is NULL.
17
+#' @param outward logical, control the orientation of curve when the layout of tree is circular,
18
+#' fan or other layout in polar coordinate, default is "auto", meaning It will automatically.
19
+#' @param ..., additional parameter.
20
+#' @section Aesthetics:
21
+#' \code{geom_taxalink()} understands the following aesthethics (required aesthetics are in bold):
22
+#'     \itemize{
23
+#'        \item \strong{\code{taxa1}}
24
+#'        \item \strong{\code{taxa2}}
25
+#'        \item \code{group}
26
+#'        \item \code{colour}
27
+#'        \item \code{linetype}
28
+#'        \item \code{size}
29
+#'        \item \code{curvature}
30
+#'        \item \code{hratio}
31
+#'        \item \code{ncp}
32
+#'     }
33
+#' @return a list object.
34
+#' @export
35
+geom_taxalink <- function(data=NULL, 
36
+                          mapping=NULL,
37
+                          taxa1=NULL, 
38
+                          taxa2=NULL, 
39
+                          offset = NULL,
40
+                          outward = "auto",
41
+                          ...){
42
+
43
+    if(is.character(data) && is.character(mapping)) {
44
+        ## may be taxa1 and taxa2 passed by position in previous version
45
+        ## calls <- names(sapply(match.call(), deparse))[-1]
46
+        message("taxa1 and taxa2 is not in the 1st and 2nd positions of the parameter list.\n",
47
+                "Please specify parameter name in future as this backward compatibility will be removed.\n" )
48
+        taxa1 <- data
49
+        taxa2 <- mapping
50
+        data <- NULL
51
+        mapping <- NULL
52
+    }
53
+
54
+    
55
+    params <- list(...)
56
+    structure(list(data    = data,
57
+                   mapping = mapping,
58
+                   taxa1   = taxa1,
59
+                   taxa2   = taxa2,
60
+                   offset  = offset, 
61
+                   outward = outward,
62
+                   params  = params), 
63
+              class = 'taxalink')
50 64
 }
51 65
 
52
-StatTaxalink <- ggproto("StatTaxalink", Stat,
53
-                        compute_group = function(self, data, scales, params, taxa1, taxa2, offset) {
54
-                            node1 <- taxa2node(data, taxa1)
55
-                            node2 <- taxa2node(data, taxa2)
56
-                            x <- data$x
57
-                            y <- data$y
58
-                            if (!is.null(offset)){
59
-                                tmpshift <- offset * (max(x, na.rm=TRUE)-min(x, na.rm=TRUE))
60
-                                data.frame(x    = x[node1] + tmpshift,
61
-                                           xend = x[node2] + tmpshift,
62
-                                           y    = y[node1],
63
-                                           yend = y[node2])
64
-                            }else{
65
-
66
-                                data.frame(x = x[node1],
67
-                                           xend = x[node2],
68
-                                           y = y[node1],
69
-                                           yend = y[node2])
70
-                            }
71
-                        },
72
-                        required_aes = c("x", "y", "xend", "yend")
73
-                        )
66
+
67
+## ##' link between taxa
68
+## ##'
69
+## ##'
70
+## ##' @title geom_taxalink
71
+## ##' @param taxa1 taxa1, can be label or node number
72
+## ##' @param taxa2 taxa2, can be label or node number
73
+## ##' @param curvature A numeric value giving the amount of curvature.
74
+## ##' Negative values produce left-hand curves,
75
+## ##' positive values produce right-hand curves, and zero produces a straight line.
76
+## ##' @param arrow specification for arrow heads, as created by arrow().
77
+## ##' @param arrow.fill fill color to usse for the arrow head (if closed). `NULL` means use `colour` aesthetic.
78
+## ##' @param offset numeric, control the shift of curve line (the ratio of axis value, 
79
+## ##' range is "(0-1)"), default is NULL.
80
+## ##' @param hratio numeric, the height of curve line, default is 1.
81
+## ##' @param outward logical, control the orientation of curve when the layout of tree is circular, 
82
+## ##' fan or other layout in polar coordinate, default is TRUE.
83
+## ##' @param ... additional parameter.
84
+## ##' @return ggplot layer
85
+## ##' @export
86
+## ##' @author Guangchuang Yu
87
+## geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, 
88
+##                           arrow.fill = NULL, offset=NULL, hratio=1, 
89
+##                           outward = TRUE, ...) {
90
+##     position = "identity"
91
+##     show.legend = NA
92
+##     na.rm = TRUE
93
+##     inherit.aes = FALSE
94
+
95
+##     mapping <- aes_(x=~x, y=~y, node=~node, label=~label, xend=~x, yend=~y)
96
+
97
+##     layer(stat=StatTaxalink,
98
+##           mapping=mapping,
99
+##           data = NULL,
100
+##           geom=GeomCurvelink,
101
+##           position='identity',
102
+##           show.legend=show.legend,
103
+##           inherit.aes = inherit.aes,
104
+##           params = list(taxa1 = taxa1,
105
+##                         taxa2 = taxa2,
106
+##                         curvature = curvature,
107
+##                         na.rm = na.rm,
108
+##                         arrow = arrow,
109
+##                         arrow.fill = arrow.fill,
110
+##                         offset = offset,
111
+##                         hratio = hratio,
112
+##                         outward = outward,
113
+##                         ...),
114
+##           check.aes = FALSE
115
+##           )
116
+## }
117
+
118
+## StatTaxalink <- ggproto("StatTaxalink", Stat,
119
+##                         compute_group = function(self, data, scales, params, taxa1, taxa2, offset) {
120
+##                             node1 <- taxa2node(data, taxa1)
121
+##                             node2 <- taxa2node(data, taxa2)
122
+##                             x <- data$x
123
+##                             y <- data$y
124
+##                             if (!is.null(offset)){
125
+##                                 tmpshift <- offset * (max(x, na.rm=TRUE)-min(x, na.rm=TRUE))
126
+##                                 data.frame(x    = x[node1] + tmpshift,
127
+##                                            xend = x[node2] + tmpshift,
128
+##                                            y    = y[node1],
129
+##                                            yend = y[node2])
130
+##                             }else{
131
+
132
+##                                 data.frame(x = x[node1],
133
+##                                            xend = x[node2],
134
+##                                            y = y[node1],
135
+##                                            yend = y[node2])
136
+##                             }
137
+##                         },
138
+##                         required_aes = c("x", "y", "xend", "yend")
139
+##                         )
74 140
 
75 141
 
76 142
 geom_curvelink <- function(data=NULL, 
Browse code

update curve layer

xiangpin authored on 19/07/2020 08:00:22
Showing 1 changed files
... ...
@@ -31,7 +31,7 @@ geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL,
31 31
     layer(stat=StatTaxalink,
32 32
           mapping=mapping,
33 33
           data = NULL,
34
-          geom=GeomCurveLink,
34
+          geom=GeomCurvelink,
35 35
           position='identity',
36 36
           show.legend=show.legend,
37 37
           inherit.aes = inherit.aes,
... ...
@@ -53,7 +53,6 @@ StatTaxalink <- ggproto("StatTaxalink", Stat,
53 53
                         compute_group = function(self, data, scales, params, taxa1, taxa2, offset) {
54 54
                             node1 <- taxa2node(data, taxa1)
55 55
                             node2 <- taxa2node(data, taxa2)
56
-                           
57 56
                             x <- data$x
58 57
                             y <- data$y
59 58
                             if (!is.null(offset)){
... ...
@@ -73,14 +72,47 @@ StatTaxalink <- ggproto("StatTaxalink", Stat,
73 72
                         required_aes = c("x", "y", "xend", "yend")
74 73
                         )
75 74
 
75
+
76
+geom_curvelink <- function(data=NULL, 
77
+                           mapping=NULL, 
78
+                           stat = "identity", 
79
+                           position = "identity",
80
+                           angle = 90,
81
+                           arrow = NULL,
82
+                           arrow.fill = NULL,
83
+                           lineend = "butt",
84
+                           na.rm = FALSE,
85
+                           show.legend = NA,
86
+                           inherit.aes = TRUE,...){
87
+
88
+    layer(
89
+       data = data,
90
+       mapping = mapping,
91
+       stat = stat,
92
+       geom = GeomCurvelink,
93
+       position = position,
94
+       show.legend = show.legend,
95
+       inherit.aes = inherit.aes,
96
+       params = list(
97
+         arrow = arrow,
98
+         arrow.fill = arrow.fill,
99
+         angle = angle,
100
+         lineend = lineend,
101
+         na.rm = na.rm,
102
+         ...
103
+       )
104
+    )
105
+
106
+}
107
+
76 108
 #' @importFrom ggplot2 GeomSegment
77 109
 #' @importFrom grid gTree curveGrob gpar
78 110
 #' @importFrom scales alpha
79
-GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
80
-  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5),
81
-  draw_panel = function(data, panel_params, coord, angle = 90, shape=0.5, hratio=1, outward=TRUE,
82
-                        ncp = 1, arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
83
-
111
+GeomCurvelink <- ggproto("GeomCurvelink", GeomSegment,
112
+  required_aes = c("x", "y", "xend", "yend"),
113
+  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5, hratio=1, ncp=1),
114
+  draw_panel = function(data, panel_params, coord, angle = 90, shape=0.5, outward=TRUE,
115
+                        arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
84 116
     if (!coord$is_linear()) {
85 117
         tmpgroup <- data$group
86 118
         starts <- subset(data, select = c(-xend, -yend))
... ...
@@ -94,11 +126,11 @@ GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
94 126
         ends <- trans[trans$group==2, ,drop=FALSE]
95 127
         if (outward){
96 128
             curvature <- unlist(mapply(generate_curvature2, starttheta=starts$theta,
97
-                                       endtheta=ends$theta, MoreArgs=list(hratio=hratio, ncp=ncp),
129
+                                       endtheta=ends$theta, hratio=starts$hratio, ncp=starts$ncp,
98 130
                                        SIMPLIFY=FALSE))
99 131
         }else{
100 132
             curvature <- unlist(mapply(generate_curvature, starttheta=starts$theta, 
101
-                                       endtheta=ends$theta, MoreArgs=list(hratio=hratio, ncp=ncp), 
133
+                                       endtheta=ends$theta, hratio=starts$hratio, ncp=starts$ncp, 
102 134
                                        SIMPLIFY=FALSE))
103 135
         }
104 136
         ends <- rename(subset(ends, select=c(x, y)), c("xend"="x", "yend"="y"))
... ...
@@ -114,7 +146,7 @@ GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
114 146
                         curveGrob(
115 147
                               trans$x[i], trans$y[i], trans$xend[i], trans$yend[i],
116 148
                               default.units = "native",
117
-                              curvature = trans$curvature[i], angle = angle, ncp = ncp,
149
+                              curvature = trans$curvature[i], angle = angle, ncp = trans$ncp[i],
118 150
                               square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
119 151
                               gp = gpar(col = alpha(trans$colour[i], trans$alpha[i]),
120 152
                                         fill = alpha(arrow.fill[i], trans$alpha[i]),
... ...
@@ -124,7 +156,7 @@ GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
124 156
                               arrow = arrow,
125 157
                               shape = shape)})
126 158
     class(grobs) <- "gList"
127
-    return(ggname("geom_curve_link", gTree(children=grobs)))
159
+    return(ggname("geom_curvelink", gTree(children=grobs)))
128 160
   }
129 161
 )
130 162
 
Browse code

Merge branch 'temp'

xiangpin authored on 16/07/2020 03:30:55
Showing 0 changed files
Browse code

update geom_taxalink method

xiangpin authored on 16/07/2020 03:29:46
Showing 1 changed files
... ...
@@ -9,15 +9,18 @@
9 9
 ##' positive values produce right-hand curves, and zero produces a straight line.
10 10
 ##' @param arrow specification for arrow heads, as created by arrow().
11 11
 ##' @param arrow.fill fill color to usse for the arrow head (if closed). `NULL` means use `colour` aesthetic.
12
-##' @param xexpand numeric, control the shift of curve line (the ratio of axis value, 
13
-##' rang is "(0-1)"), default is NULL.
14
-##' @param hratio numeric, the height of curve line, default is 0.5.
15
-##' @param ... additional parameter
12
+##' @param offset numeric, control the shift of curve line (the ratio of axis value, 
13
+##' range is "(0-1)"), default is NULL.
14
+##' @param hratio numeric, the height of curve line, default is 1.
15
+##' @param outward logical, control the orientation of curve when the layout of tree is circular, 
16
+##' fan or other layout in polar coordinate, default is TRUE.
17
+##' @param ... additional parameter.
16 18
 ##' @return ggplot layer
17 19
 ##' @export
18 20
 ##' @author Guangchuang Yu
19 21
 geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, 
20
-                          arrow.fill = NULL, xexpand=NULL, hratio=0.5, ...) {
22
+                          arrow.fill = NULL, offset=NULL, hratio=1, 
23
+                          outward = TRUE, ...) {
21 24
     position = "identity"
22 25
     show.legend = NA
23 26
     na.rm = TRUE
... ...
@@ -38,22 +41,23 @@ geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL,
38 41
                         na.rm = na.rm,
39 42
                         arrow = arrow,
40 43
                         arrow.fill = arrow.fill,
41
-                        xexpand = xexpand,
44
+                        offset = offset,
42 45
                         hratio = hratio,
46
+                        outward = outward,
43 47
                         ...),
44 48
           check.aes = FALSE
45 49
           )
46 50
 }
47 51
 
48 52
 StatTaxalink <- ggproto("StatTaxalink", Stat,
49
-                        compute_group = function(self, data, scales, params, taxa1, taxa2, xexpand) {
53
+                        compute_group = function(self, data, scales, params, taxa1, taxa2, offset) {
50 54
                             node1 <- taxa2node(data, taxa1)
51 55
                             node2 <- taxa2node(data, taxa2)
52 56
                            
53 57
                             x <- data$x
54 58
                             y <- data$y
55
-                            if (!is.null(xexpand)){
56
-                                tmpshift <- xexpand * (max(x, na.rm=TRUE)-min(x, na.rm=TRUE))
59
+                            if (!is.null(offset)){
60
+                                tmpshift <- offset * (max(x, na.rm=TRUE)-min(x, na.rm=TRUE))
57 61
                                 data.frame(x    = x[node1] + tmpshift,
58 62
                                            xend = x[node2] + tmpshift,
59 63
                                            y    = y[node1],
... ...
@@ -71,9 +75,10 @@ StatTaxalink <- ggproto("StatTaxalink", Stat,
71 75
 
72 76
 #' @importFrom ggplot2 GeomSegment
73 77
 #' @importFrom grid gTree curveGrob gpar
78
+#' @importFrom scales alpha
74 79
 GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
75 80
   default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5),
76
-  draw_panel = function(data, panel_params, coord, angle = 90, shape=0.5, hratio=0.5,
81
+  draw_panel = function(data, panel_params, coord, angle = 90, shape=0.5, hratio=1, outward=TRUE,
77 82
                         ncp = 1, arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
78 83
 
79 84
     if (!coord$is_linear()) {
... ...
@@ -87,9 +92,15 @@ GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
87 92
         trans <- coord$transform(pieces, panel_params)
88 93
         starts <- trans[trans$group==1, ,drop=FALSE]
89 94
         ends <- trans[trans$group==2, ,drop=FALSE]
90
-        curvature <- unlist(mapply(generate_curvature, starttheta=starts$theta, 
91
-                                   endtheta=ends$theta, MoreArgs=list(hratio=hratio, ncp=ncp), 
92
-                                   SIMPLIFY=FALSE))
95
+        if (outward){
96
+            curvature <- unlist(mapply(generate_curvature2, starttheta=starts$theta,
97
+                                       endtheta=ends$theta, MoreArgs=list(hratio=hratio, ncp=ncp),
98
+                                       SIMPLIFY=FALSE))
99
+        }else{
100
+            curvature <- unlist(mapply(generate_curvature, starttheta=starts$theta, 
101
+                                       endtheta=ends$theta, MoreArgs=list(hratio=hratio, ncp=ncp), 
102
+                                       SIMPLIFY=FALSE))
103
+        }
93 104
         ends <- rename(subset(ends, select=c(x, y)), c("xend"="x", "yend"="y"))
94 105
         trans <- cbind(starts, ends)
95 106
         trans$group <- tmpgroup
... ...
@@ -117,46 +128,52 @@ GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
117 128
   }
118 129
 )
119 130
 
131
+# for inward curve lines
120 132
 generate_curvature <- function(starttheta, endtheta, hratio, ncp){
121 133
     flag <- endtheta - starttheta
122 134
     newflag <- min(c(abs(flag), 2*pi-abs(flag)))
123 135
     if (flag > 0){
124
-        if (flag <= pi/2){
125
-            origin_direction <- 1
126
-            if (ncp==1){
127
-                origin_direction <- origin_direction * hratio * 0.68 * pi/newflag
128
-            }
129
-        }else if (flag < pi && flag > pi/2){
136
+        if (flag <= pi){
130 137
             origin_direction <- 1
131
-        }else if (flag > pi && flag <=3*pi/2){
132
-            origin_direction <- -1
133 138
         }else{
134 139
             origin_direction <- -1
135
-            if (ncp==1){
136
-                origin_direction <- origin_direction * hratio * 0.68 * pi/newflag
137
-            }
138 140
         }
139 141
     }else{
140
-        if (abs(flag)<=pi/2){
141
-            origin_direction <- -1
142
-            if (ncp == 1){
143
-                origin_direction <- origin_direction * hratio * 0.68 * pi/newflag
144
-            }
145
-        }else if (abs(flag) < pi && abs(flag) > pi/2){
142
+        if (abs(flag)<=pi){
146 143
             origin_direction <- -1
147
-        }else if (abs(flag) > pi && abs(flag) <= 3*pi/2){
148
-            origin_direction <- 1
149 144
         }else{
150 145
             origin_direction <- 1
151
-            if (ncp==1){
152
-                origin_direction <- origin_direction * hratio * 0.68 * pi/newflag 
153
-            }
154 146
         }
155 147
     }
156
-    curvature <- origin_direction * (1 - newflag/pi)
148
+    curvature <- hratio * origin_direction * (1 - newflag/pi)
157 149
     return(curvature)
158 150
 }
159 151
 
152
+# for outward curve lines
153
+generate_curvature2 <- function(starttheta, endtheta, hratio, ncp){
154
+    flag <- endtheta - starttheta
155
+    newflag <- min(c(abs(flag), 2*pi-abs(flag)))
156
+    if (flag > 0){
157
+        if (flag <= pi){
158
+            origin_direction <- -1
159
+        }else{
160
+            origin_direction <- 1
161
+        }
162
+    }else{
163
+        if (abs(flag)<=pi){
164
+            origin_direction <- 1
165
+        }else{
166
+            origin_direction <- -1
167
+        }
168
+    }
169
+    if (newflag>pi/2){
170
+        curvature <- hratio * origin_direction * pi/newflag
171
+    }else{
172
+        curvature <- hratio * origin_direction * (1-newflag/pi)
173
+    }
174
+    return (curvature)
175
+}
176
+
160 177
 #' @importFrom utils getFromNamespace
161 178
 ggname <- getFromNamespace("ggname", "ggplot2")
162 179
 
Browse code

update docs

Guangchuang Yu authored on 16/07/2020 01:51:43
Showing 1 changed files
... ...
@@ -162,12 +162,15 @@ ggname <- getFromNamespace("ggname", "ggplot2")
162 162
 
163 163
 "%|||%" <- function(x, y){
164 164
     if (is.null(x)){
165
+        return(y)
166
+    }
167
+    if (is.null(y)) {
168
+        return(x)
169
+    }
170
+
171
+    if (length(x)<length(y)) {
165 172
         return (y)
166
-    }else{
167
-        if (length(x)<length(y)){
168
-            return (y)
169
-        }else{
170
-            return (x)
171
-        }
173
+    } else {
174
+        return (x)
172 175
     }
173 176
 }    
Browse code

optimize the method to calculate curvature

xiangpin authored on 13/07/2020 13:36:54
Showing 1 changed files
... ...
@@ -9,11 +9,15 @@
9 9
 ##' positive values produce right-hand curves, and zero produces a straight line.
10 10
 ##' @param arrow specification for arrow heads, as created by arrow().
11 11
 ##' @param arrow.fill fill color to usse for the arrow head (if closed). `NULL` means use `colour` aesthetic.
12
+##' @param xexpand numeric, control the shift of curve line (the ratio of axis value, 
13
+##' rang is "(0-1)"), default is NULL.
14
+##' @param hratio numeric, the height of curve line, default is 0.5.
12 15
 ##' @param ... additional parameter
13 16
 ##' @return ggplot layer
14 17
 ##' @export
15 18
 ##' @author Guangchuang Yu
16
-geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, arrow.fill = NULL, ...) {
19
+geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, 
20
+                          arrow.fill = NULL, xexpand=NULL, hratio=0.5, ...) {
17 21
     position = "identity"
18 22
     show.legend = NA
19 23
     na.rm = TRUE
... ...
@@ -34,24 +38,33 @@ geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, arrow.fill
34 38
                         na.rm = na.rm,
35 39
                         arrow = arrow,
36 40
                         arrow.fill = arrow.fill,
41
+                        xexpand = xexpand,
42
+                        hratio = hratio,
37 43
                         ...),
38 44
           check.aes = FALSE
39 45
           )
40 46
 }
41 47
 
42 48
 StatTaxalink <- ggproto("StatTaxalink", Stat,
43
-                        compute_group = function(self, data, scales, params, taxa1, taxa2) {
49
+                        compute_group = function(self, data, scales, params, taxa1, taxa2, xexpand) {
44 50
                             node1 <- taxa2node(data, taxa1)
45 51
                             node2 <- taxa2node(data, taxa2)
46 52
                            
47 53
                             x <- data$x
48 54
                             y <- data$y
55
+                            if (!is.null(xexpand)){
56
+                                tmpshift <- xexpand * (max(x, na.rm=TRUE)-min(x, na.rm=TRUE))
57
+                                data.frame(x    = x[node1] + tmpshift,
58
+                                           xend = x[node2] + tmpshift,
59
+                                           y    = y[node1],
60
+                                           yend = y[node2])
61
+                            }else{
49 62
 
50
-                            data.frame(x = x[node1],
51
-                                       xend = x[node2],
52
-                                       y = y[node1],
53
-                                       yend = y[node2])
54
-
63
+                                data.frame(x = x[node1],
64
+                                           xend = x[node2],
65
+                                           y = y[node1],
66
+                                           yend = y[node2])
67
+                            }
55 68
                         },
56 69
                         required_aes = c("x", "y", "xend", "yend")
57 70
                         )
... ...
@@ -60,8 +73,8 @@ StatTaxalink <- ggproto("StatTaxalink", Stat,
60 73
 #' @importFrom grid gTree curveGrob gpar
61 74
 GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
62 75
   default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5),
63
-  draw_panel = function(data, panel_params, coord, angle = 90,
64
-                        ncp = 5, arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
76
+  draw_panel = function(data, panel_params, coord, angle = 90, shape=0.5, hratio=0.5,
77
+                        ncp = 1, arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
65 78
 
66 79
     if (!coord$is_linear()) {
67 80
         tmpgroup <- data$group
... ...
@@ -74,7 +87,9 @@ GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
74 87
         trans <- coord$transform(pieces, panel_params)
75 88
         starts <- trans[trans$group==1, ,drop=FALSE]
76 89
         ends <- trans[trans$group==2, ,drop=FALSE]
77
-        curvature <- unlist(mapply(generate_curvature, starttheta=starts$theta, endtheta=ends$theta, SIMPLIFY=FALSE))
90
+        curvature <- unlist(mapply(generate_curvature, starttheta=starts$theta, 
91
+                                   endtheta=ends$theta, MoreArgs=list(hratio=hratio, ncp=ncp), 
92
+                                   SIMPLIFY=FALSE))
78 93
         ends <- rename(subset(ends, select=c(x, y)), c("xend"="x", "yend"="y"))
79 94
         trans <- cbind(starts, ends)
80 95
         trans$group <- tmpgroup
... ...
@@ -82,7 +97,7 @@ GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
82 97
     }else{
83 98
         trans <- coord$transform(data, panel_params)
84 99
     }
85
-    arrow.fill <- arrow.fill %||% trans$colour
100
+    arrow.fill <- arrow.fill %|||% trans$colour
86 101
 
87 102
     grobs <- lapply(seq_len(nrow(trans)), function(i){
88 103
                         curveGrob(
... ...
@@ -95,33 +110,64 @@ GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
95 110
                                         lwd = trans$size[i] * .pt,
96 111
                                         lty = trans$linetype[i],
97 112
                                         lineend = lineend),
98
-                                        arrow = arrow)})
113
+                              arrow = arrow,
114
+                              shape = shape)})
99 115
     class(grobs) <- "gList"
100 116
     return(ggname("geom_curve_link", gTree(children=grobs)))
101 117
   }
102 118
 )
103 119
 
104
-generate_curvature <- function(starttheta, endtheta){
120
+generate_curvature <- function(starttheta, endtheta, hratio, ncp){
105 121
     flag <- endtheta - starttheta
122
+    newflag <- min(c(abs(flag), 2*pi-abs(flag)))
106 123
     if (flag > 0){
107
-        if (flag < pi){
124
+        if (flag <= pi/2){
125
+            origin_direction <- 1
126
+            if (ncp==1){
127
+                origin_direction <- origin_direction * hratio * 0.68 * pi/newflag
128
+            }
129
+        }else if (flag < pi && flag > pi/2){
108 130
             origin_direction <- 1
131
+        }else if (flag > pi && flag <=3*pi/2){
132
+            origin_direction <- -1
109 133
         }else{
110 134
             origin_direction <- -1
135
+            if (ncp==1){
136
+                origin_direction <- origin_direction * hratio * 0.68 * pi/newflag
137
+            }
111 138
         }
112 139
     }else{
113
-        if (abs(flag) < pi){
114
-            origin_direction <- - 1
140
+        if (abs(flag)<=pi/2){
141
+            origin_direction <- -1
142
+            if (ncp == 1){
143
+                origin_direction <- origin_direction * hratio * 0.68 * pi/newflag
144
+            }
145
+        }else if (abs(flag) < pi && abs(flag) > pi/2){
146
+            origin_direction <- -1
147
+        }else if (abs(flag) > pi && abs(flag) <= 3*pi/2){
148
+            origin_direction <- 1
115 149
         }else{
116 150
             origin_direction <- 1
151
+            if (ncp==1){
152
+                origin_direction <- origin_direction * hratio * 0.68 * pi/newflag 
153
+            }
117 154
         }
118 155
     }
119
-    flag <- min(c(abs(flag), 2*pi-abs(flag)))
120
-    curvature <- origin_direction * (1 - flag/pi)
156
+    curvature <- origin_direction * (1 - newflag/pi)
121 157
     return(curvature)
122 158
 }
123 159
 
124 160
 #' @importFrom utils getFromNamespace
125
-"%||%" <- getFromNamespace("%||%", "ggplot2")
126
-
127 161
 ggname <- getFromNamespace("ggname", "ggplot2")
162
+
163
+"%|||%" <- function(x, y){
164
+    if (is.null(x)){
165
+        return (y)
166
+    }else{
167
+        if (length(x)<length(y)){
168
+            return (y)
169
+        }else{
170
+            return (x)
171
+        }
172
+    }
173
+}    
Browse code

support polar coordinate for taxalink

xiangpin authored on 13/07/2020 03:04:10
Showing 1 changed files
... ...
@@ -12,7 +12,6 @@
12 12
 ##' @param ... additional parameter
13 13
 ##' @return ggplot layer
14 14
 ##' @export
15
-##' @importFrom ggplot2 GeomCurve
16 15
 ##' @author Guangchuang Yu
17 16
 geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, arrow.fill = NULL, ...) {
18 17
     position = "identity"
... ...
@@ -25,7 +24,7 @@ geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, arrow.fill
25 24
     layer(stat=StatTaxalink,
26 25
           mapping=mapping,
27 26
           data = NULL,
28
-          geom=GeomCurve,
27
+          geom=GeomCurveLink,
29 28
           position='identity',
30 29
           show.legend=show.legend,
31 30
           inherit.aes = inherit.aes,
... ...
@@ -40,7 +39,6 @@ geom_taxalink <- function(taxa1, taxa2, curvature=0.5, arrow = NULL, arrow.fill
40 39
           )
41 40
 }
42 41
 
43
-
44 42
 StatTaxalink <- ggproto("StatTaxalink", Stat,
45 43
                         compute_group = function(self, data, scales, params, taxa1, taxa2) {
46 44
                             node1 <- taxa2node(data, taxa1)
... ...
@@ -58,3 +56,72 @@ StatTaxalink <- ggproto("StatTaxalink", Stat,
58 56
                         required_aes = c("x", "y", "xend", "yend")
59 57
                         )
60 58
 
59
+#' @importFrom ggplot2 GeomSegment
60
+#' @importFrom grid gTree curveGrob gpar
61
+GeomCurveLink <- ggproto("GeomCurveLink", GeomSegment,
62
+  default_aes = aes(colour = "black", size = 0.5, linetype = 1, alpha = NA, curvature=0.5),
63
+  draw_panel = function(data, panel_params, coord, angle = 90,
64
+                        ncp = 5, arrow = NULL, arrow.fill=NULL, lineend = "butt", na.rm = FALSE) {
65
+
66
+    if (!coord$is_linear()) {
67
+        tmpgroup <- data$group
68
+        starts <- subset(data, select = c(-xend, -yend))
69
+        starts$group <- 1
70
+        ends <- rename(subset(data, select = c(-x, -y)), c("x" = "xend", "y" = "yend"))
71
+        ends$group <- 2
72
+        pieces <- rbind(starts, ends)
73
+
74
+        trans <- coord$transform(pieces, panel_params)
75
+        starts <- trans[trans$group==1, ,drop=FALSE]
76
+        ends <- trans[trans$group==2, ,drop=FALSE]
77
+        curvature <- unlist(mapply(generate_curvature, starttheta=starts$theta, endtheta=ends$theta, SIMPLIFY=FALSE))
78
+        ends <- rename(subset(ends, select=c(x, y)), c("xend"="x", "yend"="y"))
79
+        trans <- cbind(starts, ends)
80
+        trans$group <- tmpgroup
81
+        trans$curvature <- curvature
82
+    }else{
83
+        trans <- coord$transform(data, panel_params)
84
+    }
85
+    arrow.fill <- arrow.fill %||% trans$colour
86
+
87
+    grobs <- lapply(seq_len(nrow(trans)), function(i){
88
+                        curveGrob(
89
+                              trans$x[i], trans$y[i], trans$xend[i], trans$yend[i],
90
+                              default.units = "native",
91
+                              curvature = trans$curvature[i], angle = angle, ncp = ncp,
92
+                              square = FALSE, squareShape = 1, inflect = FALSE, open = TRUE,
93
+                              gp = gpar(col = alpha(trans$colour[i], trans$alpha[i]),
94
+                                        fill = alpha(arrow.fill[i], trans$alpha[i]),
95
+                                        lwd = trans$size[i] * .pt,
96
+                                        lty = trans$linetype[i],
97
+                                        lineend = lineend),
98
+                                        arrow = arrow)})
99
+    class(grobs) <- "gList"
100
+    return(ggname("geom_curve_link", gTree(children=grobs)))
101
+  }
102
+)
103
+
104
+generate_curvature <- function(starttheta, endtheta){
105
+    flag <- endtheta - starttheta