This is a test version of Biostars. For the public version, visit https://www.biostars.org.
GGTree flips my trees

Hi all,

I hope you are doing well. I am trying to visualize my phylogenetic tree with ggtree, but it keeps flipping my trees upside down. Below, you'll find a screenshot from Figtree and the ggtree output for the same tree. I circled the red clade from the Figtree in the ggtree output to help you better follow the topology. I tried flipping branches, rotating the tree, etc., but I couldn't manage to solve the problem. Is there any parameter to fix this? My code for ggtree is also provided below.

Can you help me with this issue? Thanks in advance.

p <- ggtree(tree, ladderize = FALSE, layout = "rectangular")

enter image description here enter image description here

r ggtree phylogenetics

1 answer

Funny, I just ran into exactly this problem myself recently. ggtree has its own rotate function but I couldn't figure out how to get it to work. Instead I made a wrapper around ape::rotate that seemed to do the trick:

rotate_all <- function(tree) {
  for (idx in seq(tree$Nnode + 2, nrow(tree$edge) + 1)) {
    tree <- ape::rotate(tree, idx)
  } 
  tree
}

(This implicitly assumes how nodes and edges are structured, with internal nodes getting higher numbers. That was just what seemed to be the case when I was playing around with it, but again, seemed to do the trick.)

Thank you very much, Jesse! I've been struggling with this issue for a while. This function is a game-changer for me. Thanks!

Log in to answer this question.