subview

[This article was first published on R on G. 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 implemented a function, subview, in ggtree that make it easy to embed a subplot in ggplot.

An example is shown below:

library(ggplot2)
library(ggtree)

dd <- data.frame(x=LETTERS[1:3], y=1:3)
pie <- ggplot(dd, aes(x=1, y, fill=x)) + 
             geom_bar(stat="identity", width=1) + 
                    coord_polar(theta="y") + theme_tree() + 
                          xlab(NULL) + ylab(NULL) + 
                               theme_transparent()

x <- sample(2:9)
y <- sample(2:9)
width <- sample(seq(0.05, 0.15, length.out=length(x)))
height <- width

p <- ggplot(data=data.frame(x=c(0, 10), y=c(0, 10)), aes(x, y))+geom_blank()
print(p)
for (i in seq_along(x)) {
    p %<>% subview(pie, x[i], y[i], width[i], height[i])
    print(p)
}

With this function, we can plot a specific clade and add a subplot of the whole topology; we can also add relative statistic graph above the tree or to a specific node. It’s not specific to phylogenetic tree, it works with all ggplot objects as demonstrated in the example.

PS: ggtree is now presented in CRAN Task View: Phylogenetics.

AND get its first citation.

To leave a comment for the author, please follow the link and comment on their blog: R on G. 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)