flip and rotate branches in ggtree

June 30, 2015
By

(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
1
2
3
4
5
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
1
2
3
4
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 their blog: YGC » R.

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.

Sponsors

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)