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

March 19, 2016

(This article was first published on R on Guangchuang Yu, and kindly contributed to R-bloggers)

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.

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


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)