# flip and rotate branches in ggtree

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**YGC » R**, 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.

* 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 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.