update ggtree to compatible with the ggplot2 3.4.0
... | ... |
@@ -444,10 +444,12 @@ get_clade_position_ <- function(data, node, reverse=FALSE) { |
444 | 444 |
xmin <- min(sp.df$branch, na.rm=TRUE) |
445 | 445 |
xmax <- max(x, na.rm=TRUE) |
446 | 446 |
} |
447 |
+ |
|
448 |
+ w <- getOption("clade_width_extend", default = 0.5) |
|
447 | 449 |
data.frame(xmin=xmin, |
448 | 450 |
xmax=xmax, |
449 |
- ymin=min(y, na.rm=TRUE) - 0.5, |
|
450 |
- ymax=max(y, na.rm=TRUE) + 0.5) |
|
451 |
+ ymin=min(y, na.rm=TRUE) - w, |
|
452 |
+ ymax=max(y, na.rm=TRUE) + w) |
|
451 | 453 |
} |
452 | 454 |
|
453 | 455 |
build_align_data <- function(data, align){ |
... | ... |
@@ -38,7 +38,7 @@ |
38 | 38 |
#' \item \code{extend} extend xmax of the rectangle, defaults to 0. |
39 | 39 |
#' \item \code{extendto} specify a value, meaning the rectangle extend to, defaults to NULL. |
40 | 40 |
#' \item \code{linetype} the line type of margin, defaults to 1. |
41 |
-#' \item \code{size} the width of line of margin, defaults to 0.5. |
|
41 |
+#' \item \code{linewidth} the width of line of margin, defaults to 0.5. |
|
42 | 42 |
#' } |
43 | 43 |
#' \code{geom_hilight()} understands the following aesthethics for encircle layer (required |
44 | 44 |
#' aesthetics are in bold): |
... | ... |
@@ -49,7 +49,7 @@ |
49 | 49 |
#' \item \code{alpha} the transparency of fill, defaults to 0.5. |
50 | 50 |
#' \item \code{expand} expands the xspline clade region, defaults to 0. |
51 | 51 |
#' \item \code{spread} control the size, when only one point. |
52 |
-#' \item \code{size} the width of line of margin, defaults to 0.5. |
|
52 |
+#' \item \code{linewidth} the width of line of margin, defaults to 0.5. |
|
53 | 53 |
#' \item \code{linetype} the line type of margin, defaults to 1. |
54 | 54 |
#' \item \code{s_shape} the shape of the spline relative to the control points, defaults to 0.5. |
55 | 55 |
#' \item \code{s_open} whether the spline is a line or a closed shape, defaults to FALSE. |
... | ... |
@@ -125,10 +125,11 @@ geom_hilight_rect2 <- function(data=NULL, |
125 | 125 |
#' @importFrom grid rectGrob gpar grobTree |
126 | 126 |
GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
127 | 127 |
default_aes = aes(colour = NA, fill = "steelblue", |
128 |
- size = 0.5, linetype = 1, alpha = 0.5, |
|
128 |
+ linewidth = 0.5, linetype = 1, alpha = 0.5, |
|
129 | 129 |
extend=0, extendto=NULL), |
130 | 130 |
required_aes = c("xmin", "xmax", "ymin", "ymax", "clade_root_node"), |
131 | 131 |
draw_key = draw_key_polygon, |
132 |
+ rename_size = TRUE, |
|
132 | 133 |
draw_panel = function(self, data, panel_params, coord, |
133 | 134 |
linejoin = "mitre", align="none", |
134 | 135 |
gradient = FALSE, |
... | ... |
@@ -195,7 +196,7 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
195 | 196 |
#data <- data %>% dplyr::left_join(df, by="clade_root_node") |
196 | 197 |
polys <- lapply(split(data, seq_len(nrow(data))), function(row) { |
197 | 198 |
poly <- rect_to_poly(row$xmin, row$xmax, row$ymin, row$ymax) |
198 |
- aes <- new_data_frame(row[aesthetics])[rep(1,5), ] |
|
199 |
+ aes <- row[rep(1,5), aesthetics] |
|
199 | 200 |
#draw_panel_polar(data = cbind(poly, aes), |
200 | 201 |
# panel_params = panel_params, |
201 | 202 |
# coord = coord, |
... | ... |
@@ -203,7 +204,7 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
203 | 204 |
# gradient.direction = gradient.direction, |
204 | 205 |
# gradient.length.out = gradient.length.out |
205 | 206 |
# ) |
206 |
- GeomPolygon$draw_panel(cbind(poly, aes), panel_params, coord) |
|
207 |
+ GeomPolygon$draw_panel(vctrs::vec_cbind(poly, aes), panel_params, coord) |
|
207 | 208 |
}) |
208 | 209 |
ggname("geom_hilight_rect2", do.call("grobTree", polys)) |
209 | 210 |
}else{ |
... | ... |
@@ -235,7 +236,7 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
235 | 236 |
just = c("left", "top"), |
236 | 237 |
gp = gpar(col = row$colour, |
237 | 238 |
fill = fill, |
238 |
- lwd = row$size * ggplot2:::.pt, |
|
239 |
+ lwd = row$linewidth * ggplot2:::.pt, |
|
239 | 240 |
lty = row$linetype, |
240 | 241 |
linejoin = linejoin, |
241 | 242 |
lineend = if (identical(linejoin, "round")) "round" else "square") |
... | ... |
@@ -255,7 +256,7 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
255 | 256 |
gp = grid::gpar( |
256 | 257 |
col = row$colour, |
257 | 258 |
fill = alpha(row$fill, row$alpha), |
258 |
- lwd = row$size * ggplot2::.pt, |
|
259 |
+ lwd = row$linewidth * ggplot2::.pt, |
|
259 | 260 |
lty = row$linetype, |
260 | 261 |
lineend = "butt" |
261 | 262 |
) |
... | ... |
@@ -271,7 +272,7 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
271 | 272 |
just = c("left", "top"), |
272 | 273 |
gp = gpar(col = coords$colour, |
273 | 274 |
fill = alpha(coords$fill, coords$alpha), |
274 |
- lwd = coords$size * ggplot2:::.pt, |
|
275 |
+ lwd = coords$linewidth * ggplot2:::.pt, |
|
275 | 276 |
lty = coords$linetype, |
276 | 277 |
linejoin = linejoin, |
277 | 278 |
lineend = if (identical(linejoin, "round")) "round" else "square") |
... | ... |
@@ -306,9 +307,10 @@ geom_hilight_encircle2 <- function(data=NULL, |
306 | 307 |
GeomHilightEncircle <- ggproto("GeomHilightEncircle", Geom, |
307 | 308 |
required_aes = c("x", "y", "clade_root_node"), |
308 | 309 |
default_aes = aes(colour="black", fill="steelblue", alpha = 0.5, |
309 |
- expand=0, spread=0.1, linetype=1, size=0.5, |
|
310 |
+ expand=0, spread=0.1, linetype=1, linewidth = 0.5, |
|
310 | 311 |
s_shape=0.5, s_open=FALSE), |
311 | 312 |
draw_key = draw_key_polygon, |
313 |
+ rename_size = TRUE, |
|
312 | 314 |
draw_panel = function(data, panel_scales, coord){ |
313 | 315 |
globs <- lapply(split(data, data$clade_root_node), function(i) |
314 | 316 |
get_glob_encircle(i, panel_scales, coord)) |
... | ... |
@@ -475,9 +477,14 @@ build_align_data <- function(data, align){ |
475 | 477 |
|
476 | 478 |
|
477 | 479 |
#' @importFrom utils getFromNamespace |
478 |
-warning_wrap <- getFromNamespace("warning_wrap", "ggplot2") |
|
480 |
+#warning_wrap <- getFromNamespace("warning_wrap", "ggplot2") |
|
481 |
+warning_wrap <- function(...){ |
|
482 |
+ x = paste0(...) |
|
483 |
+ x = paste(strwrap(x), collapse = "\n") |
|
484 |
+ warning(x, call. = FALSE) |
|
485 |
+} |
|
479 | 486 |
rect_to_poly <- getFromNamespace("rect_to_poly", "ggplot2") |
480 |
-new_data_frame <- getFromNamespace("new_data_frame", "ggplot2") |
|
487 |
+#new_data_frame <- getFromNamespace("new_data_frame", "ggplot2") |
|
481 | 488 |
|
482 | 489 |
## ##' layer of hilight clade with rectangle |
483 | 490 |
## ##' |
... | ... |
@@ -13,6 +13,8 @@ |
13 | 13 |
#' unrooted and daylight layout tree use will use encircle layer. You can specify this parameter to |
14 | 14 |
#' `rect` (rectangular layer) or `encircle` (encircle layer), 'gradient' (gradient color), |
15 | 15 |
#' 'roundrect' (round rectangular layer). |
16 |
+#' @param to.bottom logical, whether set the high light layer to the bottom in all layers of 'ggtree' |
|
17 |
+#' object, default is FALSE. |
|
16 | 18 |
#' @param ... additional parameters, see also the below and Aesthetics section. |
17 | 19 |
#' \itemize{ |
18 | 20 |
#' \item \code{align} control the align direction of the edge of high light rectangular. |
... | ... |
@@ -82,12 +84,14 @@ geom_hilight <- function(data=NULL, |
82 | 84 |
mapping=NULL, |
83 | 85 |
node=NULL, |
84 | 86 |
type="auto", |
87 |
+ to.bottom=FALSE, |
|
85 | 88 |
...){ |
86 | 89 |
params <- list(...) |
87 | 90 |
structure(list(data = data, |
88 | 91 |
mapping = mapping, |
89 | 92 |
node = node, |
90 | 93 |
type = type, |
94 |
+ to.bottom = to.bottom, |
|
91 | 95 |
params = params), |
92 | 96 |
class = 'hilight') |
93 | 97 |
} |
... | ... |
@@ -5,10 +5,10 @@ |
5 | 5 |
#' |
6 | 6 |
#' @title geom_hilight |
7 | 7 |
#' @rdname geom-hilight |
8 |
-#' @param data data.frame, The data to be displayed in this layer, default is NULL. |
|
9 |
-#' @param mapping Set of aesthetic mappings, default is NULL. |
|
8 |
+#' @param data data.frame, The data to be displayed in this layer, defaults to NULL. |
|
9 |
+#' @param mapping Set of aesthetic mappings, defaults to NULL. |
|
10 | 10 |
#' @param node selected node to hilight, when data and mapping is NULL, it is required. |
11 |
-#' @param type the type of layer, default is `auto`, meaning rectangular, circular, |
|
11 |
+#' @param type the type of layer, defaults to `auto`, meaning rectangular, circular, |
|
12 | 12 |
#' slanted, fan, inward_circular, radial, equal_angle, ape layout tree will use rectangular layer, |
13 | 13 |
#' unrooted and daylight layout tree use will use encircle layer. You can specify this parameter to |
14 | 14 |
#' `rect` (rectangular layer) or `encircle` (encircle layer), 'gradient' (gradient color), |
... | ... |
@@ -18,39 +18,39 @@ |
18 | 18 |
#' \item \code{align} control the align direction of the edge of high light rectangular. |
19 | 19 |
#' Options is 'none' (default), 'left', 'right', 'both'. This argument only work when the |
20 | 20 |
#' 'geom_hilight' is plotting using geom_hilight(mapping=aes(...)). |
21 |
-#' \item \code{gradient.direction} character, the direction of gradient color, default is 'rt' |
|
21 |
+#' \item \code{gradient.direction} character, the direction of gradient color, defaults to 'rt' |
|
22 | 22 |
#' meaning the locations of gradient color is from root to tip, options are 'rt' and 'tr'. |
23 | 23 |
#' \item \code{gradient.length.out} integer, desired length of the sequence of gradient color, |
24 |
-#' default is 2. |
|
24 |
+#' defaults to 2. |
|
25 | 25 |
#' \item \code{roundrect.r} numeric, the radius of the rounded corners, when \code{roundrect=TRUE}, |
26 |
-#' default is 0.05. |
|
26 |
+#' defaults to 0.05. |
|
27 | 27 |
#' } |
28 | 28 |
#' @section Aesthetics: |
29 | 29 |
#' \code{geom_hilight()} understands the following aesthetics for rectangular layer (required |
30 | 30 |
#' aesthetics are in bold): |
31 | 31 |
#' \itemize{ |
32 | 32 |
#' \item \strong{\code{node}} selected node to hight light, it is required. |
33 |
-#' \item \code{colour} the colour of margin, default is NA. |
|
34 |
-#' \item \code{fill} the colour of fill, default is 'steelblue'. |
|
35 |
-#' \item \code{alpha} the transparency of fill, default is 0.5. |
|
36 |
-#' \item \code{extend} extend xmax of the rectangle, default is 0. |
|
37 |
-#' \item \code{extendto} specify a value, meaning the rectangle extend to, default is NULL. |
|
38 |
-#' \item \code{linetype} the line type of margin, default is 1. |
|
39 |
-#' \item \code{size} the width of line of margin, default is 0.5. |
|
33 |
+#' \item \code{colour} the colour of margin, defaults to NA. |
|
34 |
+#' \item \code{fill} the colour of fill, defaults to 'steelblue'. |
|
35 |
+#' \item \code{alpha} the transparency of fill, defaults to 0.5. |
|
36 |
+#' \item \code{extend} extend xmax of the rectangle, defaults to 0. |
|
37 |
+#' \item \code{extendto} specify a value, meaning the rectangle extend to, defaults to NULL. |
|
38 |
+#' \item \code{linetype} the line type of margin, defaults to 1. |
|
39 |
+#' \item \code{size} the width of line of margin, defaults to 0.5. |
|
40 | 40 |
#' } |
41 | 41 |
#' \code{geom_hilight()} understands the following aesthethics for encircle layer (required |
42 | 42 |
#' aesthetics are in bold): |
43 | 43 |
#' \itemize{ |
44 | 44 |
#' \item \strong{\code{node}} selected node to hight light, it is required. |
45 |
-#' \item \code{colour} the colour of margin, default is 'black'. |
|
46 |
-#' \item \code{fill} the colour of fill, default is 'steelblue'. |
|
47 |
-#' \item \code{alpha} the transparency of fill, default is 0.5. |
|
48 |
-#' \item \code{expand} expands the xspline clade region, default is 0. |
|
45 |
+#' \item \code{colour} the colour of margin, defaults to 'black'. |
|
46 |
+#' \item \code{fill} the colour of fill, defaults to 'steelblue'. |
|
47 |
+#' \item \code{alpha} the transparency of fill, defaults to 0.5. |
|
48 |
+#' \item \code{expand} expands the xspline clade region, defaults to 0. |
|
49 | 49 |
#' \item \code{spread} control the size, when only one point. |
50 |
-#' \item \code{size} the width of line of margin, default is 0.5. |
|
51 |
-#' \item \code{linetype} the line type of margin, default is 1. |
|
52 |
-#' \item \code{s_shape} the shape of the spline relative to the control points, default is 0.5. |
|
53 |
-#' \item \code{s_open} whether the spline is a line or a closed shape, default is FALSE. |
|
50 |
+#' \item \code{size} the width of line of margin, defaults to 0.5. |
|
51 |
+#' \item \code{linetype} the line type of margin, defaults to 1. |
|
52 |
+#' \item \code{s_shape} the shape of the spline relative to the control points, defaults to 0.5. |
|
53 |
+#' \item \code{s_open} whether the spline is a line or a closed shape, defaults to FALSE. |
|
54 | 54 |
#' } |
55 | 55 |
#' @return a list object. |
56 | 56 |
#' @author Guangchuang Yu and Shuangbin Xu |
... | ... |
@@ -74,6 +74,10 @@ |
74 | 74 |
#' # display the high light layer with round rectangular. |
75 | 75 |
#' p8 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "roundrect", alpha=0.68) |
76 | 76 |
#' p2/ p3/ p4/ p5 / p6/ p7/ p8 |
77 |
+#' @references |
|
78 |
+#' For more detailed demonstration, please refer to chapter 5.2.2 of |
|
79 |
+#' *Data Integration, Manipulation and Visualization of Phylogenetic Trees* |
|
80 |
+#' <http://yulab-smu.top/treedata-book/index.html> by Guangchuang Yu. |
|
77 | 81 |
geom_hilight <- function(data=NULL, |
78 | 82 |
mapping=NULL, |
79 | 83 |
node=NULL, |
... | ... |
@@ -18,15 +18,10 @@ |
18 | 18 |
#' \item \code{align} control the align direction of the edge of high light rectangular. |
19 | 19 |
#' Options is 'none' (default), 'left', 'right', 'both'. This argument only work when the |
20 | 20 |
#' 'geom_hilight' is plotting using geom_hilight(mapping=aes(...)). |
21 |
-#' \item \code{gradient} logical, whether to set the color of hight light layer to gradient, |
|
22 |
-#' default is FALSE, it only work for rectangular, ellipse, roundrect layouts. |
|
23 | 21 |
#' \item \code{gradient.direction} character, the direction of gradient color, default is 'rt' |
24 | 22 |
#' meaning the locations of gradient color is from root to tip, options are 'rt' and 'tr'. |
25 | 23 |
#' \item \code{gradient.length.out} integer, desired length of the sequence of gradient color, |
26 | 24 |
#' default is 2. |
27 |
-#' \item \code{roundrect} logical, whether to use the round rectangular layer, default is FALSE, |
|
28 |
-#' it can not be used with \code{gradient=TRUE}, and it only work for rectangular, ellipse, |
|
29 |
-#' roundrect layouts |
|
30 | 25 |
#' \item \code{roundrect.r} numeric, the radius of the rounded corners, when \code{roundrect=TRUE}, |
31 | 26 |
#' default is 0.05. |
32 | 27 |
#' } |
... | ... |
@@ -73,9 +68,11 @@ |
73 | 68 |
#' p3 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="left") |
74 | 69 |
#' p4 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="right") |
75 | 70 |
#' p5 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="both") |
76 |
-#' p6 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), gradient = TRUE, alpha=0.68) |
|
77 |
-#' p7 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), gradient = TRUE, gradient.direction="tr", alpha=0.68) |
|
78 |
-#' p8 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), roundrect = TRUE, alpha=0.68) |
|
71 |
+#' # display the high light layer with gradiental color rectangular. |
|
72 |
+#' p6 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "gradient", alpha=0.68) |
|
73 |
+#' p7 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "gradient", gradient.direction="tr", alpha=0.68) |
|
74 |
+#' # display the high light layer with round rectangular. |
|
75 |
+#' p8 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), type = "roundrect", alpha=0.68) |
|
79 | 76 |
#' p2/ p3/ p4/ p5 / p6/ p7/ p8 |
80 | 77 |
geom_hilight <- function(data=NULL, |
81 | 78 |
mapping=NULL, |
... | ... |
@@ -11,20 +11,10 @@ |
11 | 11 |
#' @param type the type of layer, default is `auto`, meaning rectangular, circular, |
12 | 12 |
#' slanted, fan, inward_circular, radial, equal_angle, ape layout tree will use rectangular layer, |
13 | 13 |
#' unrooted and daylight layout tree use will use encircle layer. You can specify this parameter to |
14 |
-#' `rect` (rectangular layer) or `encircle` (encircle layer). |
|
15 |
-#' @param ... additional parameters, see also Aesthetics section. |
|
16 |
-#' @section Aesthetics: |
|
17 |
-#' \code{geom_hilight()} understands the following aesthetics for rectangular layer (required |
|
18 |
-#' aesthetics are in bold): |
|
14 |
+#' `rect` (rectangular layer) or `encircle` (encircle layer), 'gradient' (gradient color), |
|
15 |
+#' 'roundrect' (round rectangular layer). |
|
16 |
+#' @param ... additional parameters, see also the below and Aesthetics section. |
|
19 | 17 |
#' \itemize{ |
20 |
-#' \item \strong{\code{node}} selected node to hight light, it is required. |
|
21 |
-#' \item \code{colour} the colour of margin, default is NA. |
|
22 |
-#' \item \code{fill} the colour of fill, default is 'steelblue'. |
|
23 |
-#' \item \code{alpha} the transparency of fill, default is 0.5. |
|
24 |
-#' \item \code{extend} extend xmax of the rectangle, default is 0. |
|
25 |
-#' \item \code{extendto} specify a value, meaning the rectangle extend to, default is NULL. |
|
26 |
-#' \item \code{linetype} the line type of margin, default is 1. |
|
27 |
-#' \item \code{size} the width of line of margin, default is 0.5. |
|
28 | 18 |
#' \item \code{align} control the align direction of the edge of high light rectangular. |
29 | 19 |
#' Options is 'none' (default), 'left', 'right', 'both'. This argument only work when the |
30 | 20 |
#' 'geom_hilight' is plotting using geom_hilight(mapping=aes(...)). |
... | ... |
@@ -35,11 +25,24 @@ |
35 | 25 |
#' \item \code{gradient.length.out} integer, desired length of the sequence of gradient color, |
36 | 26 |
#' default is 2. |
37 | 27 |
#' \item \code{roundrect} logical, whether to use the round rectangular layer, default is FALSE, |
38 |
-#' it can not be used with \code{gradient=TRUE}, and it only work for rectangular, ellipse, |
|
28 |
+#' it can not be used with \code{gradient=TRUE}, and it only work for rectangular, ellipse, |
|
39 | 29 |
#' roundrect layouts |
40 | 30 |
#' \item \code{roundrect.r} numeric, the radius of the rounded corners, when \code{roundrect=TRUE}, |
41 | 31 |
#' default is 0.05. |
42 | 32 |
#' } |
33 |
+#' @section Aesthetics: |
|
34 |
+#' \code{geom_hilight()} understands the following aesthetics for rectangular layer (required |
|
35 |
+#' aesthetics are in bold): |
|
36 |
+#' \itemize{ |
|
37 |
+#' \item \strong{\code{node}} selected node to hight light, it is required. |
|
38 |
+#' \item \code{colour} the colour of margin, default is NA. |
|
39 |
+#' \item \code{fill} the colour of fill, default is 'steelblue'. |
|
40 |
+#' \item \code{alpha} the transparency of fill, default is 0.5. |
|
41 |
+#' \item \code{extend} extend xmax of the rectangle, default is 0. |
|
42 |
+#' \item \code{extendto} specify a value, meaning the rectangle extend to, default is NULL. |
|
43 |
+#' \item \code{linetype} the line type of margin, default is 1. |
|
44 |
+#' \item \code{size} the width of line of margin, default is 0.5. |
|
45 |
+#' } |
|
43 | 46 |
#' \code{geom_hilight()} understands the following aesthethics for encircle layer (required |
44 | 47 |
#' aesthetics are in bold): |
45 | 48 |
#' \itemize{ |
... | ... |
@@ -28,6 +28,17 @@ |
28 | 28 |
#' \item \code{align} control the align direction of the edge of high light rectangular. |
29 | 29 |
#' Options is 'none' (default), 'left', 'right', 'both'. This argument only work when the |
30 | 30 |
#' 'geom_hilight' is plotting using geom_hilight(mapping=aes(...)). |
31 |
+#' \item \code{gradient} logical, whether to set the color of hight light layer to gradient, |
|
32 |
+#' default is FALSE, it only work for rectangular, ellipse, roundrect layouts. |
|
33 |
+#' \item \code{gradient.direction} character, the direction of gradient color, default is 'rt' |
|
34 |
+#' meaning the locations of gradient color is from root to tip, options are 'rt' and 'tr'. |
|
35 |
+#' \item \code{gradient.length.out} integer, desired length of the sequence of gradient color, |
|
36 |
+#' default is 2. |
|
37 |
+#' \item \code{roundrect} logical, whether to use the round rectangular layer, default is FALSE, |
|
38 |
+#' it can not be used with \code{gradient=TRUE}, and it only work for rectangular, ellipse, |
|
39 |
+#' roundrect layouts |
|
40 |
+#' \item \code{roundrect.r} numeric, the radius of the rounded corners, when \code{roundrect=TRUE}, |
|
41 |
+#' default is 0.05. |
|
31 | 42 |
#' } |
32 | 43 |
#' \code{geom_hilight()} understands the following aesthethics for encircle layer (required |
33 | 44 |
#' aesthetics are in bold): |
... | ... |
@@ -59,7 +70,10 @@ |
59 | 70 |
#' p3 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="left") |
60 | 71 |
#' p4 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="right") |
61 | 72 |
#' p5 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), align="both") |
62 |
-#' p2/ p3/ p4/ p5 |
|
73 |
+#' p6 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), gradient = TRUE, alpha=0.68) |
|
74 |
+#' p7 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), gradient = TRUE, gradient.direction="tr", alpha=0.68) |
|
75 |
+#' p8 <- p + geom_hilight(data=dat, mapping=aes(node=id, fill=type), roundrect = TRUE, alpha=0.68) |
|
76 |
+#' p2/ p3/ p4/ p5 / p6/ p7/ p8 |
|
63 | 77 |
geom_hilight <- function(data=NULL, |
64 | 78 |
mapping=NULL, |
65 | 79 |
node=NULL, |
... | ... |
@@ -111,7 +125,9 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
111 | 125 |
linejoin = "mitre", align="none", |
112 | 126 |
gradient = FALSE, |
113 | 127 |
gradient.direction = "rt", |
114 |
- gradient.length.out = 2 |
|
128 |
+ gradient.length.out = 2, |
|
129 |
+ roundrect = FALSE, |
|
130 |
+ roundrect.r = 0.05 |
|
115 | 131 |
){ |
116 | 132 |
data$xmax <- data$xmax + data$extend |
117 | 133 |
if (!any(is.null(data$extendto)) && !any(is.na(data$extendto))){ |
... | ... |
@@ -140,45 +156,55 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
140 | 156 |
} |
141 | 157 |
data <- build_align_data(data=data, align=align) |
142 | 158 |
if (!coord$is_linear()) { |
143 |
- aesthetics <- setdiff(c(names(data), "x.start", "y.start", "x.stop", "y.stop"), |
|
159 |
+ if (gradient){ |
|
160 |
+ warning_wrap("The gradient color hight light layer only presents in rectangular, ellipse, roundrect layouts") |
|
161 |
+ } |
|
162 |
+ if (roundrect){ |
|
163 |
+ warning_wrap("The round rectangular hight light layer only presents in rectangular, ellipse, roundrect layouts") |
|
164 |
+ } |
|
165 |
+ aesthetics <- setdiff(colnames(data), #"x.start", "y.start", "x.stop", "y.stop"), |
|
144 | 166 |
c("xmin", "xmax", "ymin", "ymax", "clade_root_node")) |
145 |
- df.start <- lapply(split(data, data$clade_root_node), function(node){ |
|
146 |
- dplyr::mutate(node, x=.data$xmin, y=(.data$ymax-.data$ymin)/2) |
|
147 |
- }) %>% |
|
148 |
- dplyr::bind_rows() %>% |
|
149 |
- dplyr::select(!c("xmin", "xmax", "ymin", "ymax")) |
|
150 |
- df.stop <- lapply(split(data, data$clade_root_node), function(node){ |
|
151 |
- dplyr::mutate(node, x=.data$xmax, y=(.data$ymax-.data$ymin)/2) |
|
152 |
- }) %>% |
|
153 |
- dplyr::bind_rows() %>% |
|
154 |
- dplyr::select(!c("xmin", "xmax", "ymin", "ymax")) |
|
155 |
- |
|
156 |
- df.start <- ggplot2::coord_munch(coord, data = df.start, panel_params) %>% |
|
157 |
- dplyr::select(c("x", "y", "clade_root_node")) %>% |
|
158 |
- dplyr::rename(x.start="x", y.start="y") |
|
167 |
+ #df.start <- lapply(split(data, data$clade_root_node), function(node){ |
|
168 |
+ # dplyr::mutate(node, x=.data$xmin, y=(.data$ymax-.data$ymin)/2) |
|
169 |
+ # }) %>% |
|
170 |
+ # dplyr::bind_rows() %>% |
|
171 |
+ # dplyr::select(!c("xmin", "xmax", "ymin", "ymax")) |
|
172 |
+ #df.stop <- lapply(split(data, data$clade_root_node), function(node){ |
|
173 |
+ # dplyr::mutate(node, x=.data$xmax, y=(.data$ymax-.data$ymin)/2) |
|
174 |
+ # }) %>% |
|
175 |
+ # dplyr::bind_rows() %>% |
|
176 |
+ # dplyr::select(!c("xmin", "xmax", "ymin", "ymax")) |
|
177 |
+ # |
|
178 |
+ #df.start <- ggplot2::coord_munch(coord, data = df.start, panel_params) %>% |
|
179 |
+ # dplyr::select(c("x", "y", "clade_root_node")) %>% |
|
180 |
+ # dplyr::rename(x.start="x", y.start="y") |
|
159 | 181 |
|
160 |
- df.stop <- ggplot2::coord_munch(coord, data = df.stop, panel_params) %>% |
|
161 |
- dplyr::select(c("x", "y", "clade_root_node")) %>% |
|
162 |
- dplyr::rename(x.stop="x", y.stop="y") |
|
182 |
+ #df.stop <- ggplot2::coord_munch(coord, data = df.stop, panel_params) %>% |
|
183 |
+ # dplyr::select(c("x", "y", "clade_root_node")) %>% |
|
184 |
+ # dplyr::rename(x.stop="x", y.stop="y") |
|
163 | 185 |
|
164 |
- df <- df.start %>% left_join(df.stop, by="clade_root_node") |
|
165 |
- data <- data %>% dplyr::left_join(df, by="clade_root_node") |
|
166 |
- print(data) |
|
186 |
+ #df <- df.start %>% left_join(df.stop, by="clade_root_node") |
|
187 |
+ #data <- data %>% dplyr::left_join(df, by="clade_root_node") |
|
167 | 188 |
polys <- lapply(split(data, seq_len(nrow(data))), function(row) { |
168 | 189 |
poly <- rect_to_poly(row$xmin, row$xmax, row$ymin, row$ymax) |
169 | 190 |
aes <- new_data_frame(row[aesthetics])[rep(1,5), ] |
170 |
- draw_panel_polar(data = cbind(poly, aes), |
|
171 |
- panel_params = panel_params, |
|
172 |
- coord = coord, |
|
173 |
- gradient = gradient, |
|
174 |
- gradient.direction = gradient.direction, |
|
175 |
- gradient.length.out = gradient.length.out |
|
176 |
- ) |
|
191 |
+ #draw_panel_polar(data = cbind(poly, aes), |
|
192 |
+ # panel_params = panel_params, |
|
193 |
+ # coord = coord, |
|
194 |
+ # gradient = gradient, |
|
195 |
+ # gradient.direction = gradient.direction, |
|
196 |
+ # gradient.length.out = gradient.length.out |
|
197 |
+ # ) |
|
198 |
+ GeomPolygon$draw_panel(cbind(poly, aes), panel_params, coord) |
|
177 | 199 |
}) |
178 | 200 |
ggname("geom_hilight_rect2", do.call("grobTree", polys)) |
179 | 201 |
}else{ |
180 | 202 |
coords <- coord$transform(data, panel_params) |
203 |
+ hilightGrob <- ifelse(roundrect, grid::roundrectGrob, grid::rectGrob) |
|
181 | 204 |
if (gradient){ |
205 |
+ if (roundrect){ |
|
206 |
+ warning_wrap("The round rectangular and gradient are not applied simultaneously") |
|
207 |
+ } |
|
182 | 208 |
gradient.direction <- match.arg(gradient.direction, c("rt", "tr")) |
183 | 209 |
rects <- lapply(split(coords, seq_len(nrow(coords))), function(row){ |
184 | 210 |
fill <- grid::linearGradient( |
... | ... |
@@ -194,7 +220,9 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
194 | 220 |
stops = seq(0, 1, length.out = gradient.length.out) |
195 | 221 |
) |
196 | 222 |
rectGrob( |
197 |
- row$xmin, row$ymax, width = row$xmax - row$xmin, height = row$ymax - row$ymin, |
|
223 |
+ x = row$xmin, y = row$ymax, |
|
224 |
+ width = row$xmax - row$xmin, |
|
225 |
+ height = row$ymax - row$ymin, |
|
198 | 226 |
default.units = "native", |
199 | 227 |
just = c("left", "top"), |
200 | 228 |
gp = gpar(col = row$colour, |
... | ... |
@@ -207,19 +235,40 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
207 | 235 |
}) |
208 | 236 |
ggname("geom_hilight_rect2", do.call("grobTree", rects)) |
209 | 237 |
}else{ |
210 |
- ggname("geom_hilight_rect2", rectGrob( |
|
211 |
- coords$xmin, coords$ymax, |
|
212 |
- width = coords$xmax - coords$xmin, |
|
213 |
- height = coords$ymax - coords$ymin, |
|
214 |
- default.units = "native", |
|
215 |
- just = c("left", "top"), |
|
216 |
- gp = gpar(col = coords$colour, |
|
217 |
- fill = alpha(coords$fill, coords$alpha), |
|
218 |
- lwd = coords$size * ggplot2:::.pt, |
|
219 |
- lty = coords$linetype, |
|
220 |
- linejoin = linejoin, |
|
221 |
- lineend = if (identical(linejoin, "round")) "round" else "square") |
|
222 |
- )) |
|
238 |
+ if (roundrect){ |
|
239 |
+ rects <- lapply(split(coords, seq_len(nrow(coords))), function(row) |
|
240 |
+ grid::roundrectGrob( |
|
241 |
+ row$xmin, row$ymax, |
|
242 |
+ width = row$xmax - row$xmin, |
|
243 |
+ height = row$ymax - row$ymin, |
|
244 |
+ r = grid::unit(roundrect.r, "snpc"), |
|
245 |
+ default.units = "native", |
|
246 |
+ just = c("left", "top"), |
|
247 |
+ gp = grid::gpar( |
|
248 |
+ col = row$colour, |
|
249 |
+ fill = alpha(row$fill, row$alpha), |
|
250 |
+ lwd = row$size * ggplot2::.pt, |
|
251 |
+ lty = row$linetype, |
|
252 |
+ lineend = "butt" |
|
253 |
+ ) |
|
254 |
+ ) |
|
255 |
+ ) |
|
256 |
+ ggname("geom_hilight_rect2", do.call("grobTree", rects)) |
|
257 |
+ }else{ |
|
258 |
+ ggname("geom_hilight_rect2", rectGrob( |
|
259 |
+ coords$xmin, coords$ymax, |
|
260 |
+ width = coords$xmax - coords$xmin, |
|
261 |
+ height = coords$ymax - coords$ymin, |
|
262 |
+ default.units = "native", |
|
263 |
+ just = c("left", "top"), |
|
264 |
+ gp = gpar(col = coords$colour, |
|
265 |
+ fill = alpha(coords$fill, coords$alpha), |
|
266 |
+ lwd = coords$size * ggplot2:::.pt, |
|
267 |
+ lty = coords$linetype, |
|
268 |
+ linejoin = linejoin, |
|
269 |
+ lineend = if (identical(linejoin, "round")) "round" else "square") |
|
270 |
+ )) |
|
271 |
+ } |
|
223 | 272 |
} |
224 | 273 |
} |
225 | 274 |
} |
... | ... |
@@ -261,92 +310,92 @@ GeomHilightEncircle <- ggproto("GeomHilightEncircle", Geom, |
261 | 310 |
) |
262 | 311 |
|
263 | 312 |
|
264 |
-draw_panel_polar <- function (data, panel_params, coord, rule = "evenodd", gradient, gradient.direction, gradient.length.out){ |
|
265 |
- n <- nrow(data) |
|
266 |
- if (n == 1) |
|
267 |
- return(zeroGrob()) |
|
268 |
- munched <- ggplot2::coord_munch(coord, data, panel_params) |
|
269 |
- if (is.null(munched$subgroup)) { |
|
270 |
- munched <- munched[order(munched$group), ] |
|
271 |
- first_idx <- !duplicated(munched$group) |
|
272 |
- first_rows <- munched[first_idx, ] |
|
273 |
- if (gradient){ |
|
274 |
- gradient.direction <- match.arg(gradient.direction, c("rt", "tr")) |
|
275 |
- rects <- lapply(split(munched, munched$group), function(row){ |
|
276 |
- fill <- grid::linearGradient( |
|
277 |
- x1 = unique(row$x.start), |
|
278 |
- x2 = unique(row$x.stop), |
|
279 |
- y1 = unique(row$y.start), |
|
280 |
- y2 = unique(row$y.stop), |
|
281 |
- default.units = "native", |
|
282 |
- colours = if (gradient.direction=="rt"){ |
|
283 |
- alpha(c(first_rows$fill, "white"), first_rows$alpha) |
|
284 |
- }else{ |
|
285 |
- rev(alpha(c(first_rows$fill, "white"), first_rows$alpha)) |
|
286 |
- }, |
|
287 |
- stops = seq(0, 1, length.out = gradient.length.out) |
|
288 |
- ) |
|
289 |
- grid::polygonGrob( |
|
290 |
- row$x, row$y, id = row$group, |
|
291 |
- default.units = "native", |
|
292 |
- gp = gpar(col = first_rows$colour, |
|
293 |
- fill = fill, |
|
294 |
- lwd = first_rows$size * ggplot2:::.pt, |
|
295 |
- lty = first_rows$linetype) |
|
296 |
- ) |
|
297 |
- }) |
|
298 |
- ggname("geom_polygon2", do.call("grobTree", rects)) |
|
299 |
- }else{ |
|
300 |
- ggname("geom_polygon2", grid::polygonGrob(munched$x, munched$y, |
|
301 |
- default.units = "native", id = munched$group, gp = gpar(col = first_rows$colour, |
|
302 |
- fill = alpha(first_rows$fill, first_rows$alpha), |
|
303 |
- lwd = first_rows$size * ggplot2::.pt, lty = first_rows$linetype))) |
|
304 |
- } |
|
305 |
- } |
|
306 |
- else { |
|
307 |
- if (utils::packageVersion("grid") < "3.6") { |
|
308 |
- abort("Polygons with holes requires R 3.6 or above") |
|
309 |
- } |
|
310 |
- munched <- munched[order(munched$group, munched$subgroup), ] |
|
311 |
- id <- match(munched$subgroup, unique(munched$subgroup)) |
|
312 |
- first_idx <- !duplicated(munched$group) |
|
313 |
- first_rows <- munched[first_idx, ] |
|
314 |
- if (gradient){ |
|
315 |
- gradient.direction <- match.arg(gradient.direction, c("rt", "tr")) |
|
316 |
- rects <- lapply(split(munched, munched$group), function(row){ |
|
317 |
- fill <- grid::linearGradient( |
|
318 |
- x1 = first_rows$x.start, |
|
319 |
- x2 = first_rows$x.stop, |
|
320 |
- y1 = first_rows$y.start, |
|
321 |
- y2 = first_rows$y.stop, |
|
322 |
- default.units = "native", |
|
323 |
- colours = if (gradient.direction=="rt"){ |
|
324 |
- alpha(c(first_rows$fill, "white"), first_rows$alpha) |
|
325 |
- }else{ |
|
326 |
- rev(alpha(c(first_rows$fill, "white"), first_rows$alpha)) |
|
327 |
- }, |
|
328 |
- stops = seq(0, 1, length.out = gradient.length.out) |
|
329 |
- ) |
|
330 |
- grid::pathGrob( |
|
331 |
- row$x, row$y, id = match(row$subgroup, unique(row$group)), |
|
332 |
- pathId = munched$group, |
|
333 |
- rule = rule, default.units = "native", |
|
334 |
- gp = gpar(col = first_rows$colour, |
|
335 |
- fill = fill, |
|
336 |
- lwd = first_rows$size * ggplot2:::.pt, |
|
337 |
- lty = first_rows$linetype) |
|
338 |
- ) |
|
339 |
- }) |
|
340 |
- ggname("geom_polygon2", do.call("grobTree", rects)) |
|
341 |
- }else{ |
|
342 |
- ggname("geom_polygon2", grid::pathGrob(munched$x, munched$y, |
|
343 |
- default.units = "native", id = id, pathId = munched$group, |
|
344 |
- rule = rule, gp = gpar(col = first_rows$colour, fill = alpha(first_rows$fill, |
|
345 |
- first_rows$alpha), lwd = first_rows$size * ggplot2::.pt, |
|
346 |
- lty = first_rows$linetype))) |
|
347 |
- } |
|
348 |
- } |
|
349 |
-} |
|
313 |
+#draw_panel_polar <- function (data, panel_params, coord, rule = "evenodd", gradient, gradient.direction, gradient.length.out){ |
|
314 |
+# n <- nrow(data) |
|
315 |
+# if (n == 1) |
|
316 |
+# return(zeroGrob()) |
|
317 |
+# munched <- ggplot2::coord_munch(coord, data, panel_params) |
|
318 |
+# if (is.null(munched$subgroup)) { |
|
319 |
+# munched <- munched[order(munched$group), ] |
|
320 |
+# first_idx <- !duplicated(munched$group) |
|
321 |
+# first_rows <- munched[first_idx, ] |
|
322 |
+# if (gradient){ |
|
323 |
+# gradient.direction <- match.arg(gradient.direction, c("rt", "tr")) |
|
324 |
+# rects <- lapply(split(munched, munched$group), function(row){ |
|
325 |
+# fill <- grid::linearGradient( |
|
326 |
+# x1 = unique(row$x.start), |
|
327 |
+# x2 = unique(row$x.stop), |
|
328 |
+# y1 = 0, |
|
329 |
+# y2 = 1, |
|
330 |
+# default.units = "native", |
|
331 |
+# colours = if (gradient.direction=="rt"){ |
|
332 |
+# alpha(c(first_rows$fill, "white"), first_rows$alpha) |
|
333 |
+# }else{ |
|
334 |
+# rev(alpha(c(first_rows$fill, "white"), first_rows$alpha)) |
|
335 |
+# }, |
|
336 |
+# stops = seq(0, 1, length.out = gradient.length.out) |
|
337 |
+# ) |
|
338 |
+# grid::polygonGrob( |
|
339 |
+# row$x, row$y, id = row$group, |
|
340 |
+# default.units = "native", |
|
341 |
+# gp = gpar(col = first_rows$colour, |
|
342 |
+# fill = fill, |
|
343 |
+# lwd = first_rows$size * ggplot2:::.pt, |
|
344 |
+# lty = first_rows$linetype) |
|
345 |
+# ) |
|
346 |
+# }) |
|
347 |
+# ggname("geom_polygon2", do.call("grobTree", rects)) |
|
348 |
+# }else{ |
|
349 |
+# ggname("geom_polygon2", grid::polygonGrob(munched$x, munched$y, |
|
350 |
+# default.units = "native", id = munched$group, gp = gpar(col = first_rows$colour, |
|
351 |
+# fill = alpha(first_rows$fill, first_rows$alpha), |
|
352 |
+# lwd = first_rows$size * ggplot2::.pt, lty = first_rows$linetype))) |
|
353 |
+# } |
|
354 |
+# } |
|
355 |
+# else { |
|
356 |
+# if (utils::packageVersion("grid") < "3.6") { |
|
357 |
+# abort("Polygons with holes requires R 3.6 or above") |
|
358 |
+# } |
|
359 |
+# munched <- munched[order(munched$group, munched$subgroup), ] |
|
360 |
+# id <- match(munched$subgroup, unique(munched$subgroup)) |
|
361 |
+# first_idx <- !duplicated(munched$group) |
|
362 |
+# first_rows <- munched[first_idx, ] |
|
363 |
+# if (gradient){ |
|
364 |
+# gradient.direction <- match.arg(gradient.direction, c("rt", "tr")) |
|
365 |
+# rects <- lapply(split(munched, munched$group), function(row){ |
|
366 |
+# fill <- grid::linearGradient( |
|
367 |
+# x1 = first_rows$x.start, |
|
368 |
+# x2 = first_rows$x.stop, |
|
369 |
+# y1 = first_rows$y.start, |
|
370 |
+# y2 = first_rows$y.stop, |
|
371 |
+# default.units = "native", |
|
372 |
+# colours = if (gradient.direction=="rt"){ |
|
373 |
+# alpha(c(first_rows$fill, "white"), first_rows$alpha) |
|
374 |
+# }else{ |
|
375 |
+# rev(alpha(c(first_rows$fill, "white"), first_rows$alpha)) |
|
376 |
+# }, |
|
377 |
+# stops = seq(0, 1, length.out = gradient.length.out) |
|
378 |
+# ) |
|
379 |
+# grid::pathGrob( |
|
380 |
+# row$x, row$y, id = match(row$subgroup, unique(row$group)), |
|
381 |
+# pathId = munched$group, |
|
382 |
+# rule = rule, default.units = "native", |
|
383 |
+# gp = gpar(col = first_rows$colour, |
|
384 |
+# fill = fill, |
|
385 |
+# lwd = first_rows$size * ggplot2:::.pt, |
|
386 |
+# lty = first_rows$linetype) |
|
387 |
+# ) |
|
388 |
+# }) |
|
389 |
+# ggname("geom_polygon2", do.call("grobTree", rects)) |
|
390 |
+# }else{ |
|
391 |
+# ggname("geom_polygon2", grid::pathGrob(munched$x, munched$y, |
|
392 |
+# default.units = "native", id = id, pathId = munched$group, |
|
393 |
+# rule = rule, gp = gpar(col = first_rows$colour, fill = alpha(first_rows$fill, |
|
394 |
+# first_rows$alpha), lwd = first_rows$size * ggplot2::.pt, |
|
395 |
+# lty = first_rows$linetype))) |
|
396 |
+# } |
|
397 |
+# } |
|
398 |
+#} |
|
350 | 399 |
|
351 | 400 |
##' get position of clade (xmin, xmax, ymin, ymax) |
352 | 401 |
##' |
... | ... |
@@ -107,7 +107,12 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
107 | 107 |
extend=0, extendto=NULL), |
108 | 108 |
required_aes = c("xmin", "xmax", "ymin", "ymax", "clade_root_node"), |
109 | 109 |
draw_key = draw_key_polygon, |
110 |
- draw_panel = function(self, data, panel_params, coord, linejoin = "mitre", align="none") { |
|
110 |
+ draw_panel = function(self, data, panel_params, coord, |
|
111 |
+ linejoin = "mitre", align="none", |
|
112 |
+ gradient = FALSE, |
|
113 |
+ gradient.direction = "rt", |
|
114 |
+ gradient.length.out = 2 |
|
115 |
+ ){ |
|
111 | 116 |
data$xmax <- data$xmax + data$extend |
112 | 117 |
if (!any(is.null(data$extendto)) && !any(is.na(data$extendto))){ |
113 | 118 |
# check whether the x of tree is reversed. |
... | ... |
@@ -135,28 +140,87 @@ GeomHilightRect <- ggproto("GeomHilightRect", Geom, |
135 | 140 |
} |
136 | 141 |
data <- build_align_data(data=data, align=align) |
137 | 142 |
if (!coord$is_linear()) { |
138 |
- aesthetics <- setdiff(names(data), c("xmin", "xmax", "ymin", "ymax", "clade_root_node")) |
|
143 |
+ aesthetics <- setdiff(c(names(data), "x.start", "y.start", "x.stop", "y.stop"), |
|
144 |
+ c("xmin", "xmax", "ymin", "ymax", "clade_root_node")) |
|
145 |
+ df.start <- lapply(split(data, data$clade_root_node), function(node){ |
|
146 |
+ dplyr::mutate(node, x=.data$xmin, y=(.data$ymax-.data$ymin)/2) |
|
147 |
+ }) %>% |
|
148 |
+ dplyr::bind_rows() %>% |
|
149 |
+ dplyr::select(!c("xmin", "xmax", "ymin", "ymax")) |
|
150 |
+ df.stop <- lapply(split(data, data$clade_root_node), function(node){ |
|
151 |
+ dplyr::mutate(node, x=.data$xmax, y=(.data$ymax-.data$ymin)/2) |
|
152 |
+ }) %>% |
|
153 |
+ dplyr::bind_rows() %>% |
|
154 |
+ dplyr::select(!c("xmin", "xmax", "ymin", "ymax")) |
|
155 |
+ |
|
156 |
+ df.start <- ggplot2::coord_munch(coord, data = df.start, panel_params) %>% |
|
157 |
+ dplyr::select(c("x", "y", "clade_root_node")) %>% |
|
158 |
+ dplyr::rename(x.start="x", y.start="y") |
|
159 |
+ |
|
160 |
+ df.stop <- ggplot2::coord_munch(coord, data = df.stop, panel_params) %>% |
|
161 |
+ dplyr::select(c("x", "y", "clade_root_node")) %>% |
|
162 |
+ dplyr::rename(x.stop="x", y.stop="y") |
|
163 |
+ |
|
164 |
+ df <- df.start %>% left_join(df.stop, by="clade_root_node") |
|
165 |
+ data <- data %>% dplyr::left_join(df, by="clade_root_node") |
|
166 |
+ print(data) |
|
139 | 167 |
polys <- lapply(split(data, seq_len(nrow(data))), function(row) { |
140 | 168 |
poly <- rect_to_poly(row$xmin, row$xmax, row$ymin, row$ymax) |
141 | 169 |
aes <- new_data_frame(row[aesthetics])[rep(1,5), ] |
142 |
- GeomPolygon$draw_panel(cbind(poly, aes), panel_params, coord) |
|
170 |
+ draw_panel_polar(data = cbind(poly, aes), |
|
171 |
+ panel_params = panel_params, |
|
172 |
+ coord = coord, |
|
173 |
+ gradient = gradient, |
|
174 |
+ gradient.direction = gradient.direction, |
|
175 |
+ gradient.length.out = gradient.length.out |
|
176 |
+ ) |
|
143 | 177 |
}) |
144 |
- ggname("bar", do.call("grobTree", polys)) |
|
178 |
+ ggname("geom_hilight_rect2", do.call("grobTree", polys)) |
|
145 | 179 |
}else{ |
146 | 180 |
coords <- coord$transform(data, panel_params) |
147 |
- ggname("geom_hilight_rect2", rectGrob( |
|
148 |
- coords$xmin, coords$ymax, |
|
149 |
- width = coords$xmax - coords$xmin, |
|
150 |
- height = coords$ymax - coords$ymin, |
|
151 |
- default.units = "native", |
|
152 |
- just = c("left", "top"), |
|
153 |
- gp = gpar(col = coords$colour, |
|
154 |
- fill = alpha(coords$fill, coords$alpha), |
|
155 |
- lwd = coords$size * ggplot2:::.pt, |
|
156 |
- lty = coords$linetype, |
|
157 |
- linejoin = linejoin, |
|
158 |
- lineend = if (identical(linejoin, "round")) "round" else "square") |
|
181 |
+ if (gradient){ |
|
182 |
+ gradient.direction <- match.arg(gradient.direction, c("rt", "tr")) |
|
183 |
+ rects <- lapply(split(coords, seq_len(nrow(coords))), function(row){ |
|
184 |
+ fill <- grid::linearGradient( |
|
185 |
+ x1 = 0, |
|
186 |
+ x2 = 1, |
|
187 |
+ y1 = 0.5, |
|
188 |
+ y2 = 0.5, |
|
189 |
+ colours = if(gradient.direction == "rt"){ |
|
190 |
+ alpha(c(row$fill, "white"), row$alpha) |
|
191 |
+ }else{ |
|
192 |
+ rev(alpha(c(row$fill, "white"), row$alpha)) |
|
193 |
+ }, |
|
194 |
+ stops = seq(0, 1, length.out = gradient.length.out) |
|
195 |
+ ) |
|
196 |
+ rectGrob( |
|
197 |
+ row$xmin, row$ymax, width = row$xmax - row$xmin, height = row$ymax - row$ymin, |
|
198 |
+ default.units = "native", |
|
199 |
+ just = c("left", "top"), |
|
200 |
+ gp = gpar(col = row$colour, |
|
201 |
+ fill = fill, |
|
202 |
+ lwd = row$size * ggplot2:::.pt, |
|
203 |
+ lty = row$linetype, |
|
204 |
+ linejoin = linejoin, |
|
205 |
+ lineend = if (identical(linejoin, "round")) "round" else "square") |
|
206 |
+ ) |
|
207 |
+ }) |
|
208 |
+ ggname("geom_hilight_rect2", do.call("grobTree", rects)) |
|
209 |
+ }else{ |
|
210 |
+ ggname("geom_hilight_rect2", rectGrob( |
|
211 |
+ coords$xmin, coords$ymax, |
|
212 |
+ width = coords$xmax - coords$xmin, |
|
213 |
+ height = coords$ymax - coords$ymin, |
|
214 |
+ default.units = "native", |
|
215 |
+ just = c("left", "top"), |
|
216 |
+ gp = gpar(col = coords$colour, |
|
217 |
+ fill = alpha(coords$fill, coords$alpha), |
|
218 |
+ lwd = coords$size * ggplot2:::.pt, |
|
219 |
+ lty = coords$linetype, |
|
220 |
+ linejoin = linejoin, |
|
221 |
+ lineend = if (identical(linejoin, "round")) "round" else "square") |
|
159 | 222 |
)) |
223 |
+ } |
|
160 | 224 |
} |
161 | 225 |
} |
162 | 226 |
|
... | ... |
@@ -196,6 +260,94 @@ GeomHilightEncircle <- ggproto("GeomHilightEncircle", Geom, |
196 | 260 |
|
197 | 261 |
) |
198 | 262 |
|
263 |
+ |
|
264 |
+draw_panel_polar <- function (data, panel_params, coord, rule = "evenodd", gradient, gradient.direction, gradient.length.out){ |
|
265 |
+ n <- nrow(data) |
|
266 |
+ if (n == 1) |
|
267 |
+ return(zeroGrob()) |
|
268 |
+ munched <- ggplot2::coord_munch(coord, data, panel_params) |
|
269 |
+ if (is.null(munched$subgroup)) { |
|
270 |
+ munched <- munched[order(munched$group), ] |
|
271 |
+ first_idx <- !duplicated(munched$group) |
|
272 |
+ first_rows <- munched[first_idx, ] |
|
273 |
+ if (gradient){ |
|
274 |
+ gradient.direction <- match.arg(gradient.direction, c("rt", "tr")) |
|
275 |
+ rects <- lapply(split(munched, munched$group), function(row){ |
|
276 |
+ fill <- grid::linearGradient( |
|
277 |
+ x1 = unique(row$x.start), |
|
278 |
+ x2 = unique(row$x.stop), |
|
279 |
+ y1 = unique(row$y.start), |
|
280 |
+ y2 = unique(row$y.stop), |
|
281 |
+ default.units = "native", |
|
282 |
+ colours = if (gradient.direction=="rt"){ |
|
283 |
+ alpha(c(first_rows$fill, "white"), first_rows$alpha) |
|
284 |
+ }else{ |
|
285 |
+ rev(alpha(c(first_rows$fill, "white"), first_rows$alpha)) |
|
286 |
+ }, |
|
287 |
+ stops = seq(0, 1, length.out = gradient.length.out) |
|
288 |
+ ) |
|
289 |
+ grid::polygonGrob( |
|
290 |
+ row$x, row$y, id = row$group, |
|
291 |
+ default.units = "native", |
|
292 |
+ gp = gpar(col = first_rows$colour, |
|
293 |
+ fill = fill, |
|
294 |
+ lwd = first_rows$size * ggplot2:::.pt, |
|
295 |
+ lty = first_rows$linetype) |
|
296 |
+ ) |
|
297 |
+ }) |
|
298 |
+ ggname("geom_polygon2", do.call("grobTree", rects)) |
|
299 |
+ }else{ |
|
300 |
+ ggname("geom_polygon2", grid::polygonGrob(munched$x, munched$y, |
|
301 |
+ default.units = "native", id = munched$group, gp = gpar(col = first_rows$colour, |
|
302 |
+ fill = alpha(first_rows$fill, first_rows$alpha), |
|
303 |
+ lwd = first_rows$size * ggplot2::.pt, lty = first_rows$linetype))) |
|
304 |
+ } |
|
305 |
+ } |
|
306 |
+ else { |
|
307 |
+ if (utils::packageVersion("grid") < "3.6") { |
|
308 |
+ abort("Polygons with holes requires R 3.6 or above") |
|
309 |
+ } |
|
310 |
+ munched <- munched[order(munched$group, munched$subgroup), ] |
|
311 |
+ id <- match(munched$subgroup, unique(munched$subgroup)) |
|
312 |
+ first_idx <- !duplicated(munched$group) |
|
313 |
+ first_rows <- munched[first_idx, ] |
|
314 |
+ if (gradient){ |
|
315 |
+ gradient.direction <- match.arg(gradient.direction, c("rt", "tr")) |
|
316 |
+ rects <- lapply(split(munched, munched$group), function(row){ |
|
317 |
+ fill <- grid::linearGradient( |
|
318 |
+ x1 = first_rows$x.start, |
|
319 |
+ x2 = first_rows$x.stop, |
|
320 |
+ y1 = first_rows$y.start, |
|
321 |
+ y2 = first_rows$y.stop, |
|
322 |
+ default.units = "native", |
|
323 |
+ colours = if (gradient.direction=="rt"){ |
|
324 |
+ alpha(c(first_rows$fill, "white"), first_rows$alpha) |
|
325 |
+ }else{ |
|
326 |
+ rev(alpha(c(first_rows$fill, "white"), first_rows$alpha)) |
|
327 |
+ }, |
|
328 |
+ stops = seq(0, 1, length.out = gradient.length.out) |
|
329 |
+ ) |
|
330 |
+ grid::pathGrob( |
|
331 |
+ row$x, row$y, id = match(row$subgroup, unique(row$group)), |
|
332 |
+ pathId = munched$group, |
|
333 |
+ rule = rule, default.units = "native", |
|
334 |
+ gp = gpar(col = first_rows$colour, |
|
335 |
+ fill = fill, |
|
336 |
+ lwd = first_rows$size * ggplot2:::.pt, |
|