Site icon R-bloggers

[ggtree] annotate phylogenetic tree with local images

In ggtree, we provide a function annotation_image for annotating phylogenetic tree with images.

To demonstrate the usage, I created a tree view from a random tree as shown below:

?View Code RSPLUS
1
2
set.seed(2015-08-01)
p <- ggtree(rtree(10)) + xlim(0, 5)+ ylim(0, 11)

We need a data.frame that contains information of taxa labels and image paths. Here I created such a data.frame containing image files downloaded from phylopic.

?View Code RSPLUS
1
2
3
4
5
6
7
> img_info
  V1                                           V2
1 t1 25f165fa-f279-4f7c-9869-c55be251ffb8.512.png
2 t2 5d7ab302-960b-4db7-9dfd-215175a55906.512.png
3 t3 6fbe723c-3a6b-4d06-8680-bb2a52113df4.512.png
4 t4 d83c02ca-76ed-436b-83ae-7f98d7297be9.512.png
5 t5 ee764929-c865-44f6-b5db-b4e7d5693d1a.512.png

Annotating tree with images is simple in ggtree by using annotation_image function.

?View Code RSPLUS
1
annotation_image(p, img_info)

By default, all the images will align to the right hand side. We can use align=FALSE, to disable it.

?View Code RSPLUS
1
annotation_image(p, img_info, align=FALSE)

We can change the type and size of lines, as demonstrated below:

?View Code RSPLUS
1
annotation_image(p, img_info, linetype="dashed", linesize=0.2)

The width of the images were controlled by width parameter, and the height will automatically determined by image dimension.

?View Code RSPLUS
1
annotation_image(p, img_info, width = .2, linetype="dashed", linesize=0.2)

In the following example, we add tip labels to the tree.

?View Code RSPLUS
1
2
p <- p+geom_tiplab(align=TRUE, linetype="dashed", linesize=.2)
print(p)

If we also want to add the image and align them, we don’t want to show the line added by annotation_image function. This can be achieved by setting linetype=NULL. By default the images and tip labels will be overlapped, we can move the images by offset parameter.

?View Code RSPLUS
1
p %>% annotation_image(img_info, width=.2, linetype=NULL, offset=.3)

Related Posts