Browse code

remove rvcheck depend

xiangpin authored on 18/08/2021 06:27:37
Showing 1 changed files
... ...
@@ -9,7 +9,7 @@
9 9
 ##' @param align where to place gene label, default is 'centre' and can be set to 'left' and 'right'
10 10
 ##' @param ... additional parameters
11 11
 ##' @return geom layer
12
-##' @importFrom rvcheck get_aes_var
12
+##' @importFrom ggfun get_aes_var
13 13
 ##' @export
14 14
 ##' @author Guangchuang Yu
15 15
 geom_motif <- function(mapping, data, on, label, align = 'centre', ...) {
Browse code

td_unnest

Guangchuang Yu authored on 14/09/2020 02:14:03
Showing 1 changed files
... ...
@@ -1,27 +1,3 @@
1
-##' filter data for tree annotation layer
2
-##'
3
-##' The 'tree_filter()' function returns another function that can be
4
-##' used to subset ggtree() plot data. The function can be passed to the 'data' parameter
5
-##' in geom layer to perform subsetting. All rows that satisy your conditions will be retained.
6
-##' @title tree_filter
7
-##' @param ... Expressions that return a logical value.
8
-##' @return A function to filter ggtree plot data using conditions defined by '...'.
9
-##' @seealso
10
-##' [filter][dplyr::filter] 
11
-##' @author Guangchuang Yu
12
-##' @examples
13
-##' tree <- rtree(30)
14
-##' ## similar to 'ggtree(tree) + geom_tippoint()'
15
-##' ggtree(tree) + geom_point(data = tree_filter(isTip))
16
-##' @export
17
-tree_filter <- function(...) {
18
-    dots <- rlang::quos(...)
19
-    function(.data) {
20
-        dplyr::filter(.data, !!!dots)
21
-    }
22
-}
23
-
24
-
25 1
 ##' geom layer to draw aligned motif
26 2
 ##'
27 3
 ##'
Browse code

tree_filter

Guangchuang Yu authored on 29/08/2020 05:49:35
Showing 1 changed files
... ...
@@ -1,3 +1,27 @@
1
+##' filter data for tree annotation layer
2
+##'
3
+##' The 'tree_filter()' function returns another function that can be
4
+##' used to subset ggtree() plot data. The function can be passed to the 'data' parameter
5
+##' in geom layer to perform subsetting. All rows that satisy your conditions will be retained.
6
+##' @title tree_filter
7
+##' @param ... Expressions that return a logical value.
8
+##' @return A function to filter ggtree plot data using conditions defined by '...'.
9
+##' @seealso
10
+##' [filter][dplyr::filter] 
11
+##' @author Guangchuang Yu
12
+##' @examples
13
+##' tree <- rtree(30)
14
+##' ## similar to 'ggtree(tree) + geom_tippoint()'
15
+##' ggtree(tree) + geom_point(data = tree_filter(isTip))
16
+##' @export
17
+tree_filter <- function(...) {
18
+    dots <- rlang::quos(...)
19
+    function(.data) {
20
+        dplyr::filter(.data, !!!dots)
21
+    }
22
+}
23
+
24
+
1 25
 ##' geom layer to draw aligned motif
2 26
 ##'
3 27
 ##'
Browse code

remove functions

Guangchuang Yu authored on 28/07/2020 07:02:57
Showing 1 changed files
... ...
@@ -40,7 +40,8 @@ geom_motif <- function(mapping, data, on, label, align = 'centre', ...) {
40 40
          ly_lab)
41 41
 }
42 42
 
43
-
43
+##' @importFrom ggplot2 annotation_custom
44
+##' @importFrom ggplot2 ggplotGrob
44 45
 plot_fantree <- function(fantree, upper=TRUE) {
45 46
     if (upper) {
46 47
         ymin <- -.25
... ...
@@ -213,19 +214,3 @@ coplot <- function(tree1, tree2, hjust=0) {
213 214
 
214 215
 
215 216
 
216
-##' set legend for multiple geom_hilight layers
217
-##'
218
-##'
219
-##' @title set_hilight_legend
220
-##' @param p ggtree object
221
-##' @param color color vector
222
-##' @param label label vector
223
-##' @param alpha transparency of color
224
-##' @return updated ggtree object
225
-##' @export
226
-##' @author Guangchuang Yu
227
-set_hilight_legend <- function(p, color, label, alpha=1) {
228
-	d <- data.frame(color=color, clade=label, x=0, y=1, alpha=alpha)
229
-	p + geom_rect(aes_(fill=~clade, xmin=~x, xmax=~x, ymin=~y, ymax=~y), data=d, inherit.aes=F) +
230
-		guides(fill=guide_legend(override.aes=list(fill=alpha(d$color, d$alpha))))
231
-}
Browse code

geom_motif

Guangchuang Yu authored on 27/08/2019 08:10:45
Showing 1 changed files
... ...
@@ -5,26 +5,39 @@
5 5
 ##' @param mapping aes mapping
6 6
 ##' @param data data
7 7
 ##' @param on gene to center (i.e. set middle position of the `on` gene to 0)
8
+##' @param label specify a column to be used to label genes
9
+##' @param align where to place gene label, default is 'centre' and can be set to 'left' and 'right'
8 10
 ##' @param ... additional parameters
9 11
 ##' @return geom layer
10 12
 ##' @importFrom rvcheck get_aes_var
11 13
 ##' @export
12 14
 ##' @author Guangchuang Yu
13
-geom_motif <- function(mapping, data, on, ...) {
14
-
15
-    seqnames <- 'label' 
15
+geom_motif <- function(mapping, data, on, label, align = 'centre', ...) {
16
+   
16 17
     id <- get_aes_var(mapping, 'fill')
17 18
 
18 19
     dd <- data[data[, id] == on,]
19 20
     mid <- dd$start + (dd$end - dd$start)/2
20
-    names(mid) <- as.character(dd[, seqnames])
21 21
 
22
-    adj <- mid[as.character(data[, seqnames])]
22
+    names(mid) <- dd$label
23
+
24
+    adj <- mid[data$label]
23 25
     data$start <- data$start - adj
24 26
     data$end <- data$end - adj
25 27
     geom_gene_arrow <- get_fun_from_pkg("gggenes", "geom_gene_arrow")
26 28
     mapping <- modifyList(mapping, aes_(y = ~y))
27
-    geom_gene_arrow(mapping = mapping, data = data, inherit.aes = FALSE, ...)
29
+    ly_gene <- geom_gene_arrow(mapping = mapping, data = data, inherit.aes = FALSE, ...)
30
+    if (missing(label)) {
31
+        return(ly_gene)
32
+    }
33
+
34
+    geom_gene_label <- get_fun_from_pkg("gggenes", "geom_gene_label")
35
+    mapping <- modifyList(mapping, aes_string(label = label))
36
+    if (align == 'center') align <- 'centre'
37
+    ly_lab <- geom_gene_label(mapping = mapping, data = data, align = align,
38
+                              inherit.aes = FALSE,...)
39
+    list(ly_gene,
40
+         ly_lab)
28 41
 }
29 42
 
30 43
 
Browse code

support phylog

Guangchuang Yu authored on 21/08/2019 05:59:44
Showing 1 changed files
... ...
@@ -7,28 +7,24 @@
7 7
 ##' @param on gene to center (i.e. set middle position of the `on` gene to 0)
8 8
 ##' @param ... additional parameters
9 9
 ##' @return geom layer
10
+##' @importFrom rvcheck get_aes_var
10 11
 ##' @export
11 12
 ##' @author Guangchuang Yu
12 13
 geom_motif <- function(mapping, data, on, ...) {
13
-    if (is.null(unlist(mapping)$y)) {
14
-        seqnames <- as.character(unlist(mapping)$group)
15
-    } else {
16
-        seqnames <- as.character(unlist(mapping)$y)
17
-    }
18 14
 
19
-    if (is.null(unlist(mapping$fill))) {
20
-        id <- as.character(unlist(mapping$id))
21
-    } else {
22
-        id <- as.character(unlist(mapping$fill))
23
-    }
24
-    dd <- data[unlist(data[, id]) == on,]
15
+    seqnames <- 'label' 
16
+    id <- get_aes_var(mapping, 'fill')
17
+
18
+    dd <- data[data[, id] == on,]
25 19
     mid <- dd$start + (dd$end - dd$start)/2
26
-    names(mid) <- as.character(unlist(dd[, seqnames]))
27
-    adj <- mid[as.character(unlist(data[, seqnames]))]
20
+    names(mid) <- as.character(dd[, seqnames])
21
+
22
+    adj <- mid[as.character(data[, seqnames])]
28 23
     data$start <- data$start - adj
29 24
     data$end <- data$end - adj
30 25
     geom_gene_arrow <- get_fun_from_pkg("gggenes", "geom_gene_arrow")
31
-    geom_gene_arrow(mapping = mapping, data = as.data.frame(data), ...)
26
+    mapping <- modifyList(mapping, aes_(y = ~y))
27
+    geom_gene_arrow(mapping = mapping, data = data, inherit.aes = FALSE, ...)
32 28
 }
33 29
 
34 30
 
Browse code

update scale_x_ggtree

Guangchuang Yu authored on 22/05/2019 14:09:37
Showing 1 changed files
... ...
@@ -84,58 +84,6 @@ get_heatmap_column_position <- function(treeview, by="bottom") {
84 84
     return(mapping)
85 85
 }
86 86
 
87
-##' scale x for tree with heatmap
88
-##'
89
-##'
90
-##' @title scale_x_ggtree
91
-##' @param tree_view tree view
92
-##' @param breaks breaks for tree
93
-##' @param labels lables for corresponding breaks
94
-##' @return tree view
95
-##' @importFrom ggplot2 scale_x_continuous
96
-##' @importFrom ggplot2 scale_x_date
97
-##' @export
98
-##' @author Guangchuang Yu
99
-scale_x_ggtree <- function(tree_view, breaks=NULL, labels=NULL) {
100
-    p <- get_tree_view(tree_view)
101
-
102
-    mrsd <- get("mrsd", envir=tree_view$plot_env)
103
-    if (!is.null(mrsd) && class(p$data$x) == "Date") {
104
-        x <- Date2decimal(p$data$x)
105
-    } else {
106
-        x <- p$data$x
107
-    }
108
-
109
-    if (is.null(breaks)) {
110
-        breaks <- graphics::hist(x, breaks=5, plot=FALSE)$breaks
111
-    }
112
-    m <- attr(p, "mapping")
113
-
114
-    if (!is.null(mrsd) &&class(m$to) == "Date") {
115
-        to <- Date2decimal(m$to)
116
-    } else {
117
-        to <- m$to
118
-    }
119
-
120
-    idx <- which(sapply(breaks, function(x) any(x > m$to)))
121
-    if (length(idx)) {
122
-        breaks <- breaks[-idx]
123
-    }
124
-
125
-    if (is.null(labels)) {
126
-        labels <- breaks
127
-    }
128
-
129
-    breaks <- c(breaks, to)
130
-    labels <- c(labels, gsub("\\.", "", as.character(m$from)))
131
-
132
-    if (!is.null(mrsd) && class(p$data$x) == "Date") {
133
-        p <- p + scale_x_date(breaks=decimal2Date(breaks), labels)
134
-    } else {
135
-        p <- p + scale_x_continuous(breaks=breaks, labels=labels)
136
-    }
137
-    return(p)
138
-}
139 87
 
140 88
 
141 89
 
Browse code

reduce dependency

Guangchuang Yu authored on 27/01/2019 23:14:00
Showing 1 changed files
... ...
@@ -8,7 +8,7 @@
8 8
 ##' @param ... additional parameters
9 9
 ##' @return geom layer
10 10
 ##' @export
11
-##' @author guangchuang yu
11
+##' @author Guangchuang Yu
12 12
 geom_motif <- function(mapping, data, on, ...) {
13 13
     if (is.null(unlist(mapping)$y)) {
14 14
         seqnames <- as.character(unlist(mapping)$group)
... ...
@@ -107,7 +107,7 @@ scale_x_ggtree <- function(tree_view, breaks=NULL, labels=NULL) {
107 107
     }
108 108
 
109 109
     if (is.null(breaks)) {
110
-        breaks <- hist(x, breaks=5, plot=FALSE)$breaks
110
+        breaks <- graphics::hist(x, breaks=5, plot=FALSE)$breaks
111 111
     }
112 112
     m <- attr(p, "mapping")
113 113
 
... ...
@@ -266,8 +266,7 @@ coplot <- function(tree1, tree2, hjust=0) {
266 266
 ##' @param alpha transparency of color
267 267
 ##' @return updated ggtree object
268 268
 ##' @export
269
-##' @importFrom scales alpha
270
-##' @author guangchuang yu
269
+##' @author Guangchuang Yu
271 270
 set_hilight_legend <- function(p, color, label, alpha=1) {
272 271
 	d <- data.frame(color=color, clade=label, x=0, y=1, alpha=alpha)
273 272
 	p + geom_rect(aes_(fill=~clade, xmin=~x, xmax=~x, ymin=~y, ymax=~y), data=d, inherit.aes=F) +
Browse code

just in case fix examples in experimental function gplot

Vladimir Mikryukov authored on 27/09/2018 04:12:26
Showing 1 changed files
... ...
@@ -156,7 +156,7 @@ scale_x_ggtree <- function(tree_view, breaks=NULL, labels=NULL) {
156 156
 ## ##' @export
157 157
 ## ##' @author Guangchuang Yu \url{http://ygc.name}
158 158
 ## ##' @examples
159
-## ##' nwk <- system.file("extdata", "sample.nwk", package="ggtree")
159
+## ##' nwk <- system.file("extdata", "sample.nwk", package="treeio")
160 160
 ## ##' tree <- read.tree(nwk)
161 161
 ## ##' p <- ggtree(tree)
162 162
 ## ##' d <- matrix(abs(rnorm(52)), ncol=4)
Browse code

remove theme_transparent

guangchuang yu authored on 04/12/2017 12:41:52
Showing 1 changed files
... ...
@@ -47,16 +47,12 @@ plot_fantree <- function(fantree, upper=TRUE) {
47 47
         annotation_custom(ggplotGrob(fantree),
48 48
                           xmin=-.15, xmax=1.15,
49 49
                           ymin=ymin, ymax=ymax)
50
-    ## d <- data.frame(x=c(0,1), y=c(0.5, 1))
51
-    ## ggplot(d, aes_(x=~x, y=~y)) %>%
52
-    ##     subview(fantree, 0.5, y, width=2, height=2.2) ##  %>%
53
-    ## subview(p2+theme_transparent(), 0.5, 0.45, width=2, height=2.2) + theme_tree()
54 50
 }
55 51
 
56 52
 plot_fantrees <- function(uppertree, lowertree) {
57 53
     ggplot() + xlim(0,1) + ylim(0.5, 1) + theme_tree() +
58 54
         annotation_custom(ggplotGrob(uppertree), xmin=-.15, xmax=1.15, ymin=0.52, ymax=1.02) +
59
-        annotation_custom(ggplotGrob(lowertree + theme_transparent()), xmin=-.15, xmax=1.15, ymin=0.48, ymax=0.98)
55
+        annotation_custom(ggplotGrob(lowertree + ggimage::theme_transparent()), xmin=-.15, xmax=1.15, ymin=0.48, ymax=0.98)
60 56
 }
61 57
 
62 58
 
Browse code

mrsd for treedata object

guangchuang yu authored on 15/11/2017 07:45:39
Showing 1 changed files
... ...
@@ -267,11 +267,13 @@ coplot <- function(tree1, tree2, hjust=0) {
267 267
 ##' @param p ggtree object
268 268
 ##' @param color color vector
269 269
 ##' @param label label vector
270
+##' @param alpha transparency of color
270 271
 ##' @return updated ggtree object
271 272
 ##' @export
273
+##' @importFrom scales alpha
272 274
 ##' @author guangchuang yu
273
-set_hilight_legend <- function(p, color, label) {
274
-	d <- data.frame(color=color, clade=label, x=0, y=1)
275
+set_hilight_legend <- function(p, color, label, alpha=1) {
276
+	d <- data.frame(color=color, clade=label, x=0, y=1, alpha=alpha)
275 277
 	p + geom_rect(aes_(fill=~clade, xmin=~x, xmax=~x, ymin=~y, ymax=~y), data=d, inherit.aes=F) +
276
-		guides(fill=guide_legend(override.aes=list(fill=d$color)))
278
+		guides(fill=guide_legend(override.aes=list(fill=alpha(d$color, d$alpha))))
277 279
 }
guangchuang yu authored on 30/08/2017 08:07:43
Showing 1 changed files
... ...
@@ -272,6 +272,6 @@ coplot <- function(tree1, tree2, hjust=0) {
272 272
 ##' @author guangchuang yu
273 273
 set_hilight_legend <- function(p, color, label) {
274 274
 	d <- data.frame(color=color, clade=label, x=0, y=1)
275
-	p <- p + geom_rect(aes_(fill=~clade, xmin=~x, xmax=~x, ymin=~y, ymax=~y), data=d, inherit.aes=F) +
275
+	p + geom_rect(aes_(fill=~clade, xmin=~x, xmax=~x, ymin=~y, ymax=~y), data=d, inherit.aes=F) +
276 276
 		guides(fill=guide_legend(override.aes=list(fill=d$color)))
277 277
 }
Browse code

set_hilight_legend

guangchuang yu authored on 30/08/2017 06:34:31
Showing 1 changed files
... ...
@@ -260,4 +260,18 @@ coplot <- function(tree1, tree2, hjust=0) {
260 260
 
261 261
 
262 262
 
263
-
263
+##' set legend for multiple geom_hilight layers
264
+##'
265
+##'
266
+##' @title set_hilight_legend
267
+##' @param p ggtree object
268
+##' @param color color vector
269
+##' @param label label vector
270
+##' @return updated ggtree object
271
+##' @export
272
+##' @author guangchuang yu
273
+set_hilight_legend <- function(p, color, label) {
274
+	d <- data.frame(color=color, clade=label, x=0, y=1)
275
+	p <- p + geom_rect(aes_(fill=~clade, xmin=~x, xmax=~x, ymin=~y, ymax=~y), data=d, inherit.aes=F) +
276
+		guides(fill=guide_legend(override.aes=list(fill=d$color)))
277
+}
Browse code

geom_motif #148

guangchuang yu authored on 22/08/2017 08:22:37
Showing 1 changed files
... ...
@@ -1,3 +1,37 @@
1
+##' geom layer to draw aligned motif
2
+##'
3
+##'
4
+##' @title geom_motif
5
+##' @param mapping aes mapping
6
+##' @param data data
7
+##' @param on gene to center (i.e. set middle position of the `on` gene to 0)
8
+##' @param ... additional parameters
9
+##' @return geom layer
10
+##' @export
11
+##' @author guangchuang yu
12
+geom_motif <- function(mapping, data, on, ...) {
13
+    if (is.null(unlist(mapping)$y)) {
14
+        seqnames <- as.character(unlist(mapping)$group)
15
+    } else {
16
+        seqnames <- as.character(unlist(mapping)$y)
17
+    }
18
+
19
+    if (is.null(unlist(mapping$fill))) {
20
+        id <- as.character(unlist(mapping$id))
21
+    } else {
22
+        id <- as.character(unlist(mapping$fill))
23
+    }
24
+    dd <- data[unlist(data[, id]) == on,]
25
+    mid <- dd$start + (dd$end - dd$start)/2
26
+    names(mid) <- as.character(unlist(dd[, seqnames]))
27
+    adj <- mid[as.character(unlist(data[, seqnames]))]
28
+    data$start <- data$start - adj
29
+    data$end <- data$end - adj
30
+    geom_gene_arrow <- get_fun_from_pkg("gggenes", "geom_gene_arrow")
31
+    geom_gene_arrow(mapping = mapping, data = as.data.frame(data), ...)
32
+}
33
+
34
+
1 35
 plot_fantree <- function(fantree, upper=TRUE) {
2 36
     if (upper) {
3 37
         ymin <- -.25
Browse code

plot_fantree & plot_fantrees

guangchuang yu authored on 06/03/2017 09:38:25
Showing 1 changed files
... ...
@@ -1,3 +1,30 @@
1
+plot_fantree <- function(fantree, upper=TRUE) {
2
+    if (upper) {
3
+        ymin <- -.25
4
+        ymax <- 1.3
5
+        ## y <- 0.55
6
+    } else {
7
+        ymin <- .2
8
+        ymax <- 1.75
9
+        ## y <- 0.45
10
+    }
11
+
12
+    ggplot() + xlim(0,1) + ylim(0.5, 1) + theme_tree() +
13
+        annotation_custom(ggplotGrob(fantree),
14
+                          xmin=-.15, xmax=1.15,
15
+                          ymin=ymin, ymax=ymax)
16
+    ## d <- data.frame(x=c(0,1), y=c(0.5, 1))
17
+    ## ggplot(d, aes_(x=~x, y=~y)) %>%
18
+    ##     subview(fantree, 0.5, y, width=2, height=2.2) ##  %>%
19
+    ## subview(p2+theme_transparent(), 0.5, 0.45, width=2, height=2.2) + theme_tree()
20
+}
21
+
22
+plot_fantrees <- function(uppertree, lowertree) {
23
+    ggplot() + xlim(0,1) + ylim(0.5, 1) + theme_tree() +
24
+        annotation_custom(ggplotGrob(uppertree), xmin=-.15, xmax=1.15, ymin=0.52, ymax=1.02) +
25
+        annotation_custom(ggplotGrob(lowertree + theme_transparent()), xmin=-.15, xmax=1.15, ymin=0.48, ymax=0.98)
26
+}
27
+
1 28
 
2 29
 
3 30
 ##' return a data.frame that contains position information
Browse code

update set_branch_length

guangchuang yu authored on 15/02/2017 08:05:49
Showing 1 changed files
... ...
@@ -1,9 +1,9 @@
1 1
 
2 2
 
3
-##' return a data.frame that contains position information 
3
+##' return a data.frame that contains position information
4 4
 ##' for labeling column names of heatmap produced by `gheatmap` function
5 5
 ##'
6
-##' 
6
+##'
7 7
 ##' @title get_heatmap_column_position
8 8
 ##' @param treeview output of `gheatmap`
9 9
 ##' @param by one of 'bottom' or 'top'
... ...
@@ -29,7 +29,7 @@ get_heatmap_column_position <- function(treeview, by="bottom") {
29 29
 
30 30
 ##' scale x for tree with heatmap
31 31
 ##'
32
-##' 
32
+##'
33 33
 ##' @title scale_x_ggtree
34 34
 ##' @param tree_view tree view
35 35
 ##' @param breaks breaks for tree
... ...
@@ -59,16 +59,16 @@ scale_x_ggtree <- function(tree_view, breaks=NULL, labels=NULL) {
59 59
     } else {
60 60
         to <- m$to
61 61
     }
62
-    
62
+
63 63
     idx <- which(sapply(breaks, function(x) any(x > m$to)))
64 64
     if (length(idx)) {
65 65
         breaks <- breaks[-idx]
66 66
     }
67
-    
67
+
68 68
     if (is.null(labels)) {
69 69
         labels <- breaks
70 70
     }
71
-    
71
+
72 72
     breaks <- c(breaks, to)
73 73
     labels <- c(labels, gsub("\\.", "", as.character(m$from)))
74 74
 
... ...
@@ -77,7 +77,7 @@ scale_x_ggtree <- function(tree_view, breaks=NULL, labels=NULL) {
77 77
     } else {
78 78
         p <- p + scale_x_continuous(breaks=breaks, labels=labels)
79 79
     }
80
-    return(p)    
80
+    return(p)
81 81
 }
82 82
 
83 83
 
... ...
@@ -134,37 +134,37 @@ scale_x_ggtree <- function(tree_view, breaks=NULL, labels=NULL) {
134 134
 ##     ## p <- p+geom_segment(aes(x=x*1.02, xend=max(x)*1.08, yend=y), subset=.(isTip), linetype="dashed", size=0.4)
135 135
 ##     df=p$data
136 136
 ##     df=df[df$isTip,]
137
-    
137
+
138 138
 ##     dd$Var1 <- factor(dd$Var1, levels = df$label[order(df$y)])
139 139
 ##     if (any(dd$value == "")) {
140 140
 ##         dd$value[dd$value == ""] <- NA
141 141
 ##     }
142
-    
142
+
143 143
 ##     p2 <- ggplot(dd, aes(Var2, Var1, fill=value))+geom_tile(color=color)
144 144
 ##     if (is(dd$value,"numeric")) {
145 145
 ##         p2 <- p2 + scale_fill_gradient(low=low, high=high, na.value="white")
146 146
 ##     } else {
147 147
 ##         p2 <- p2 + scale_fill_discrete(na.value="white")
148 148
 ##     }
149
-    
149
+
150 150
 ##     p2 <- p2+xlab("")+ylab("")
151 151
 ##     p2 <- p2+theme_tree2() + theme(axis.ticks.x = element_blank(),
152 152
 ##                                    axis.line.x=element_blank())
153 153
 ##     ## p1 <- p1 + theme(axis.text.x = element_text(size = font.size))
154
-##     p2 <- p2 + theme(axis.ticks.margin = unit(0, "lines")) 
154
+##     p2 <- p2 + theme(axis.ticks.margin = unit(0, "lines"))
155 155
 ##     p2 <- p2 + theme(axis.text.x = element_text(size = font.size))
156 156
 ##     ## p2 <- p2 + theme(axis.text.y = element_text(size=font.size))
157
-    
158
-##     ## plot.margin   margin around entire plot (unit with the sizes of the top, right, bottom, and left margins) 
157
+
158
+##     ## plot.margin   margin around entire plot (unit with the sizes of the top, right, bottom, and left margins)
159 159
 ##     ## units can be given in "lines" or  something more specific like "cm"...
160 160
 
161
-    
161
+
162 162
 ##     p2 <- p2 + theme(panel.margin=unit(0, "null"))
163 163
 ##     p2 <- p2 + theme(plot.margin = unit(c(1, 1, .5, -0.5), "lines"))
164 164
 ##     p2 <- p2 + theme(legend.position = "right")
165 165
 ##     p2 <- p2 + guides(fill = guide_legend(override.aes = list(colour = NULL)))
166 166
 ##     ## p2 <- p2 + labs(fill="")
167
-    
167
+
168 168
 ##     return(p2)
169 169
 ## }
170 170
 
... ...
@@ -185,7 +185,7 @@ coplot <- function(tree1, tree2, hjust=0) {
185 185
         geom_tree(layout="phylogram", subset=.(tree=="A")) +
186 186
             geom_tree(layout="phylogram", subset=.(tree=="B")) +
187 187
                 theme_tree()
188
- 
188
+
189 189
     p <- p  + geom_text(aes(label=label),
190 190
                         subset=.(isTip & tree == "A"),
191 191
                         hjust=-offset/40) +
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
... ...
@@ -200,4 +200,3 @@ coplot <- function(tree1, tree2, hjust=0) {
200 200
 
201 201
 
202 202
 
203
-
Browse code

open_tree & rotate_tree

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

g.yu authored on 12/05/2016 10:16:07
Showing 1 changed files
... ...
@@ -1,3 +1,5 @@
1
+
2
+
1 3
 ##' return a data.frame that contains position information 
2 4
 ##' for labeling column names of heatmap produced by `gheatmap` function
3 5
 ##'
... ...
@@ -25,120 +27,6 @@ get_heatmap_column_position <- function(treeview, by="bottom") {
25 27
     return(mapping)
26 28
 }
27 29
 
28
-##' multiple sequence alignment with phylogenetic tree
29
-##'
30
-##' 
31
-##' @title msaplot
32
-##' @param p tree view
33
-##' @param fasta fasta file, multiple sequence alignment
34
-##' @param offset offset of MSA to tree
35
-##' @param width total width of alignment, compare to width of tree
36
-##' @param color color 
37
-##' @param window specific a slice to display
38
-##' @return tree view
39
-##' @export
40
-##' @importFrom Biostrings readBStringSet
41
-##' @importMethodsFrom Biostrings width
42
-## @importFrom colorspace rainbow_hcl
43
-##' @importFrom ggplot2 geom_segment
44
-##' @importFrom ggplot2 geom_rect
45
-##' @importFrom ggplot2 scale_fill_manual
46
-##' @author Guangchuang Yu
47
-msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
48
-    if (missingArg(fasta)) {
49
-        aln <- NULL
50
-    } else if (is(fasta, "BStringSet")) {
51
-        aln <- fasta
52
-    } else if (is(fasta, "character")) {
53
-        aln <- readBStringSet(fasta)
54
-    } else {
55
-        aln <- NULL
56
-    }
57
-        
58
-    if (is(p, "phylip")) {
59
-        aln <- p@sequence
60
-        p <- ggtree(p) + geom_tiplab()
61
-    }
62
-
63
-    if (is.null(aln)) {
64
-        stop("multiple sequence alignment is not available...\n-> check the parameter 'fasta'...")
65
-    }
66
-    
67
-    if (is.null(window)) {
68
-        window <- c(1, width(aln)[1])
69
-    }
70
-    slice <- seq(window[1], window[2], by=1)
71
-    
72
-    seqs <- lapply(1:length(aln), function(i) {
73
-        x <- toString(aln[i])
74
-        seq <- substring(x, slice, slice)
75
-
76
-        seq[seq == '?'] <- '-'
77
-        seq[seq == '*'] <- '-'
78
-        seq[seq == ' '] <- '-'
79
-        return(seq)
80
-    })
81
-    names(seqs) <- names(aln)
82
-    
83
-    if(is.null(color)) {
84
-        alphabet <- unlist(seqs) %>% unique
85
-        alphabet <- alphabet[alphabet != '-']
86
-        ## color <- rainbow_hcl(length(alphabet))
87
-        color <- getCols(length(alphabet))
88
-        names(color) <- alphabet
89
-        color <- c(color, '-'=NA)
90
-    }
91
-
92
-    df <- p$data
93
-    ## if (is.null(width)) {
94
-    ##     width <- (df$x %>% range %>% diff)/500
95
-    ## }
96
-
97
-    ## convert width to width of each cell
98
-    width <- width * (df$x %>% range %>% diff) / diff(window)
99
-    
100
-    df=df[df$isTip,]
101
-    start <- max(df$x) * 1.02 + offset
102
-
103
-    seqs <- seqs[df$label[order(df$y)]]
104
-    ## seqs.df <- do.call("rbind", seqs)
105
-
106
-    h <- ceiling(diff(range(df$y))/length(df$y))
107
-    xmax <- start + seq_along(slice) * width
108
-    xmin <- xmax - width
109
-    y <- sort(df$y)
110
-    ymin <- y - 0.4 *h
111
-    ymax <- y + 0.4 *h
112
-
113
-    from <- to <- NULL
114
-    
115
-    lines.df <- data.frame(from=min(xmin), to=max(xmax), y = y)
116
-
117
-    p <- p + geom_segment(data=lines.df, aes(x=from, xend=to, y=y, yend=y))
118
-    msa <- lapply(1:length(y), function(i) {
119
-        data.frame(name=names(seqs)[i],
120
-                   xmin=xmin,
121
-                   xmax=xmax,
122
-                   ymin=ymin[i],
123
-                   ymax=ymax[i],
124
-                   seq=seqs[[i]])
125
-    })
126
-
127
-    msa.df <- do.call("rbind", msa)
128
-
129
-    p <- p + geom_rect(data=msa.df, aes(x=xmin, y=ymin, 
130
-                           xmin=xmin, xmax=xmax,
131
-                           ymin=ymin, ymax=ymax, fill=seq)) +
132
-                               scale_fill_manual(values=color)
133
-
134
-    breaks <- hist(seq_along(slice), breaks=10, plot=FALSE)$breaks
135
-    pos <- start + breaks * width
136
-    mapping <- data.frame(from=breaks+1, to=pos)
137
-    attr(p, "mapping") <- mapping
138
-    
139
-    return(p)
140
-}
141
-
142 30
 ##' scale x for tree with heatmap
143 31
 ##'
144 32
 ##' 
Browse code

use plot

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

g.yu authored on 06/04/2016 08:34:24
Showing 1 changed files
... ...
@@ -1,2 +1,315 @@
1
+##' return a data.frame that contains position information 
2
+##' for labeling column names of heatmap produced by `gheatmap` function
3
+##'
4
+##' 
5
+##' @title get_heatmap_column_position
6
+##' @param treeview output of `gheatmap`
7
+##' @param by one of 'bottom' or 'top'
8
+##' @return data.frame
9
+##' @export
10
+##' @author Guangchuang Yu
11
+get_heatmap_column_position <- function(treeview, by="bottom") {
12
+    by %<>% match.arg(c("bottom", "top"))
13
+
14
+    mapping <- attr(treeview, "mapping")
15
+    if (is.null(mapping)) {
16
+        stop("treeview is not an output of `gheatmap`...")
17
+    }
18
+
19
+    colnames(mapping) <- c("label", "x")
20
+    if (by == "bottom") {
21
+        mapping$y <- 0
22
+    } else {
23
+        mapping$y <- max(treeview$data$y) + 1
24
+    }
25
+    return(mapping)
26
+}
27
+
28
+##' multiple sequence alignment with phylogenetic tree
29
+##'
30
+##' 
31
+##' @title msaplot
32
+##' @param p tree view
33
+##' @param fasta fasta file, multiple sequence alignment
34
+##' @param offset offset of MSA to tree
35
+##' @param width total width of alignment, compare to width of tree
36
+##' @param color color 
37
+##' @param window specific a slice to display
38
+##' @return tree view
39
+##' @export
40
+##' @importFrom Biostrings readBStringSet
41
+##' @importMethodsFrom Biostrings width
42
+## @importFrom colorspace rainbow_hcl
43
+##' @importFrom ggplot2 geom_segment
44
+##' @importFrom ggplot2 geom_rect
45
+##' @importFrom ggplot2 scale_fill_manual
46
+##' @author Guangchuang Yu
47
+msaplot <- function(p, fasta, offset=0, width=1, color=NULL, window=NULL){
48
+    if (missingArg(fasta)) {
49
+        aln <- NULL
50
+    } else if (is(fasta, "BStringSet")) {
51
+        aln <- fasta
52
+    } else if (is(fasta, "character")) {
53
+        aln <- readBStringSet(fasta)
54
+    } else {
55
+        aln <- NULL
56
+    }
57
+        
58
+    if (is(p, "phylip")) {
59
+        aln <- p@sequence
60
+        p <- ggtree(p) + geom_tiplab()
61
+    }
62
+
63
+    if (is.null(aln)) {
64
+        stop("multiple sequence alignment is not available...\n-> check the parameter 'fasta'...")
65
+    }
66
+    
67
+    if (is.null(window)) {
68
+        window <- c(1, width(aln)[1])
69
+    }
70
+    slice <- seq(window[1], window[2], by=1)
71
+    
72
+    seqs <- lapply(1:length(aln), function(i) {
73
+        x <- toString(aln[i])
74
+        seq <- substring(x, slice, slice)
75
+
76
+        seq[seq == '?'] <- '-'
77
+        seq[seq == '*'] <- '-'
78
+        seq[seq == ' '] <- '-'
79
+        return(seq)
80
+    })
81
+    names(seqs) <- names(aln)
82
+    
83
+    if(is.null(color)) {
84
+        alphabet <- unlist(seqs) %>% unique
85
+        alphabet <- alphabet[alphabet != '-']
86
+        ## color <- rainbow_hcl(length(alphabet))
87
+        color <- getCols(length(alphabet))
88
+        names(color) <- alphabet
89
+        color <- c(color, '-'=NA)
90
+    }
91
+
92
+    df <- p$data
93
+    ## if (is.null(width)) {
94
+    ##     width <- (df$x %>% range %>% diff)/500
95
+    ## }
96
+
97
+    ## convert width to width of each cell
98
+    width <- width * (df$x %>% range %>% diff) / diff(window)
99
+    
100
+    df=df[df$isTip,]
101
+    start <- max(df$x) * 1.02 + offset
102
+
103
+    seqs <- seqs[df$label[order(df$y)]]
104
+    ## seqs.df <- do.call("rbind", seqs)
105
+
106
+    h <- ceiling(diff(range(df$y))/length(df$y))
107
+    xmax <- start + seq_along(slice) * width
108
+    xmin <- xmax - width
109
+    y <- sort(df$y)
110
+    ymin <- y - 0.4 *h
111
+    ymax <- y + 0.4 *h
112
+
113
+    from <- to <- NULL
114
+    
115
+    lines.df <- data.frame(from=min(xmin), to=max(xmax), y = y)
116
+
117
+    p <- p + geom_segment(data=lines.df, aes(x=from, xend=to, y=y, yend=y))
118
+    msa <- lapply(1:length(y), function(i) {
119
+        data.frame(name=names(seqs)[i],
120
+                   xmin=xmin,
121
+                   xmax=xmax,
122
+                   ymin=ymin[i],
123
+                   ymax=ymax[i],
124
+                   seq=seqs[[i]])
125
+    })
126
+
127
+    msa.df <- do.call("rbind", msa)
128
+
129
+    p <- p + geom_rect(data=msa.df, aes(x=xmin, y=ymin, 
130
+                           xmin=xmin, xmax=xmax,
131
+                           ymin=ymin, ymax=ymax, fill=seq)) +
132
+                               scale_fill_manual(values=color)
133
+
134
+    breaks <- hist(seq_along(slice), breaks=10, plot=FALSE)$breaks
135
+    pos <- start + breaks * width
136
+    mapping <- data.frame(from=breaks+1, to=pos)
137
+    attr(p, "mapping") <- mapping
138
+    
139
+    return(p)
140
+}
141
+
142
+##' scale x for tree with heatmap
143
+##'
144
+##' 
145
+##' @title scale_x_ggtree
146
+##' @param tree_view tree view
147
+##' @param breaks breaks for tree
148
+##' @param labels lables for corresponding breaks
149
+##' @return tree view
150
+##' @importFrom ggplot2 scale_x_continuous
151
+##' @importFrom ggplot2 scale_x_date
152
+##' @export
153
+##' @author Guangchuang Yu
154
+scale_x_ggtree <- function(tree_view, breaks=NULL, labels=NULL) {
155
+    p <- get_tree_view(tree_view)
156
+
157
+    mrsd <- get("mrsd", envir=tree_view$plot_env)
158
+    if (!is.null(mrsd) && class(p$data$x) == "Date") {
159
+        x <- Date2decimal(p$data$x)
160
+    } else {
161
+        x <- p$data$x
162
+    }
163
+
164
+    if (is.null(breaks)) {
165
+        breaks <- hist(x, breaks=5, plot=FALSE)$breaks
166
+    }
167
+    m <- attr(p, "mapping")
168
+
169
+    if (!is.null(mrsd) &&class(m$to) == "Date") {
170
+        to <- Date2decimal(m$to)
171
+    } else {
172
+        to <- m$to
173
+    }
174
+    
175
+    idx <- which(sapply(breaks, function(x) any(x > m$to)))
176
+    if (length(idx)) {
177
+        breaks <- breaks[-idx]
178
+    }
179
+    
180
+    if (is.null(labels)) {
181
+        labels <- breaks
182
+    }
183
+    
184
+    breaks <- c(breaks, to)
185
+    labels <- c(labels, gsub("\\.", "", as.character(m$from)))
186
+
187
+    if (!is.null(mrsd) && class(p$data$x) == "Date") {
188
+        p <- p + scale_x_date(breaks=decimal2Date(breaks), labels)
189
+    } else {
190
+        p <- p + scale_x_continuous(breaks=breaks, labels=labels)
191
+    }
192
+    return(p)    
193
+}
194
+
195
+
196
+
197
+## ##' view tree and associated matrix
198
+## ##'
199
+## ##' @title gplot
200
+## ##' @param p tree view
201
+## ##' @param data matrix
202
+## ##' @param low low color
203
+## ##' @param high high color
204
+## ##' @param widths widths of sub plot
205
+## ##' @param color color
206
+## ##' @param font.size font size
207
+## ##' @return list of figure
208
+## ##' @importFrom gridExtra grid.arrange
209
+## ##' @importFrom ggplot2 scale_x_continuous
210
+## ##' @importFrom ggplot2 scale_y_continuous
211
+## ##' @export
212
+## ##' @author Guangchuang Yu \url{http://ygc.name}
213
+## ##' @examples
214
+## ##' nwk <- system.file("extdata", "sample.nwk", package="ggtree")
215
+## ##' tree <- read.tree(nwk)
216
+## ##' p <- ggtree(tree)
217
+## ##' d <- matrix(abs(rnorm(52)), ncol=4)
218
+## ##' rownames(d) <- tree$tip.label
219
+## ##' colnames(d) <- paste0("G", 1:4)
220
+## ##' gplot(p, d, low="green", high="red")
221
+## gplot <- function(p, data, low="green", high="red", widths=c(0.5, 0.5), color="white", font.size=14) {
222
+##     ## p <- p + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0.6))
223
+##     p1 <- p + scale_y_continuous(expand = c(0, 0.6))
224
+##     ## p1 <- p + theme(panel.margin=unit(0, "null"))
225
+##     ## p1 <- p1 + theme(plot.margin = unit(c(1, -1, 1.5, 1), "lines"))
226
+##     p2 <- gplot.heatmap(p, data, low, high, color, font.size)
227
+##     grid.arrange(p1, p2, ncol=2, widths=widths)
228
+##     invisible(list(p1=p1, p2=p2))
229
+## }
230
+
231
+
232
+## ##' @importFrom grid unit
233
+## ##' @importFrom ggplot2 scale_fill_gradient
234
+## ##' @importFrom ggplot2 scale_fill_discrete
235
+## ##' @importFrom ggplot2 element_text
236
+## ##' @importFrom ggplot2 geom_tile
237
+## ##' @importFrom ggplot2 labs
238
+## ##' @importFrom ggplot2 guides
239
+## ##' @importFrom ggplot2 guide_legend
240
+## ##' @importFrom reshape2 melt