Browse code

annotation_image

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

g.yu authored on 01/08/2015 13:12:53
Showing 8 changed files

... ...
@@ -19,6 +19,7 @@ export(add_legend)
19 19
 export(aes)
20 20
 export(annotation_clade)
21 21
 export(annotation_clade2)
22
+export(annotation_image)
22 23
 export(as.binary)
23 24
 export(collapse)
24 25
 export(download.phylopic)
... ...
@@ -1,5 +1,6 @@
1 1
 CHANGES IN VERSION 1.1.13
2 2
 ------------------------
3
+ o implement annotation_image <2015-08-01, Sat>
3 4
  o better implementation of geom_tiplab for accepting aes mapping and auto add align dotted line <2015-08-01, Sat>
4 5
  o open group_name parameter of groupOTU/groupClade to user <2015-08-01, Sat>
5 6
  
... ...
@@ -160,7 +160,7 @@ geom_hilight <- function(tree_object, node, ...) {
160 160
 ##' @param hjust horizontal adjustment
161 161
 ##' @param align align tip lab or not, logical
162 162
 ##' @param linetype linetype for adding line if align = TRUE
163
-##' @param line.size line size of line if align = TRUE
163
+##' @param linesize line size of line if align = TRUE
164 164
 ##' @param ... additional parameter
165 165
 ##' @return tip label layer
166 166
 ##' @importFrom ggplot2 geom_text
... ...
@@ -170,7 +170,7 @@ geom_hilight <- function(tree_object, node, ...) {
170 170
 ##' require(ape)
171 171
 ##' tr <- rtree(10)
172 172
 ##' ggtree(tr) + geom_tiplab()
173
-geom_tiplab <- function(mapping=NULL, hjust = 0, align = FALSE, linetype = "dotted", line.size=1, ...) {
173
+geom_tiplab <- function(mapping=NULL, hjust = 0, align = FALSE, linetype = "dotted", linesize=1, ...) {
174 174
     x <- y <- label <- isTip <- NULL
175 175
     if (align == TRUE) {
176 176
         self_mapping <- aes(x = max(x) + diff(range(x))/200, label = label)
... ...
@@ -186,7 +186,7 @@ geom_tiplab <- function(mapping=NULL, hjust = 0, align = FALSE, linetype = "dott
186 186
     }
187 187
 
188 188
     dot_mapping <- NULL
189
-    if (align && (!is.na(linetype) || !is.null(linetype))) {
189
+    if (align && (!is.na(linetype) && !is.null(linetype))) {
190 190
         dot_mapping <- aes(xend=x+diff(range(x))/200, x=max(x), yend=y)
191 191
         if (!is.null(mapping)) {
192 192
             dot_mapping <- modifyList(dot_mapping, mapping)
... ...
@@ -201,7 +201,7 @@ geom_tiplab <- function(mapping=NULL, hjust = 0, align = FALSE, linetype = "dott
201 201
             geom_segment(mapping=dot_mapping,
202 202
                          subset=.(isTip),
203 203
                          linetype = linetype,
204
-                         size = line.size, ...)
204
+                         size = linesize, ...)
205 205
         )
206 206
 }
207 207
 
... ...
@@ -29,16 +29,9 @@ get.phylopic <- function(id, size=512, color="black", alpha=1) {
29 29
 ##' @export
30 30
 ##' @author Guangchuang Yu
31 31
 download.phylopic <- function(id, size=512, color="black", alpha=1) {
32
-    size %<>% as.character %>%
33
-        match.arg(c("64", "128", "256", "512", "1024"))
32
+    imgfile <- tempfile(fileext = ".png")
33
+    download.phylopic_internal(id, size, imgfile)
34 34
 
35
-    imgurl <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png")
36
-    imgfile <- tempfile(fileext = ".png") 
37
-    if (Sys.info()["sysname"] == "Windows") {
38
-        download.file(imgurl, imgfile, mode="wb", quiet = TRUE)
39
-    } else {
40
-        download.file(imgurl, imgfile, quiet = TRUE)
41
-    }
42 35
     img <- readImage(imgfile)
43 36
        
44 37
     color <- col2rgb(color) / 255
... ...
@@ -52,6 +45,19 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) {
52 45
     return(img)
53 46
 }
54 47
 
48
+download.phylopic_internal <- function(id, size=512, outfile=NULL) {
49
+    size %<>% as.character %>%
50
+        match.arg(c("64", "128", "256", "512", "1024"))
51
+
52
+    imgurl <- paste0("http://phylopic.org/assets/images/submissions/", id, ".", size, ".png")
53
+    if (is.null(outfile)) {
54
+        outfile <- sub(".*/", "", imgurl)
55
+    }
56
+    ## mode = "wb" for Windows platform
57
+    download.file(imgurl, outfile, mode="wb", quiet = TRUE) 
58
+}
59
+
60
+
55 61
 ##' add phylopic layer
56 62
 ##'
57 63
 ##' 
... ...
@@ -72,7 +78,8 @@ download.phylopic <- function(id, size=512, color="black", alpha=1) {
72 78
 ##' @author Guangchuang Yu
73 79
 phylopic <- function(tree_view, phylopic_id,
74 80
                      size=512, color="black", alpha=0.5,
75
-                     node=NULL, x=NULL, y=NULL, width=5) {
81
+                     node=NULL, x=NULL, y=NULL, width=.1) {
82
+    width <- diff(range(tree_view$data$x)) * width
76 83
     img <- download.phylopic(phylopic_id, size, color, alpha)
77 84
     if ( is.null(node) ) {
78 85
         xmin <- ymin <- -Inf
... ...
@@ -85,9 +92,7 @@ phylopic <- function(tree_view, phylopic_id,
85 92
             x <- tree_view$data[node, "x"]
86 93
             y <- tree_view$data[node, "y"]
87 94
         }
88
-          
89
-        dims <- dim(img)[1:2]
90
-        AR <- dims[1]/dims[2]
95
+        AR <- getAR(img)
91 96
         xmin <- x - width/2
92 97
         xmax <- x + width/2
93 98
         ymin <- y - AR * width/2
... ...
@@ -99,3 +104,60 @@ phylopic <- function(tree_view, phylopic_id,
99 104
                                   rasterGrob(img))
100 105
 }
101 106
 
107
+getAR <- function(img) {
108
+    dims <- dim(img)[1:2]
109
+    dims[1]/dims[2]
110
+}
111
+
112
+
113
+##' annotation taxa with images
114
+##'
115
+##' 
116
+##' @title annotation_image
117
+##' @param tree_view tree view
118
+##' @param img_info data.frame with first column of taxa name and second column of image names
119
+##' @param width width of the image to be plotted in image
120
+##' @param align logical
121
+##' @param linetype line type if align = TRUE
122
+##' @param linesize line size if align = TRUE
123
+##' @param offset offset of image from the tree view
124
+##' @return tree view
125
+##' @export
126
+##' @author Guangchuang Yu
127
+annotation_image <- function(tree_view, img_info, width=0.1, align=TRUE, linetype="dotted", linesize =1, offset=0) {
128
+    df <- tree_view$data
129
+    idx <- match(img_info[,1], df$label)
130
+    x <- df[idx, "x"]
131
+    y <- df[idx, "y"]
132
+
133
+    images <- lapply(img_info[,2], readImage)
134
+
135
+    ARs <- sapply(images, getAR)
136
+
137
+    width <- width * diff(range(df$x))
138
+    if (align) {
139
+        xmin <- max(df$x) + offset
140
+        xmin <- rep(xmin, length(x))
141
+        xmax <- xmin + width
142
+    } else {
143
+        xmin <- x - width/2
144
+        xmax <- x + width/2
145
+    }
146
+    ymin <- y - ARs * width/2
147
+    ymax <- y + ARs * width/2
148
+    image_layers <- lapply(1:length(xmin), function(i) {
149
+        annotation_custom(xmin=xmin[i], ymin=ymin[i],
150
+                          xmax=xmax[i], ymax=ymax[i],
151
+                          rasterGrob(images[[i]]))
152
+    })
153
+
154
+    tree_view <- tree_view + image_layers
155
+
156
+    if (align && (!is.null(linetype) && !is.na(linetype))) {
157
+        tree_view <- tree_view + geom_segment(data=df[idx,],
158
+                                              x=xmin, xend = x*1.01,
159
+                                              y = y, yend = y,
160
+                                              linetype=linetype, size=linesize)
161
+    }
162
+    tree_view
163
+}
102 164
new file mode 100644
... ...
@@ -0,0 +1,34 @@
1
+% Generated by roxygen2 (4.1.1): do not edit by hand
2
+% Please edit documentation in R/phylopic.R
3
+\name{annotation_image}
4
+\alias{annotation_image}
5
+\title{annotation_image}
6
+\usage{
7
+annotation_image(tree_view, img_info, width = 0.1, align = TRUE,
8
+  linetype = "dotted", linesize = 1, offset = 0)
9
+}
10
+\arguments{
11
+\item{tree_view}{tree view}
12
+
13
+\item{img_info}{data.frame with first column of taxa name and second column of image names}
14
+
15
+\item{width}{width of the image to be plotted in image}
16
+
17
+\item{align}{logical}
18
+
19
+\item{linetype}{line type if align = TRUE}
20
+
21
+\item{linesize}{line size if align = TRUE}
22
+
23
+\item{offset}{offset of image from the tree view}
24
+}
25
+\value{
26
+tree view
27
+}
28
+\description{
29
+annotation taxa with images
30
+}
31
+\author{
32
+Guangchuang Yu
33
+}
34
+
... ...
@@ -5,7 +5,7 @@
5 5
 \title{geom_tiplab}
6 6
 \usage{
7 7
 geom_tiplab(mapping = NULL, hjust = 0, align = FALSE,
8
-  linetype = "dotted", line.size = 1, ...)
8
+  linetype = "dotted", linesize = 1, ...)
9 9
 }
10 10
 \arguments{
11 11
 \item{mapping}{aes mapping}
... ...
@@ -16,7 +16,7 @@ geom_tiplab(mapping = NULL, hjust = 0, align = FALSE,
16 16
 
17 17
 \item{linetype}{linetype for adding line if align = TRUE}
18 18
 
19
-\item{line.size}{line size of line if align = TRUE}
19
+\item{linesize}{line size of line if align = TRUE}
20 20
 
21 21
 \item{...}{additional parameter}
22 22
 }
... ...
@@ -5,7 +5,7 @@
5 5
 \title{phylopic}
6 6
 \usage{
7 7
 phylopic(tree_view, phylopic_id, size = 512, color = "black", alpha = 0.5,
8
-  node = NULL, x = NULL, y = NULL, width = 5)
8
+  node = NULL, x = NULL, y = NULL, width = 0.1)
9 9
 }
10 10
 \arguments{
11 11
 \item{tree_view}{tree view}
... ...
@@ -426,8 +426,8 @@ pp
426 426
 ```
427 427
 
428 428
 ```{r fig.width=5, fig.height=5, fig.align="center", warning=FALSE}
429
-pp %>% phylopic("67382184-5135-4faa-8e98-eadff02c3e8a", color="#86B875", alpha=.8, node=4, width=4) %>%
430
-    phylopic("d3563b54-780f-4711-a49a-7ea051e9dacc", color="darkcyan", alpha=.8, node=17, width=8)
429
+pp %>% phylopic("67382184-5135-4faa-8e98-eadff02c3e8a", color="#86B875", alpha=.8, node=4) %>%
430
+    phylopic("d3563b54-780f-4711-a49a-7ea051e9dacc", color="darkcyan", alpha=.8, node=17, width=.2)
431 431
 ```
432 432
 
433 433