Browse code

update identify() method

Guangchuang Yu authored on 01/04/2022 02:45:58
Showing7 changed files

... ...
@@ -1,7 +1,7 @@
1 1
 Package: ggtree
2 2
 Type: Package
3 3
 Title: an R package for visualization of tree and annotation data
4
-Version: 3.3.1.900
4
+Version: 3.3.1.901
5 5
 Authors@R: c(
6 6
        person("Guangchuang", "Yu",     email = "guangchuangyu@gmail.com", role = c("aut", "cre", "cph"), 
7 7
             comment = c(ORCID = "0000-0002-6485-8781")),
... ...
@@ -23,7 +23,7 @@ Depends:
23 23
     R (>= 3.5.0)
24 24
 Imports:
25 25
     ape,
26
-    aplot (>= 0.0.4),
26
+    aplot (>= 0.1.3),
27 27
     dplyr,
28 28
     ggplot2 (>= 3.0.0),
29 29
     grid,
... ...
@@ -25,8 +25,9 @@
25 25
 
26 26
 -->
27 27
 
28
-# ggtree 3.3.1.900
28
+# ggtree 3.3.1.901
29 29
 
30
++ update `identify.gg()` to support 'ggplot' object and `+xlim()` (2022-04-01, Fri)
30 31
 + update man files (2022-03-23, Wed, #489)
31 32
 
32 33
 # ggtree 3.3.1
... ...
@@ -120,12 +120,3 @@ revts <- function(treeview) {
120 120
     treeview
121 121
 }
122 122
 
123
-
124
-ggrange2 <- function(plot, var) {
125
-    ## aplot::ggrange extract panel range
126
-    ## this function extract plot range
127
-
128
-    var <- paste0("panel_scales_", var)
129
-    ggplot_build(plot)$layout[[var]][[1]]$range$range
130
-}
131
-
... ...
@@ -159,7 +159,7 @@ ggplot_add.ggexpand <- function(object, plot, object_name) {
159 159
 }
160 160
 
161 161
 ggexpand_internal <- function(plot, ratio, direction, var) {
162
-    r <- ggrange2(plot, var)
162
+    r <- ggrange(plot, var)
163 163
     rr <- diff(r)
164 164
     if (direction == 1) {
165 165
         res <- r[2] + rr * ratio
... ...
@@ -5,8 +5,9 @@
5 5
 ##' @rdname identify
6 6
 ##' @title identify
7 7
 ##' @param x tree view
8
-##' @param ... additional parameters
9
-##' @return node id
8
+##' @param col selected columns to extract. Default is "auto" which will select all columns for 'ggplot' object and 'node' column for 'ggtree' object
9
+##' @param ... additional parameters, normally ignored
10
+##' @return closest data point
10 11
 ##' @importFrom grid convertX
11 12
 ##' @importFrom grid convertY
12 13
 ##' @importFrom grid pushViewport
... ...
@@ -18,12 +19,23 @@
18 19
 ##' @method identify gg
19 20
 ##' @export
20 21
 ##' @author Guangchuang Yu
21
-identify.gg <- function(x = last_plot(), ...) {
22
-    tree_view <- x
22
+identify.gg <- function(x = last_plot(), col = "auto", ...) {
23
+    ## tree_view <- x
23 24
     ## x=NULL, it will call graphics::identify
24 25
 
25
-    x <- tree_view$data$x
26
-    y <- tree_view$data$y
26
+    ## x <- tree_view$data$x
27
+    ## y <- tree_view$data$y
28
+
29
+    plot <- x
30
+    xvar <- ggfun::get_aes_var(plot$mapping, 'x')
31
+    yvar <- ggfun::get_aes_var(plot$mapping, 'y')
32
+    x <- plot$data[[xvar]]
33
+    y <- plot$data[[yvar]]
34
+
35
+    xlim <- aplot::xrange(plot)
36
+    ylim <- aplot::yrange(plot)
37
+    x <- c(x, rep(xlim, times = 2))
38
+    y <- c(y, rep(ylim, each = 2))
27 39
 
28 40
     pushViewport(dataViewport(x, y))
29 41
     loc <- grid.locator('in') %>% as.numeric
... ...
@@ -32,6 +44,18 @@ identify.gg <- function(x = last_plot(), ...) {
32 44
     yy <- as.numeric(convertY( unit(y,'native'), 'in' ))
33 45
 
34 46
     idx <- which.min( (xx-loc[1])^2 + (yy-loc[2])^2 )
35
-    return(tree_view$data$node[idx])
47
+    res <- plot$data[idx,]
48
+    if (col == "auto" && inherits(plot, 'ggtree')) {
49
+        col <- 'node'
50
+    }
51
+    if (length(col) == 1 && col == "auto") {
52
+        return(res)
53
+    }
54
+
55
+    res <- res[,col]
56
+    if (length(col) == 1) {
57
+        res <- res[[1]]
58
+    }
59
+    return(res)
36 60
 }
37 61
 
... ...
@@ -173,3 +173,7 @@ message_wrap <- function(...){
173 173
 ##     tree$edge.length <- c(tree$edge.length, rep(0, ii))
174 174
 ##     return(tree)
175 175
 ## }
176
+
177
+
178
+ggrange <- getFromNamespace("ggrange", "aplot")
179
+
... ...
@@ -4,15 +4,17 @@
4 4
 \alias{identify.gg}
5 5
 \title{identify}
6 6
 \usage{
7
-\method{identify}{gg}(x = last_plot(), ...)
7
+\method{identify}{gg}(x = last_plot(), col = "auto", ...)
8 8
 }
9 9
 \arguments{
10 10
 \item{x}{tree view}
11 11
 
12
-\item{...}{additional parameters}
12
+\item{col}{selected columns to extract. Default is "auto" which will select all columns for 'ggplot' object and 'node' column for 'ggtree' object}
13
+
14
+\item{...}{additional parameters, normally ignored}
13 15
 }
14 16
 \value{
15
-node id
17
+closest data point
16 18
 }
17 19
 \description{
18 20
 identify node by interactive click