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

[This article was first published on R on Guangchuang Yu, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

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

imgfile <- tempfile(, fileext=".png")
download.file("https://avatars1.githubusercontent.com/u/626539?v=3&u=e731426406dd3f45a73d96dd604bc45ae2e7c36f&s=140",
              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.

set.seed(2015-12-31)
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.

To leave a comment for the author, please follow the link and comment on their blog: R on Guangchuang Yu.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)