... | ... |
@@ -116,6 +116,8 @@ nodebar <- function(data, cols, color, alpha=1, position="stack") { |
116 | 116 |
##' @param cols columns of the data.frame that store the stats |
117 | 117 |
##' @param color set color of bars |
118 | 118 |
##' @param alpha set transparency of the charts |
119 |
+##' @param outline.color color of outline |
|
120 |
+##' @param outline.size size of outline |
|
119 | 121 |
##' @return list of ggplot objects |
120 | 122 |
##' @export |
121 | 123 |
##' @author Guangchuang Yu |
... | ... |
@@ -119,7 +119,7 @@ nodebar <- function(data, cols, color, alpha=1, position="stack") { |
119 | 119 |
##' @return list of ggplot objects |
120 | 120 |
##' @export |
121 | 121 |
##' @author Guangchuang Yu |
122 |
-nodepie <- function(data, cols, color, alpha=1) { |
|
122 |
+nodepie <- function(data, cols, color, alpha=1, outline.color="transparent", outline.size=0) { |
|
123 | 123 |
if (! "node" %in% colnames(data)) { |
124 | 124 |
stop("data should have a column 'node'...") |
125 | 125 |
} |
... | ... |
@@ -128,14 +128,14 @@ nodepie <- function(data, cols, color, alpha=1) { |
128 | 128 |
color <- NA |
129 | 129 |
} |
130 | 130 |
ldf <- gather(data, type, value, !! cols) %>% split(., .$node) |
131 |
- lapply(ldf, function(df) ggpie(df, y=~value, fill=~type, color, alpha)) |
|
131 |
+ lapply(ldf, function(df) ggpie(df, y=~value, fill=~type, color, alpha, outline.color, outline.size)) |
|
132 | 132 |
} |
133 | 133 |
|
134 | 134 |
|
135 | 135 |
##' @importFrom methods missingArg |
136 |
-ggpie <- function(data, y, fill, color, alpha=1) { |
|
136 |
+ggpie <- function(data, y, fill, color, alpha=1, outline.color="transparent", outline.size=0) { |
|
137 | 137 |
p <- ggplot(data, aes_(x=1, y=y, fill=fill)) + |
138 |
- geom_bar(stat='identity', alpha=alpha) + |
|
138 |
+ geom_bar(stat='identity', alpha=alpha, color=outline.color, size=outline.size, show.legend = F) + |
|
139 | 139 |
coord_polar(theta='y') + theme_inset() |
140 | 140 |
|
141 | 141 |
if (missingArg(color) || is.null(color) || is.na(color)) { |
... | ... |
@@ -22,8 +22,6 @@ |
22 | 22 |
##' For demonstration of this function, please refer to chapter 8.3 of |
23 | 23 |
##' *Data Integration, Manipulation and Visualization of Phylogenetic Trees* |
24 | 24 |
##' <http://yulab-smu.top/treedata-book/index.html> by Guangchuang Yu. |
25 |
-##' |
|
26 |
- |
|
27 | 25 |
geom_inset <- function(insets, width = .1, height = .1, hjust = 0, vjust = 0, |
28 | 26 |
x = "node", reverse_x = FALSE, reverse_y = FALSE) { |
29 | 27 |
structure(list(insets = insets, width = width, height = height, |
... | ... |
@@ -37,7 +35,6 @@ geom_inset <- function(insets, width = .1, height = .1, hjust = 0, vjust = 0, |
37 | 35 |
##' @title inset |
38 | 36 |
##' @rdname inset |
39 | 37 |
##' @param tree_view tree view |
40 |
-##' @inheritParams geom_inset |
|
41 | 38 |
##' @return tree view with insets |
42 | 39 |
##' @importFrom yulab.utils get_fun_from_pkg |
43 | 40 |
##' @export |
... | ... |
@@ -1,19 +1,29 @@ |
1 |
-##' add subplots to tree |
|
2 |
-##' |
|
1 |
+##' gemo_inset can add subplots to tree by accepting a list of ggplot objects that are ancestral |
|
2 |
+##' stats or data associated with selected nodes in the tree. These ggplot objects can be any |
|
3 |
+##' kind of charts or hybrid of of these charts. |
|
4 |
+##' |
|
5 |
+##' Users can also use |
|
6 |
+##' |
|
3 | 7 |
##' |
4 | 8 |
##' @title geom_inset |
5 | 9 |
##' @rdname inset |
6 | 10 |
##' @param insets a list of ggplot objects, named by node number |
7 |
-##' @param width width of inset, relative to the range of x-axis |
|
8 |
-##' @param height height of inset, relative to the range of y-axis |
|
9 |
-##' @param hjust horizontal adjustment |
|
10 |
-##' @param vjust vertical adjustment |
|
11 |
-##' @param x x position, one of 'node' and 'branch' |
|
12 |
-##' @param reverse_x whether x axis was reversed by scale_x_reverse |
|
13 |
-##' @param reverse_y whether y axis was reversed by scale_y_reverse |
|
11 |
+##' @param width width of the inset, relative to the range of x-axis, defaults to .1 |
|
12 |
+##' @param height height of the inset, relative to the range of y-axis, defaults to .1 |
|
13 |
+##' @param hjust adjust the horizontal position of the charts, charts will go left if hjust > 0 |
|
14 |
+##' @param vjust adjust the vertical position of the charts, charts will go down if vjust > 0 |
|
15 |
+##' @param x the position where users want to place the charts, one of 'node' (default) and 'branch' |
|
16 |
+##' @param reverse_x whether to reverse x axis of the charts by 'ggplot2::scale_x_reverse', defaults to 'FALSE' |
|
17 |
+##' @param reverse_y whether to reverse y axis of the charts by 'ggplot2::scale_y_reverse', defaults to 'FALSE' |
|
14 | 18 |
##' @return inset layer |
15 | 19 |
##' @export |
16 | 20 |
##' @author Guangchuang Yu |
21 |
+##' @references |
|
22 |
+##' For demonstration of this function, please refer to chapter 8.3 of |
|
23 |
+##' *Data Integration, Manipulation and Visualization of Phylogenetic Trees* |
|
24 |
+##' <http://yulab-smu.top/treedata-book/index.html> by Guangchuang Yu. |
|
25 |
+##' |
|
26 |
+ |
|
17 | 27 |
geom_inset <- function(insets, width = .1, height = .1, hjust = 0, vjust = 0, |
18 | 28 |
x = "node", reverse_x = FALSE, reverse_y = FALSE) { |
19 | 29 |
structure(list(insets = insets, width = width, height = height, |
... | ... |
@@ -21,13 +31,13 @@ geom_inset <- function(insets, width = .1, height = .1, hjust = 0, vjust = 0, |
21 | 31 |
reverse_x = reverse_x, reverse_y = reverse_y), class = "tree_inset") |
22 | 32 |
} |
23 | 33 |
|
24 |
-##' add insets in a tree |
|
34 |
+##' add subplots as insets in a tree |
|
25 | 35 |
##' |
26 | 36 |
##' |
27 | 37 |
##' @title inset |
28 | 38 |
##' @rdname inset |
29 |
-##' @param tree_view tree view |
|
30 |
-## @inheritParams geom_inset |
|
39 |
+##' @param tree_view tree view |
|
40 |
+##' @inheritParams geom_inset |
|
31 | 41 |
##' @return tree view with insets |
32 | 42 |
##' @importFrom yulab.utils get_fun_from_pkg |
33 | 43 |
##' @export |
... | ... |
@@ -74,7 +84,7 @@ inset <- function(tree_view, insets, width, height, hjust=0, vjust=0, |
74 | 84 |
##' |
75 | 85 |
##' |
76 | 86 |
##' @title nodebar |
77 |
-##' @param position position of bar, one of 'stack' and 'dodge' |
|
87 |
+##' @param position position of bars, if 'stack' (default) make bars stacked atop one another, 'dodge' make them dodged side-to-side |
|
78 | 88 |
##' @inheritParams nodepie |
79 | 89 |
##' @return list of ggplot objects |
80 | 90 |
##' @export |
... | ... |
@@ -105,10 +115,10 @@ nodebar <- function(data, cols, color, alpha=1, position="stack") { |
105 | 115 |
##' |
106 | 116 |
##' |
107 | 117 |
##' @title nodepie |
108 |
-##' @param data a data.frame of stats with an additional column of node number |
|
109 |
-##' @param cols column of stats |
|
110 |
-##' @param color color of bar |
|
111 |
-##' @param alpha alpha |
|
118 |
+##' @param data a data.frame of stats with an additional column of node number named "node" |
|
119 |
+##' @param cols columns of the data.frame that store the stats |
|
120 |
+##' @param color set color of bars |
|
121 |
+##' @param alpha set transparency of the charts |
|
112 | 122 |
##' @return list of ggplot objects |
113 | 123 |
##' @export |
114 | 124 |
##' @author Guangchuang Yu |
... | ... |
@@ -29,7 +29,7 @@ geom_inset <- function(insets, width = .1, height = .1, hjust = 0, vjust = 0, |
29 | 29 |
##' @param tree_view tree view |
30 | 30 |
## @inheritParams geom_inset |
31 | 31 |
##' @return tree view with insets |
32 |
-##' @importFrom rvcheck get_fun_from_pkg |
|
32 |
+##' @importFrom yulab.utils get_fun_from_pkg |
|
33 | 33 |
##' @export |
34 | 34 |
##' @author Guangchuang Yu |
35 | 35 |
inset <- function(tree_view, insets, width, height, hjust=0, vjust=0, |
... | ... |
@@ -58,8 +58,8 @@ inset <- function(tree_view, insets, width, height, hjust=0, vjust=0, |
58 | 58 |
if (reverse_y) |
59 | 59 |
yy <- -yy |
60 | 60 |
|
61 |
- width <- width * diff(range(tree_view$data$x)) |
|
62 |
- height <- height * diff(range(tree_view$data$y)) |
|
61 |
+ width <- width * diff(range(tree_view$data$x, na.rm = TRUE)) |
|
62 |
+ height <- height * diff(range(tree_view$data$y, na.rm = TRUE)) |
|
63 | 63 |
|
64 | 64 |
geom_subview <- get_fun_from_pkg("ggimage", "geom_subview") |
65 | 65 |
|
... | ... |
@@ -139,7 +139,3 @@ ggpie <- function(data, y, fill, color, alpha=1) { |
139 | 139 |
return(p) |
140 | 140 |
} |
141 | 141 |
|
142 |
- |
|
143 |
- |
|
144 |
- |
|
145 |
- |
... | ... |
@@ -58,8 +58,8 @@ inset <- function(tree_view, insets, width, height, hjust=0, vjust=0, |
58 | 58 |
if (reverse_y) |
59 | 59 |
yy <- -yy |
60 | 60 |
|
61 |
- width <- width * diff(range(xx)) |
|
62 |
- height <- height * diff(range(yy)) |
|
61 |
+ width <- width * diff(range(tree_view$data$x)) |
|
62 |
+ height <- height * diff(range(tree_view$data$y)) |
|
63 | 63 |
|
64 | 64 |
geom_subview <- get_fun_from_pkg("ggimage", "geom_subview") |
65 | 65 |
|
... | ... |
@@ -27,7 +27,7 @@ geom_inset <- function(insets, width = .1, height = .1, hjust = 0, vjust = 0, |
27 | 27 |
##' @title inset |
28 | 28 |
##' @rdname inset |
29 | 29 |
##' @param tree_view tree view |
30 |
-##' @inheritParams geom_inset |
|
30 |
+## @inheritParams geom_inset |
|
31 | 31 |
##' @return tree view with insets |
32 | 32 |
##' @importFrom rvcheck get_fun_from_pkg |
33 | 33 |
##' @export |
... | ... |
@@ -1,16 +1,33 @@ |
1 |
-##' add insets in a tree |
|
2 |
-##' |
|
1 |
+##' add subplots to tree |
|
3 | 2 |
##' |
4 |
-##' @title inset |
|
5 |
-##' @param tree_view tree view |
|
3 |
+##' |
|
4 |
+##' @title geom_inset |
|
5 |
+##' @rdname inset |
|
6 | 6 |
##' @param insets a list of ggplot objects, named by node number |
7 |
-##' @param width width of inset |
|
8 |
-##' @param height height of inset |
|
7 |
+##' @param width width of inset, relative to the range of x-axis |
|
8 |
+##' @param height height of inset, relative to the range of y-axis |
|
9 | 9 |
##' @param hjust horizontal adjustment |
10 | 10 |
##' @param vjust vertical adjustment |
11 | 11 |
##' @param x x position, one of 'node' and 'branch' |
12 | 12 |
##' @param reverse_x whether x axis was reversed by scale_x_reverse |
13 | 13 |
##' @param reverse_y whether y axis was reversed by scale_y_reverse |
14 |
+##' @return inset layer |
|
15 |
+##' @export |
|
16 |
+##' @author Guangchuang Yu |
|
17 |
+geom_inset <- function(insets, width = .1, height = .1, hjust = 0, vjust = 0, |
|
18 |
+ x = "node", reverse_x = FALSE, reverse_y = FALSE) { |
|
19 |
+ structure(list(insets = insets, width = width, height = height, |
|
20 |
+ hjust = hjust, vjust = vjust, x = x, |
|
21 |
+ reverse_x = reverse_x, reverse_y = reverse_y), class = "tree_inset") |
|
22 |
+} |
|
23 |
+ |
|
24 |
+##' add insets in a tree |
|
25 |
+##' |
|
26 |
+##' |
|
27 |
+##' @title inset |
|
28 |
+##' @rdname inset |
|
29 |
+##' @param tree_view tree view |
|
30 |
+##' @inheritParams geom_inset |
|
14 | 31 |
##' @return tree view with insets |
15 | 32 |
##' @importFrom rvcheck get_fun_from_pkg |
16 | 33 |
##' @export |
... | ... |
@@ -18,6 +35,12 @@ |
18 | 35 |
inset <- function(tree_view, insets, width, height, hjust=0, vjust=0, |
19 | 36 |
x="node", reverse_x=FALSE, reverse_y=FALSE) { |
20 | 37 |
|
38 |
+ if(width < 0 || width > 1) |
|
39 |
+ stop("width should be in range of (0,1)") |
|
40 |
+ |
|
41 |
+ if(height < 0 || height > 1) |
|
42 |
+ stop("height should be in range of (0,1)") |
|
43 |
+ |
|
21 | 44 |
df <- tree_view$data[as.numeric(names(insets)),] |
22 | 45 |
x <- match.arg(x, c("node", "branch", "edge")) |
23 | 46 |
|
... | ... |
@@ -35,6 +58,9 @@ inset <- function(tree_view, insets, width, height, hjust=0, vjust=0, |
35 | 58 |
if (reverse_y) |
36 | 59 |
yy <- -yy |
37 | 60 |
|
61 |
+ width <- width * diff(range(xx)) |
|
62 |
+ height <- height * diff(range(yy)) |
|
63 |
+ |
|
38 | 64 |
geom_subview <- get_fun_from_pkg("ggimage", "geom_subview") |
39 | 65 |
|
40 | 66 |
tree_view + geom_subview(subview = insets, |
... | ... |
@@ -12,6 +12,7 @@ |
12 | 12 |
##' @param reverse_x whether x axis was reversed by scale_x_reverse |
13 | 13 |
##' @param reverse_y whether y axis was reversed by scale_y_reverse |
14 | 14 |
##' @return tree view with insets |
15 |
+##' @importFrom rvcheck get_fun_from_pkg |
|
15 | 16 |
##' @export |
16 | 17 |
##' @author Guangchuang Yu |
17 | 18 |
inset <- function(tree_view, insets, width, height, hjust=0, vjust=0, |
... | ... |
@@ -14,7 +14,7 @@ |
14 | 14 |
##' @return tree view with insets |
15 | 15 |
##' @export |
16 | 16 |
##' @author Guangchuang Yu |
17 |
-inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, |
|
17 |
+inset <- function(tree_view, insets, width, height, hjust=0, vjust=0, |
|
18 | 18 |
x="node", reverse_x=FALSE, reverse_y=FALSE) { |
19 | 19 |
|
20 | 20 |
df <- tree_view$data[as.numeric(names(insets)),] |
... | ... |
@@ -17,8 +17,6 @@ |
17 | 17 |
inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, |
18 | 18 |
x="node", reverse_x=FALSE, reverse_y=FALSE) { |
19 | 19 |
|
20 |
- message("The inset function will be defunct in next release, please use ggimage::geom_subview() instead.") |
|
21 |
- |
|
22 | 20 |
df <- tree_view$data[as.numeric(names(insets)),] |
23 | 21 |
x <- match.arg(x, c("node", "branch", "edge")) |
24 | 22 |
|
... | ... |
@@ -36,14 +36,13 @@ inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, |
36 | 36 |
if (reverse_y) |
37 | 37 |
yy <- -yy |
38 | 38 |
|
39 |
- for (i in seq_along(insets)) { |
|
40 |
- tree_view %<>% subview(insets[[i]], |
|
41 |
- x = xx[i], |
|
42 |
- y = yy[i], |
|
43 |
- width = width, |
|
44 |
- height = height) |
|
45 |
- } |
|
46 |
- return(tree_view) |
|
39 |
+ geom_subview <- get_fun_from_pkg("ggimage", "geom_subview") |
|
40 |
+ |
|
41 |
+ tree_view + geom_subview(subview = insets, |
|
42 |
+ width = width, |
|
43 |
+ height = height, |
|
44 |
+ x = xx, |
|
45 |
+ y = yy) |
|
47 | 46 |
} |
48 | 47 |
|
49 | 48 |
##' generate a list of bar charts for results of ancestral state reconstruction |
... | ... |
@@ -63,7 +63,7 @@ nodebar <- function(data, cols, color, alpha=1, position="stack") { |
63 | 63 |
} |
64 | 64 |
type <- value <- NULL |
65 | 65 |
|
66 |
- ldf <- gather(data, type, value, cols) %>% split(., .$node) |
|
66 |
+ ldf <- gather(data, type, value, !! cols) %>% split(., .$node) |
|
67 | 67 |
bars <- lapply(ldf, function(df) ggplot(df, aes_(x=1, y=~value, fill=~type)) + |
68 | 68 |
geom_bar(stat='identity', alpha=alpha, position=position) + |
69 | 69 |
theme_inset() |
... | ... |
@@ -96,7 +96,7 @@ nodepie <- function(data, cols, color, alpha=1) { |
96 | 96 |
if (missingArg(color)) { |
97 | 97 |
color <- NA |
98 | 98 |
} |
99 |
- ldf <- gather(data, type, value, cols) %>% split(., .$node) |
|
99 |
+ ldf <- gather(data, type, value, !! cols) %>% split(., .$node) |
|
100 | 100 |
lapply(ldf, function(df) ggpie(df, y=~value, fill=~type, color, alpha)) |
101 | 101 |
} |
102 | 102 |
|
... | ... |
@@ -17,6 +17,8 @@ |
17 | 17 |
inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, |
18 | 18 |
x="node", reverse_x=FALSE, reverse_y=FALSE) { |
19 | 19 |
|
20 |
+ message("The inset function will be defunct in next release, please use ggimage::geom_subview() instead.") |
|
21 |
+ |
|
20 | 22 |
df <- tree_view$data[as.numeric(names(insets)),] |
21 | 23 |
x <- match.arg(x, c("node", "branch", "edge")) |
22 | 24 |
|
... | ... |
@@ -9,10 +9,14 @@ |
9 | 9 |
##' @param hjust horizontal adjustment |
10 | 10 |
##' @param vjust vertical adjustment |
11 | 11 |
##' @param x x position, one of 'node' and 'branch' |
12 |
+##' @param reverse_x whether x axis was reversed by scale_x_reverse |
|
13 |
+##' @param reverse_y whether y axis was reversed by scale_y_reverse |
|
12 | 14 |
##' @return tree view with insets |
13 | 15 |
##' @export |
14 | 16 |
##' @author Guangchuang Yu |
15 |
-inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, x="node") { |
|
17 |
+inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, |
|
18 |
+ x="node", reverse_x=FALSE, reverse_y=FALSE) { |
|
19 |
+ |
|
16 | 20 |
df <- tree_view$data[as.numeric(names(insets)),] |
17 | 21 |
x <- match.arg(x, c("node", "branch", "edge")) |
18 | 22 |
|
... | ... |
@@ -25,6 +29,10 @@ inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, x= |
25 | 29 |
|
26 | 30 |
xx <- xx - hjust |
27 | 31 |
yy <- yy - vjust |
32 |
+ if (reverse_x) |
|
33 |
+ xx <- -xx |
|
34 |
+ if (reverse_y) |
|
35 |
+ yy <- -yy |
|
28 | 36 |
|
29 | 37 |
for (i in seq_along(insets)) { |
30 | 38 |
tree_view %<>% subview(insets[[i]], |
... | ... |
@@ -1,6 +1,6 @@ |
1 | 1 |
##' add insets in a tree |
2 | 2 |
##' |
3 |
-##' |
|
3 |
+##' |
|
4 | 4 |
##' @title inset |
5 | 5 |
##' @param tree_view tree view |
6 | 6 |
##' @param insets a list of ggplot objects, named by node number |
... | ... |
@@ -22,7 +22,7 @@ inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, x= |
22 | 22 |
xx <- df$branch |
23 | 23 |
} |
24 | 24 |
yy <- df$y |
25 |
- |
|
25 |
+ |
|
26 | 26 |
xx <- xx - hjust |
27 | 27 |
yy <- yy - vjust |
28 | 28 |
|
... | ... |
@@ -38,7 +38,7 @@ inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, x= |
38 | 38 |
|
39 | 39 |
##' generate a list of bar charts for results of ancestral state reconstruction |
40 | 40 |
##' |
41 |
-##' |
|
41 |
+##' |
|
42 | 42 |
##' @title nodebar |
43 | 43 |
##' @param position position of bar, one of 'stack' and 'dodge' |
44 | 44 |
##' @inheritParams nodepie |
... | ... |
@@ -52,7 +52,7 @@ nodebar <- function(data, cols, color, alpha=1, position="stack") { |
52 | 52 |
stop("data should have a column 'node'...") |
53 | 53 |
} |
54 | 54 |
type <- value <- NULL |
55 |
- |
|
55 |
+ |
|
56 | 56 |
ldf <- gather(data, type, value, cols) %>% split(., .$node) |
57 | 57 |
bars <- lapply(ldf, function(df) ggplot(df, aes_(x=1, y=~value, fill=~type)) + |
58 | 58 |
geom_bar(stat='identity', alpha=alpha, position=position) + |
... | ... |
@@ -69,7 +69,7 @@ nodebar <- function(data, cols, color, alpha=1, position="stack") { |
69 | 69 |
|
70 | 70 |
##' generate a list of pie charts for results of ancestral stat reconstruction |
71 | 71 |
##' |
72 |
-##' |
|
72 |
+##' |
|
73 | 73 |
##' @title nodepie |
74 | 74 |
##' @param data a data.frame of stats with an additional column of node number |
75 | 75 |
##' @param cols column of stats |
... | ... |
@@ -91,11 +91,12 @@ nodepie <- function(data, cols, color, alpha=1) { |
91 | 91 |
} |
92 | 92 |
|
93 | 93 |
|
94 |
+##' @importFrom methods missingArg |
|
94 | 95 |
ggpie <- function(data, y, fill, color, alpha=1) { |
95 | 96 |
p <- ggplot(data, aes_(x=1, y=y, fill=fill)) + |
96 | 97 |
geom_bar(stat='identity', alpha=alpha) + |
97 | 98 |
coord_polar(theta='y') + theme_inset() |
98 |
- |
|
99 |
+ |
|
99 | 100 |
if (missingArg(color) || is.null(color) || is.na(color)) { |
100 | 101 |
## do nothing |
101 | 102 |
} else { |
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@122021 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -12,7 +12,7 @@ |
12 | 12 |
##' @return tree view with insets |
13 | 13 |
##' @export |
14 | 14 |
##' @author Guangchuang Yu |
15 |
-inset <- function(tree_view, insets, width=0.05, height=0.05, hjust=0, vjust=0, x="node") { |
|
15 |
+inset <- function(tree_view, insets, width=0.1, height=0.1, hjust=0, vjust=0, x="node") { |
|
16 | 16 |
df <- tree_view$data[as.numeric(names(insets)),] |
17 | 17 |
x <- match.arg(x, c("node", "branch", "edge")) |
18 | 18 |
|
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@113922 bc3139a8-67e5-0310-9ffc-ced21a209358
... | ... |
@@ -13,7 +13,7 @@ |
13 | 13 |
##' @export |
14 | 14 |
##' @author Guangchuang Yu |
15 | 15 |
inset <- function(tree_view, insets, width=0.05, height=0.05, hjust=0, vjust=0, x="node") { |
16 |
- df <- tree_view$data[names(insets),] |
|
16 |
+ df <- tree_view$data[as.numeric(names(insets)),] |
|
17 | 17 |
x <- match.arg(x, c("node", "branch", "edge")) |
18 | 18 |
|
19 | 19 |
if (x == 'node') { |
git-svn-id: https://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks/ggtree@112112 bc3139a8-67e5-0310-9ffc-ced21a209358
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,110 @@ |
1 |
+##' add insets in a tree |
|
2 |
+##' |
|
3 |
+##' |
|
4 |
+##' @title inset |
|
5 |
+##' @param tree_view tree view |
|
6 |
+##' @param insets a list of ggplot objects, named by node number |
|
7 |
+##' @param width width of inset |
|
8 |
+##' @param height height of inset |
|
9 |
+##' @param hjust horizontal adjustment |
|
10 |
+##' @param vjust vertical adjustment |
|
11 |
+##' @param x x position, one of 'node' and 'branch' |
|
12 |
+##' @return tree view with insets |
|
13 |
+##' @export |
|
14 |
+##' @author Guangchuang Yu |
|
15 |
+inset <- function(tree_view, insets, width=0.05, height=0.05, hjust=0, vjust=0, x="node") { |
|
16 |
+ df <- tree_view$data[names(insets),] |
|
17 |
+ x <- match.arg(x, c("node", "branch", "edge")) |
|
18 |
+ |
|
19 |
+ if (x == 'node') { |
|
20 |
+ xx <- df$x |
|
21 |
+ } else { |
|
22 |
+ xx <- df$branch |
|
23 |
+ } |
|
24 |
+ yy <- df$y |
|
25 |
+ |
|
26 |
+ xx <- xx - hjust |
|
27 |
+ yy <- yy - vjust |
|
28 |
+ |
|
29 |
+ for (i in seq_along(insets)) { |
|
30 |
+ tree_view %<>% subview(insets[[i]], |
|
31 |
+ x = xx[i], |
|
32 |
+ y = yy[i], |
|
33 |
+ width = width, |
|
34 |
+ height = height) |
|
35 |
+ } |
|
36 |
+ return(tree_view) |
|
37 |
+} |
|
38 |
+ |
|
39 |
+##' generate a list of bar charts for results of ancestral state reconstruction |
|
40 |
+##' |
|
41 |
+##' |
|
42 |
+##' @title nodebar |
|
43 |
+##' @param position position of bar, one of 'stack' and 'dodge' |
|
44 |
+##' @inheritParams nodepie |
|
45 |
+##' @return list of ggplot objects |
|
46 |
+##' @export |
|
47 |
+##' @importFrom ggplot2 geom_bar |
|
48 |
+##' @importFrom tidyr gather |
|
49 |
+##' @author Guangchuang Yu |
|
50 |
+nodebar <- function(data, cols, color, alpha=1, position="stack") { |
|
51 |
+ if (! "node" %in% colnames(data)) { |
|
52 |
+ stop("data should have a column 'node'...") |
|
53 |
+ } |
|
54 |
+ type <- value <- NULL |
|
55 |
+ |
|
56 |
+ ldf <- gather(data, type, value, cols) %>% split(., .$node) |
|
57 |
+ bars <- lapply(ldf, function(df) ggplot(df, aes_(x=1, y=~value, fill=~type)) + |
|
58 |
+ geom_bar(stat='identity', alpha=alpha, position=position) + |
|
59 |
+ theme_inset() |
|
60 |
+ ) |
|
61 |
+ |
|
62 |
+ if (missingArg(color) || is.null(color) || is.na(color)) { |
|
63 |
+ ## do nothing |
|
64 |
+ } else { |
|
65 |
+ bars <- lapply(bars, function(p) p+scale_fill_manual(values=color)) |
|
66 |
+ } |
|
67 |
+ return(bars) |
|
68 |
+} |
|
69 |
+ |
|
70 |
+##' generate a list of pie charts for results of ancestral stat reconstruction |
|
71 |
+##' |
|
72 |
+##' |
|
73 |
+##' @title nodepie |
|
74 |
+##' @param data a data.frame of stats with an additional column of node number |
|
75 |
+##' @param cols column of stats |
|
76 |
+##' @param color color of bar |
|
77 |
+##' @param alpha alpha |
|
78 |
+##' @return list of ggplot objects |
|
79 |
+##' @export |
|
80 |
+##' @author Guangchuang Yu |
|
81 |
+nodepie <- function(data, cols, color, alpha=1) { |
|
82 |
+ if (! "node" %in% colnames(data)) { |
|
83 |
+ stop("data should have a column 'node'...") |
|
84 |
+ } |
|
85 |
+ type <- value <- NULL |
|
86 |
+ if (missingArg(color)) { |
|
87 |
+ color <- NA |
|
88 |
+ } |
|
89 |
+ ldf <- gather(data, type, value, cols) %>% split(., .$node) |
|
90 |
+ lapply(ldf, function(df) ggpie(df, y=~value, fill=~type, color, alpha)) |
|
91 |
+} |
|
92 |
+ |
|
93 |
+ |
|
94 |
+ggpie <- function(data, y, fill, color, alpha=1) { |
|
95 |
+ p <- ggplot(data, aes_(x=1, y=y, fill=fill)) + |
|
96 |
+ geom_bar(stat='identity', alpha=alpha) + |
|
97 |
+ coord_polar(theta='y') + theme_inset() |
|
98 |
+ |
|
99 |
+ if (missingArg(color) || is.null(color) || is.na(color)) { |
|
100 |
+ ## do nothing |
|
101 |
+ } else { |
|
102 |
+ p <- p+scale_fill_manual(values=color) |
|
103 |
+ } |
|
104 |
+ return(p) |
|
105 |
+} |
|
106 |
+ |
|
107 |
+ |
|
108 |
+ |
|
109 |
+ |
|
110 |
+ |