Browse code

inset with image files

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

g.yu authored on 23/02/2016 10:12:44
Showing 7 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.3.13
4
+Version: 1.3.14
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
... ...
@@ -1,3 +1,7 @@
1
+CHANGES IN VERSION 1.3.14
2
+------------------------
3
+ o subview and inset now supports annotating with img files <2016-02-23, Tue>
4
+ 
1 5
 CHANGES IN VERSION 1.3.13
2 6
 ------------------------
3 7
  o add example of rescale_tree function in treeAnnotation.Rmd <2016-02-07, Sun>
... ...
@@ -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') {
... ...
@@ -3,7 +3,7 @@
3 3
 ##' 
4 4
 ##' @title subview
5 5
 ##' @param mainview main view
6
-##' @param subview sub view
6
+##' @param subview a ggplot or grob object
7 7
 ##' @param x x position
8 8
 ##' @param y y position
9 9
 ##' @param width width of subview, [0,1]
... ...
@@ -20,11 +20,28 @@ subview <- function(mainview, subview, x, y, width=.1, height=.1) {
20 20
     
21 21
     xrng <- mainview$data[, aes_x] %>% range %>% diff
22 22
     yrng <- mainview$data[, aes_y] %>% range %>% diff
23
-   
23
+
24
+    if (!any(class(subview) %in% c("ggplot", "grob", "character"))) {
25
+        stop("subview should be a ggplot or grob object, or an image file...")
26
+    }
27
+    
28
+    if (is(subview, "ggplot")) {
29
+        sv <- ggplotGrob(subview)
30
+    } else if (is(subview, "grob")) {
31
+        sv <- subview
32
+    } else if (file.exists(subview)) {
33
+            EBImage <- "EBImage"
34
+            require(EBImage, character.only = TRUE)
35
+            readImage <- eval(parse(text="readImage"))
36
+            sv <- rasterGrob(readImage(subview))
37
+    } else {
38
+        stop("subview should be a ggplot or grob object, or an image file...")
39
+    }
40
+
24 41
     mainview + annotation_custom(
25
-        ggplotGrob(subview),
26
-        xmin = x - width*xrng,
27
-        xmax = x + width*xrng,
28
-        ymin = y - height*yrng,
29
-        ymax = y + height*yrng)
42
+                   sv,
43
+                   xmin = x - width*xrng,
44
+                   xmax = x + width*xrng,
45
+                   ymin = y - height*yrng,
46
+                   ymax = y + height*yrng)
30 47
 }
... ...
@@ -9,7 +9,7 @@ subview(mainview, subview, x, y, width = 0.1, height = 0.1)
9 9
 \arguments{
10 10
 \item{mainview}{main view}
11 11
 
12
-\item{subview}{sub view}
12
+\item{subview}{a ggplot or grob object}
13 13
 
14 14
 \item{x}{x position}
15 15
 
... ...
@@ -181,6 +181,17 @@ bx2 <- lapply(bx, function(g) g+coord_flip())
181 181
 inset(p2, bx2, width=.2, height=.03, vjust=.04, hjust=p2$data$x[1:15]-4) + xlim(NA, 4.5)
182 182
 ```
183 183
 
184
+## Annotate with image files
185
+
186
+```{r}
187
+imgfile <- tempfile(, fileext=".png")
188
+download.file("https://avatars1.githubusercontent.com/u/626539?v=3&u=e731426406dd3f45a73d96dd604bc45ae2e7c36f&s=140", destfile=imgfile, mode='wb')
189
+img <- list(imgfile, imgfile)
190
+names(img) <- c("18", "22")
191
+inset(p, img)
192
+```
193
+
194
+
184 195
 # Align tree with other plots on a page
185 196
 
186 197
 This is currently difficult to achieve in `ggplot2`. However, it is possible to obtain good results by creating a dummy faceting of data.
... ...
@@ -219,9 +230,5 @@ pp %>% phylopic("67382184-5135-4faa-8e98-eadff02c3e8a", color="#86B875", alpha=.
219 230
 ![](figures/phylopic2.png)
220 231
 
221 232
 
222
-Annotate phylogenetic tree with local images is also supported, please refer to the [blog post](http://guangchuangyu.github.io/2015/08/ggtree-annotate-phylogenetic-tree-with-local-images/).
223
-
224
-
225
-
226 233
 
227 234
 
... ...
@@ -31,6 +31,8 @@ library("ggplot2")
31 31
 library("ggtree")
32 32
 collapse <- ggtree::collapse
33 33
 expand <- ggtree::expand
34
+rotate <- ggtree::rotate
35
+flip <- ggtree::flip
34 36
 ```
35 37
 
36 38
 # Internal node number
... ...
@@ -193,8 +195,8 @@ Collpase selected clades can save some space, another approach is to zoom out cl
193 195
 
194 196
 ```{r fig.width=12, fig.height=6, warning=F}
195 197
 multiplot(ggtree(tree) + geom_hilight(21, "steelblue"),
196
-             ggtree(tree) %>% scaleClade(21, scale=0.3) + geom_hilight(21, "steelblue"),
197
-             ncol=2)
198
+          ggtree(tree) %>% scaleClade(21, scale=0.3) + geom_hilight(21, "steelblue"),
199
+          ncol=2)
198 200
 ```
199 201
 
200 202
 Of course, _`scaleClade`_ can accept `scale` larger than 1 and zoom in the selected portion.
... ...
@@ -202,21 +204,23 @@ Of course, _`scaleClade`_ can accept `scale` larger than 1 and zoom in the selec
202 204
 ```{r fig.width=12, fig.height=6, warning=F}
203 205
 multiplot(ggtree(tree) + geom_hilight(17, fill="steelblue") +
204 206
                  geom_hilight(21, fill="darkgreen"),
205
-             ggtree(tree) %>% scaleClade(17, scale=2) %>% scaleClade(21, scale=0.3) +
207
+          ggtree(tree) %>% scaleClade(17, scale=2) %>% scaleClade(21, scale=0.3) +
206 208
                  geom_hilight(17, "steelblue") + geom_hilight(21, fill="darkgreen"),
207
-             ncol=2)
209
+          ncol=2)
208 210
 ```
209 211
 
210 212
 # rotate clade
211 213
 
212 214
 A selected clade can be rotated by 180 degree using __`rotate`__ function.
213 215
 
216
+
214 217
 ```{r fig.width=8, fig.height=4}
215 218
 tree <- groupClade(tree, c(21, 17))
216 219
 p <- ggtree(tree, aes(color=group)) + scale_color_manual(values=c("black", "firebrick", "steelblue"))
217
-multiplot(p, p %>% rotate(21) %>% rotate(17), ncol=2)
220
+p2 <- rotate(p, 21) %>% rotate(17)
221
+multiplot(p, p2, ncol=2)
218 222
 ```
219
-                                                                                                                                                                         
223
+
220 224
 # flip clade
221 225
 
222 226
 The positions of two selected clades (should share a same parent) can be flip over using __`flip`__ function.