Browse code

bug fixed of geom_hilight

Guangchuang Yu authored on 16/10/2019 07:58:39
Showing4 changed files

... ...
@@ -1,5 +1,7 @@
1 1
 # ggtree 1.99.1
2 2
 
3
++ bug fixed of `geom_hilight` for `tree$edge.length = NULL` (2019-10-16, Wed)
4
+  - <https://groups.google.com/d/msg/bioc-ggtree/GULj-eoAluI/Llpm-HbfCwAJ>
3 5
 + `fortify` method for igraph (only work with tree graph) (2019-09-28, Sat)
4 6
 + `ggdensitree` (2019-09-11, Wed)
5 7
   - <https://github.com/YuLab-SMU/ggtree/pull/253>
... ...
@@ -24,7 +24,8 @@ geom_hilight <- function(node, fill="steelblue", alpha=.5, extend=0, ...) {
24 24
 
25 25
 
26 26
 
27
-geom_hilight_rectangular <- function(node, fill="steelblue", alpha=.5, extend=0, extendto=NULL) {
27
+geom_hilight_rectangular <- function(node, mapping = NULL, fill="steelblue",
28
+                                     alpha=.5, extend=0, extendto=NULL) {
28 29
   data = NULL
29 30
   stat = "hilight"
30 31
   position = "identity"
... ...
@@ -33,8 +34,14 @@ geom_hilight_rectangular <- function(node, fill="steelblue", alpha=.5, extend=0,
33 34
   inherit.aes = FALSE
34 35
   check.aes = FALSE
35 36
 
36
-  default_aes <- aes_(x=~x, y=~y, node=~node, parent=~parent, branch.length=~branch.length)
37
-  mapping <- default_aes
37
+  default_aes <- aes_(x=~x, y=~y, node=~node, parent=~parent, branch = ~branch)
38
+
39
+  if (is.null(mapping)) {
40
+      mapping <- default_aes
41
+  } else {
42
+      mapping <- modifyList(default_aes, mapping)
43
+  }
44
+
38 45
 
39 46
   layer(
40 47
     stat=StatHilight,
... ...
@@ -78,12 +85,12 @@ stat_hilight <- function(mapping=NULL, data=NULL, geom="rect",
78 85
                          fill, alpha, extend=0, extendto=NULL,
79 86
                          ...) {
80 87
 
81
-  default_aes <- aes_(x=~x, y=~y, node=~node, parent=~parent, branch.length=~branch.length)
88
+  default_aes <- aes_(x=~x, y=~y, node=~node, parent=~parent, branch=~branch) #, branch.length=~branch.length)
82 89
 
83 90
   if (is.null(mapping)) {
84 91
     mapping <- default_aes
85 92
   } else {
86
-    mapping <- modifyList(mapping, default_aes)
93
+    mapping <- modifyList(default_aes, mapping)
87 94
   }
88 95
 
89 96
   layer(
... ...
@@ -122,7 +129,7 @@ StatHilight <- ggproto("StatHilight", Stat,
122 129
                            }
123 130
                            return(df)
124 131
                        },
125
-                       required_aes = c("x", "y", "branch.length")
132
+                       required_aes = c("x", "y") #, "branch.length")
126 133
                        )
127 134
 
128 135
 
... ...
@@ -140,7 +147,6 @@ get_clade_position <- function(treeview, node) {
140 147
 }
141 148
 
142 149
 get_clade_position_ <- function(data, node) {
143
-    #sp <- tryCatch(tidytree:::offspring.tbl_tree(data, node)$node, error=function(e) NULL)
144 150
     sp <- tryCatch(offspring.tbl_tree(data, node)$node, error=function(e) NULL)
145 151
     i <- match(node, data$node)
146 152
     if (is.null(sp)) {
... ...
@@ -155,7 +161,7 @@ get_clade_position_ <- function(data, node) {
155 161
     y <- sp.df$y
156 162
     
157 163
     if ("branch.length" %in% colnames(data)) {
158
-        xmin <- min(x, na.rm=TRUE)-data[["branch.length"]][i]/2
164
+        xmin <- min(x, na.rm=TRUE) - data[["branch.length"]][i]/2
159 165
     } else {
160 166
         xmin <- min(sp.df$branch, na.rm=TRUE)
161 167
     }
... ...
@@ -245,12 +245,12 @@ geom_hilight_encircle <- function(data = NULL,
245 245
 
246 246
 
247 247
   # Select fields(columns) from the ggtree "data" data.frame to be passed to the GeomHilight ggproto object.
248
-  default_aes <- aes_( x=~x, y=~y, node=~node, parent=~parent, branch.length=~branch.length )
248
+  default_aes <- aes_( x=~x, y=~y, node=~node, parent=~parent, branch = ~branch)
249 249
 
250 250
   if (is.null(mapping)) {
251 251
     mapping <- default_aes
252 252
   } else {
253
-    mapping <- modifyList(mapping, default_aes)
253
+    mapping <- modifyList(default_aes, mapping)
254 254
   }
255 255
 
256 256
   # create xspline geom for non-uniform trees, e.g. unrooted layout
... ...
@@ -123,6 +123,10 @@ ggplot_add.hilight <- function(object, plot, object_name) {
123 123
     ## instead of the tree layout, you may get graphics::layout
124 124
     if (!is.character(layout)) layout <- 'rectangular'
125 125
 
126
+    if ("branch.length" %in% colnames(plot$data)) {
127
+        object$mapping <- aes_(branch.length = ~branch.length)
128
+    }
129
+
126 130
     if (layout == "unrooted" || layout == "daylight") {
127 131
         ly <- do.call(geom_hilight_encircle, object)
128 132
     } else {