Browse code

mv identify to ggfun

Guangchuang Yu authored on 01/04/2022 08:21:52
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,61 +0,0 @@
1
-
2
-##' identify node by interactive click
3
-##'
4
-##'
5
-##' @rdname identify
6
-##' @title identify
7
-##' @param x tree view
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
11
-##' @importFrom grid convertX
12
-##' @importFrom grid convertY
13
-##' @importFrom grid pushViewport
14
-##' @importFrom grid grid.locator
15
-##' @importFrom grid unit
16
-##' @importFrom grid dataViewport
17
-##' @importFrom graphics identify
18
-##' @importFrom ggplot2 last_plot
19
-##' @method identify gg
20
-##' @export
21
-##' @author Guangchuang Yu
22
-identify.gg <- function(x = last_plot(), col = "auto", ...) {
23
-    ## tree_view <- x
24
-    ## x=NULL, it will call graphics::identify
25
-
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))
39
-
40
-    pushViewport(dataViewport(x, y))
41
-    loc <- grid.locator('in') %>% as.numeric
42
-
43
-    xx <- as.numeric(convertX( unit(x,'native'), 'in' ))
44
-    yy <- as.numeric(convertY( unit(y,'native'), 'in' ))
45
-
46
-    idx <- which.min( (xx-loc[1])^2 + (yy-loc[2])^2 )
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)
60
-}
61
-
Browse code

update identify() method

Guangchuang Yu authored on 01/04/2022 02:45:58
Showing 1 changed files
... ...
@@ -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
 
Browse code

geom_range

Guangchuang Yu authored on 23/07/2019 16:25:05
Showing 1 changed files
... ...
@@ -14,13 +14,13 @@
14 14
 ##' @importFrom grid unit
15 15
 ##' @importFrom grid dataViewport
16 16
 ##' @importFrom graphics identify
17
+##' @importFrom ggplot2 last_plot
17 18
 ##' @method identify gg
18 19
 ##' @export
19 20
 ##' @author Guangchuang Yu
20
-identify.gg <- function(x, ...) {
21
+identify.gg <- function(x = last_plot(), ...) {
21 22
     tree_view <- x
22
-    ## not used, since if x=NULL, it will call graphics::identify
23
-    ## tree_view %<>% get_tree_view 
23
+    ## x=NULL, it will call graphics::identify
24 24
 
25 25
     x <- tree_view$data$x
26 26
     y <- tree_view$data$y
Browse code

identify method for ggtree

git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@118985 bc3139a8-67e5-0310-9ffc-ced21a209358

g.yu authored on 28/06/2016 06:26:04
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,37 @@
1
+
2
+##' identify node by interactive click
3
+##'
4
+##'
5
+##' @rdname identify
6
+##' @title identify
7
+##' @param x tree view
8
+##' @param ... additional parameters
9
+##' @return node id
10
+##' @importFrom grid convertX
11
+##' @importFrom grid convertY
12
+##' @importFrom grid pushViewport
13
+##' @importFrom grid grid.locator
14
+##' @importFrom grid unit
15
+##' @importFrom grid dataViewport
16
+##' @importFrom graphics identify
17
+##' @method identify gg
18
+##' @export
19
+##' @author Guangchuang Yu
20
+identify.gg <- function(x, ...) {
21
+    tree_view <- x
22
+    ## not used, since if x=NULL, it will call graphics::identify
23
+    ## tree_view %<>% get_tree_view 
24
+
25
+    x <- tree_view$data$x
26
+    y <- tree_view$data$y
27
+
28
+    pushViewport(dataViewport(x, y))
29
+    loc <- grid.locator('in') %>% as.numeric
30
+
31
+    xx <- as.numeric(convertX( unit(x,'native'), 'in' ))
32
+    yy <- as.numeric(convertY( unit(y,'native'), 'in' ))
33
+
34
+    idx <- which.min( (xx-loc[1])^2 + (yy-loc[2])^2 )
35
+    return(tree_view$data$node[idx])
36
+}
37
+