embed images in ggplot2 via subview and annotate a phylogenetic tree with images using inset function

I extended the subview function to support embed image file in a ggplot object.

d = data.frame(x=rnorm(10), y=rnorm(10))

imgfile <- tempfile(, fileext=".png")
              destfile=imgfile, mode='wb')

p = ggplot(d, aes(x, y))
subview(p, imgfile, x=d$x[1], y=d$y[1]) + geom_point(size=5)

In previous post, I have introduced using annotation_image function for annotating tips with local images.

Now with the updated subview function, we can use inset function to annotate nodes/tips with image files and/or subplots.

tr <- rtree(15)
p <- ggtree(tr)

img <- list(imgfile, imgfile)
names(img) <- c("18", "22")
inset(p, img)

PS: As I got more and more emails from ggtree users, I created a forum, https://groups.google.com/forum/#!forum/bioc-ggtree. With this forum, ggtree users can help each others and also solutions posted in the forum can help other peoples when they google search their questions. Please join the forum and post your questions if you have any.


G Yu, DK Smith, H Zhu, Y Guan, TTY Lam*. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution. doi:10.1111/2041-210X.12628.

comments powered by Disqus