Browse code

Commit made by the Bioconductor Git-SVN bridge.

Commit id: be8421a1e7f3fa220c129ffad6e504004c9c2a8d

update vignette


Commit id: c7eea4289c6eba9f4b8b9ebd94f43434f72a95c3

gheatmap


Commit id: 9ab91767cb73fcf158295ce393a6a3c10c2974bc

add_legend



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

Guangchuang Yu authored on 18/05/2015 11:18:46
Showing 10 changed files

... ...
@@ -1,7 +1,7 @@
1 1
 Package: ggtree
2 2
 Type: Package
3 3
 Title: a phylogenetic tree viewer for different types of tree annotations
4
-Version: 1.1.5
4
+Version: 1.1.6
5 5
 Author: Guangchuang Yu and Tommy Tsan-Yuk Lam
6 6
 Maintainer: Guangchuang Yu <guangchuangyu@gmail.com>
7 7
 Description: ggtree extends the ggplot2 plotting system which implemented the
... ...
@@ -15,6 +15,7 @@ export("%<+%")
15 15
 export("%>%")
16 16
 export(.)
17 17
 export(add_colorbar)
18
+export(add_legend)
18 19
 export(aes)
19 20
 export(as.binary)
20 21
 export(collapse)
... ...
@@ -40,6 +41,7 @@ export(getNodeNum)
40 41
 export(getRoot)
41 42
 export(ggplotGrob)
42 43
 export(ggtree)
44
+export(gheatmap)
43 45
 export(gplot)
44 46
 export(groupClade)
45 47
 export(groupOTU)
... ...
@@ -59,6 +61,7 @@ export(read.tree)
59 61
 export(rtree)
60 62
 export(scaleClade)
61 63
 export(scale_color)
64
+export(scale_x_heatmap)
62 65
 export(theme_tree)
63 66
 export(theme_tree2)
64 67
 export(write.jplace)
... ...
@@ -1,3 +1,9 @@
1
+CHANGES IN VERSION 1.1.6
2
+------------------------
3
+ o add example of add_legend and gheatmap in vignette <2015-05-18, Mon> 
4
+ o gheatmap implementation of gplot <2015-05-18, Mon>
5
+ o add_legend for adding evolution distance legend <2015-05-18, Mon>
6
+ 
1 7
 CHANGES IN VERSION 1.1.5
2 8
 ------------------------
3 9
  o implement scaleClade <2015-05-12, Tue>
... ...
@@ -488,3 +488,41 @@ add_colorbar <- function(p, color, x=NULL, ymin=NULL, ymax=NULL, font.size=4) {
488 488
                  ymax = ymax, fill=legend[,2], color=legend[,2]) 
489 489
     
490 490
 }
491
+
492
+##' add evolution distance legend
493
+##'
494
+##' 
495
+##' @title add_legend
496
+##' @param p tree view
497
+##' @param x x position
498
+##' @param y y position
499
+##' @param offset offset of text and line
500
+##' @param font.size font size
501
+##' @param ... additional parameter
502
+##' @return tree view
503
+##' @export
504
+##' @author Guangchuang Yu
505
+add_legend <- function(p, x=NULL, y=NULL, offset=NULL, font.size=4, ...) {
506
+    if (is.null(x)) {
507
+        x <- min(p$data$x)
508
+    }
509
+    if (is.null(y)) {
510
+        y <- -0.5
511
+    }
512
+
513
+    d <- p$data$x %>% range %>% diff
514
+    d <- d/20 
515
+    n <- 0
516
+    while (d < 1) {
517
+        d <- d*10
518
+        n <- n + 1
519
+    }
520
+    d <- floor(d)/(10^n)
521
+    if (is.null(offset)) {
522
+        offset <- p$data$y %>% range %>% diff
523
+        offset <- offset / 100
524
+    }
525
+    p <- p + geom_segment(x=x, y=y, xend=x+d, yend=y, ...) +
526
+        geom_text(x=x+d/2, y=y+offset, label=d, size=font.size, ...)
527
+    return(p)
528
+}
... ...
@@ -1,3 +1,86 @@
1
+##' append a heatmap of a matrix to right side of phylogenetic tree
2
+##'
3
+##' 
4
+##' @title gheatmap
5
+##' @param p tree view
6
+##' @param data matrix or data.frame
7
+##' @param offset offset of heatmap to tree
8
+##' @param width width of each cell in heatmap
9
+##' @param low color of lowest value
10
+##' @param high color of highest value
11
+##' @param color color of heatmap cell border
12
+##' @param colnames logical, add matrix colnames or not
13
+##' @param font.size font size of matrix colnames
14
+##' @return tree view
15
+##' @importFrom reshape2 melt
16
+##' @importFrom ggplot2 geom_tile
17
+##' @importFrom ggplot2 geom_text
18
+##' @export
19
+##' @author Guangchuang Yu
20
+gheatmap <- function(p, data, offset=0, width=NULL, low="green", high="red",
21
+                     color="white", colnames=TRUE, font.size=4) {
22
+    if (is.null(width)) {
23
+        width <- (p$data$x %>% range %>% diff)/30
24
+    }
25
+    
26
+    isTip <- x <- y <- variable <- value <- from <- to <- NULL
27
+ 
28
+    df=p$data
29
+    df=df[df$isTip,]
30
+    start <- max(df$x) + offset
31
+
32
+    dd <- data[df$label[order(df$y)],]
33
+    dd$y <- sort(df$y)
34
+
35
+    dd$lab <- rownames(dd)
36
+    dd <- melt(dd, id=c("lab", "y"))
37
+    
38
+    if (any(dd$value == "")) {
39
+        dd$value[dd$value == ""] <- NA
40
+    }
41
+
42
+    V2 <- start + as.numeric(dd$variable) * width
43
+    mapping <- data.frame(from=dd$variable, to=V2)
44
+    mapping <- unique(mapping)
45
+
46
+    dd$x <- V2
47
+
48
+    p2 <- p + geom_tile(data=dd, aes(x, y, fill=value), color=color)
49
+
50
+    if (is(dd$value,"numeric")) {
51
+        p2 <- p2 + scale_fill_gradient(low=low, high=high, na.value="white")
52
+    } else {
53
+        p2 <- p2 + scale_fill_discrete(na.value="white")
54
+    }
55
+    
56
+    if (colnames) {
57
+        p2 <- p2 + geom_text(data=mapping, aes(x=to, label=from), y=0, size=font.size)
58
+    }
59
+    
60
+    attr(p2, "mapping") <- mapping
61
+    return(p2)
62
+}
63
+
64
+##' scale x for tree with heatmap
65
+##'
66
+##' 
67
+##' @title scale_x_heatmap
68
+##' @param p tree view
69
+##' @param breaks breaks for tree
70
+##' @param labels lables for corresponding breaks
71
+##' @return tree view
72
+##' @importFrom ggplot2 scale_x_continuous
73
+##' @export
74
+##' @author Guangchuang Yu
75
+scale_x_heatmap <- function(p, breaks, labels=NULL) {
76
+    m <- attr(p, "mapping")
77
+    if (is.null(labels)) {
78
+        labels <- breaks
79
+    }
80
+    p + scale_x_continuous(breaks=c(breaks, m$to), labels=c(labels, as.character(m$from)))
81
+}
82
+
83
+
1 84
 ##' view tree and associated matrix
2 85
 ##'
3 86
 ##' @title gplot
... ...
@@ -6,6 +89,7 @@
6 89
 ##' @param low low color
7 90
 ##' @param high high color
8 91
 ##' @param widths widths of sub plot
92
+##' @param color color
9 93
 ##' @param font.size font size
10 94
 ##' @return list of figure
11 95
 ##' @importFrom gridExtra grid.arrange
... ...
@@ -21,12 +105,12 @@
21 105
 ##' rownames(d) <- tree$tip.label
22 106
 ##' colnames(d) <- paste0("G", 1:4)
23 107
 ##' gplot(p, d, low="green", high="red")
24
-gplot <- function(p, data, low="green", high="red", widths=c(0.5, 0.5), font.size=14) {
108
+gplot <- function(p, data, low="green", high="red", widths=c(0.5, 0.5), color="white", font.size=14) {
25 109
     ## p <- p + scale_x_continuous(expand = c(0, 0)) + scale_y_continuous(expand = c(0, 0.6))
26 110
     p1 <- p + scale_y_continuous(expand = c(0, 0.6))
27 111
     ## p1 <- p + theme(panel.margin=unit(0, "null"))
28 112
     ## p1 <- p1 + theme(plot.margin = unit(c(1, -1, 1.5, 1), "lines"))
29
-    p2 <- gplot.heatmap(p, data, low, high, font.size)
113
+    p2 <- gplot.heatmap(p, data, low, high, color, font.size)
30 114
     grid.arrange(p1, p2, ncol=2, widths=widths)
31 115
     invisible(list(p1=p1, p2=p2))
32 116
 }
... ...
@@ -41,7 +125,7 @@ gplot <- function(p, data, low="green", high="red", widths=c(0.5, 0.5), font.siz
41 125
 ##' @importFrom ggplot2 guides
42 126
 ##' @importFrom ggplot2 guide_legend
43 127
 ##' @importFrom reshape2 melt
44
-gplot.heatmap <- function(p, data, low, high, font.size) {
128
+gplot.heatmap <- function(p, data, low, high, color="white", font.size) {
45 129
     isTip <- x <- Var1 <- Var2 <- value <- NULL
46 130
     dd=melt(as.matrix(data))
47 131
     ## p <- ggtree(tree) ## + theme_tree2()
... ...
@@ -55,7 +139,7 @@ gplot.heatmap <- function(p, data, low, high, font.size) {
55 139
         dd$value[dd$value == ""] <- NA
56 140
     }
57 141
     
58
-    p2 <- ggplot(dd, aes(Var2, Var1, fill=value))+geom_tile(color="black")
142
+    p2 <- ggplot(dd, aes(Var2, Var1, fill=value))+geom_tile(color=color)
59 143
     if (is(dd$value,"numeric")) {
60 144
         p2 <- p2 + scale_fill_gradient(low=low, high=high, na.value="white")
61 145
     } else {
62 146
new file mode 100644
... ...
@@ -0,0 +1,31 @@
1
+% Generated by roxygen2 (4.1.1): do not edit by hand
2
+% Please edit documentation in R/ggtree.R
3
+\name{add_legend}
4
+\alias{add_legend}
5
+\title{add_legend}
6
+\usage{
7
+add_legend(p, x = NULL, y = NULL, offset = NULL, font.size = 4, ...)
8
+}
9
+\arguments{
10
+\item{p}{tree view}
11
+
12
+\item{x}{x position}
13
+
14
+\item{y}{y position}
15
+
16
+\item{offset}{offset of text and line}
17
+
18
+\item{font.size}{font size}
19
+
20
+\item{...}{additional parameter}
21
+}
22
+\value{
23
+tree view
24
+}
25
+\description{
26
+add evolution distance legend
27
+}
28
+\author{
29
+Guangchuang Yu
30
+}
31
+
0 32
new file mode 100644
... ...
@@ -0,0 +1,38 @@
1
+% Generated by roxygen2 (4.1.1): do not edit by hand
2
+% Please edit documentation in R/gplot.R
3
+\name{gheatmap}
4
+\alias{gheatmap}
5
+\title{gheatmap}
6
+\usage{
7
+gheatmap(p, data, offset = 0, width, low = "green", high = "red",
8
+  color = "white", colnames = TRUE, font.size = 4)
9
+}
10
+\arguments{
11
+\item{p}{tree view}
12
+
13
+\item{data}{matrix or data.frame}
14
+
15
+\item{offset}{offset of heatmap to tree}
16
+
17
+\item{width}{width of each cell in heatmap}
18
+
19
+\item{low}{color of lowest value}
20
+
21
+\item{high}{color of highest value}
22
+
23
+\item{color}{color of heatmap cell border}
24
+
25
+\item{colnames}{logical, add matrix colnames or not}
26
+
27
+\item{font.size}{font size of matrix colnames}
28
+}
29
+\value{
30
+tree view
31
+}
32
+\description{
33
+append a heatmap of a matrix to right side of phylogenetic tree
34
+}
35
+\author{
36
+Guangchuang Yu
37
+}
38
+
... ...
@@ -5,7 +5,7 @@
5 5
 \title{gplot}
6 6
 \usage{
7 7
 gplot(p, data, low = "green", high = "red", widths = c(0.5, 0.5),
8
-  font.size = 14)
8
+  color = "white", font.size = 14)
9 9
 }
10 10
 \arguments{
11 11
 \item{p}{tree view}
... ...
@@ -18,6 +18,8 @@ gplot(p, data, low = "green", high = "red", widths = c(0.5, 0.5),
18 18
 
19 19
 \item{widths}{widths of sub plot}
20 20
 
21
+\item{color}{color}
22
+
21 23
 \item{font.size}{font size}
22 24
 }
23 25
 \value{
24 26
new file mode 100644
... ...
@@ -0,0 +1,25 @@
1
+% Generated by roxygen2 (4.1.1): do not edit by hand
2
+% Please edit documentation in R/gplot.R
3
+\name{scale_x_heatmap}
4
+\alias{scale_x_heatmap}
5
+\title{scale_x_heatmap}
6
+\usage{
7
+scale_x_heatmap(p, breaks, labels = NULL)
8
+}
9
+\arguments{
10
+\item{p}{tree view}
11
+
12
+\item{breaks}{breaks for tree}
13
+
14
+\item{labels}{lables for corresponding breaks}
15
+}
16
+\value{
17
+tree view
18
+}
19
+\description{
20
+scale x for tree with heatmap
21
+}
22
+\author{
23
+Guangchuang Yu
24
+}
25
+
... ...
@@ -141,7 +141,15 @@ User can use _`ggtree(object)`_ command to view the phylogenetic tree directly,
141 141
  
142 142
 ## display evolution distance
143 143
 
144
-To show evolution distance, we can use `theme_tree2()` or `ggtree(showDistance=TRUE)`
144
+To show evolution distance, user can use `add_legend` function.
145
+
146
+```{r fig.width=3, fig.height=3, fig.align="center"}
147
+ggtree(tree) %>% add_legend(x=0, y=10, offset=0.5, font.size=3)
148
+```
149
+
150
+User can specific the position and offset of distance from text to line segment.
151
+
152
+We can also use `theme_tree2()` or `ggtree(showDistance=TRUE)`
145 153
 
146 154
 ```{r fig.width=3, fig.height=3, fig.align="center"}
147 155
 ggtree(tree) + theme_tree2()
... ...
@@ -155,12 +163,6 @@ ggtree(tree, showDistance=TRUE) +
155 163
       scale_x_continuous(breaks=seq(0, 60, 5))
156 164
 ```
157 165
 
158
-User can also use _`geom_segment`_ and _`geom_text`_ to specify the position and length of distance legend:
159
-```{r fig.width=3, fig.height=3, warning=FALSE, fig.align="center"}
160
-ggtree(tree) +
161
-    geom_segment(x=0, y=12, xend=10, yend=12) +
162
-        geom_text(x=5, y=12.5, label=5, size=4)
163
-```
164 166
 
165 167
 ## display nodes/tips
166 168
 
... ...
@@ -748,6 +750,30 @@ p <- ggtree(beast_tree, time_scale=TRUE)+geom_tiplab(align=TRUE)+ xlim(1990, 201
748 750
 gplot(p, genotype, widths=c(5, 1))
749 751
 ```
750 752
 
753
+`gplot` function has [an issue](https://github.com/GuangchuangYu/ggtree/issues/3) that it can't always guarantee the heatmpa aligning to the tree properly, since the line up is between two figures and it's currently not supported internally by ggplot2. I have implemented another function `gheatmap` that can do the line up properly by creating a new layer above the tree.
754
+
755
+```{r fig.width=20, fig.height=16, fig.align="center"}
756
+p <- ggtree(beast_tree, time_scale=TRUE) %>% add_legend
757
+gheatmap(p, genotype, width=1)
758
+```
759
+
760
+The _width_ parameter is to control the width of each cell in the heatmap. It supports another parameter _offset_ for controling the distance between the tree and the heatmap, for instance left space for tip labels.
761
+
762
+```{r fig.width=20, fig.height=16, fig.align="center"}
763
+gheatmap(p+geom_tiplab(size=3), genotype, offset = 2, width=0.5)
764
+```
765
+
766
+For time scaled tree, as in this example, it's more often to use x axis by using `theme_tree2`. But with this solution, the heatmap is just another layer and will change the x axis. To over come this issue, we implemented `scale_x_heatmap` to set the x axis more reasonable. User can also use `gplot` and tweak the positions of two plot to align properly.
767
+
768
+```{r fig.width=20, fig.height=16, fig.align="center"}
769
+p <- ggtree(beast_tree, time_scale=TRUE) + geom_tiplab(size=3, align=TRUE) + theme_tree2()
770
+pp <- (p + scale_y_continuous(expand=c(0, 0.3))) %>%
771
+    gheatmap(genotype, offset=4, width=0.5, colnames=FALSE) %>%
772
+        scale_x_heatmap(breaks=seq(1992, 2013, by=5))
773
+pp + theme(legend.position="right")
774
+```
775
+
776
+
751 777
 
752 778
 # External documents
753 779