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.
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.
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.
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
- April 1, 2015 — an example of drawing beast tree using ggtree (1)
- February 10, 2015 — [ggtree] updating a tree view using %<% operator (0)
- January 19, 2015 — ggtree in Bioconductor 3.1 (15)
- June 12, 2015 — ggtree with funny fonts (9)
- December 21, 2014 — viewing and annotating phylogenetic tree with ggtree (7)
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...