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.

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.

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.

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)

Citation

G Yu, DK Smith, H Zhu, Y Guan, TTY Lam*. ggtree: an R package for visualization and annotation of phylogenetic trees with their covariates and other associated data. Methods in Ecology and Evolution. doi:10.1111/2041-210X.12628.