flip and rotate branches in ggtree

R-bloggers 2015-07-01

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

flip function is a feature request from ggtree user. It accepts a tree view and two node numbers and exchange the positions of the selected clades.

?View Code RSPLUS
12345
require(ggtree)set.seed(2015-07-01)tr <- rtree(30)p <- ggtree(tr) + geom_text(aes(label=node))gridExtra::grid.arrange(p, flip(p, 38, 33), ncol=2)

It’s chainable and all possible position adjustment can be achieved via multiple flip operations.

?View Code RSPLUS
1
flip(p, 38, 33) %>% flip(53, 58) %>% flip(32, 51)

It’s tedious to rotate a clade by 180 degree with several flip operations, ggtree provides another function rotate that accepts a selected node and rotate that branch by 180 degree.

?View Code RSPLUS
1234
col = c("black", "firebrick", "steelblue")[groupClade(tr, c(33, 52))]gridExtra::grid.arrange(ggtree(tr, color=col),                         ggtree(tr, color=col) %>% rotate(33) %>% rotate(52),                         ncol=2)

Related Posts

To leave a comment for the author, please follow the link and comment on his blog: YGC » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...