YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/contribution-tree-data/
838 stars 173 forks source link

Error when trying to scale and collaps multiple clades on tree #534

Open janstrauss1 opened 2 years ago

janstrauss1 commented 2 years ago

Hi there,

I'm trying to visualize a tree with multiple selected clades being scaled (condensed) and collapsed to save space for a publication figure.

I've seen the issue also reported in the ggtree google group. I've attached a reprex below to illustrate the issue when trying to scale and collapse multiple clades.

I think https://github.com/YuLab-SMU/ggtree/issues/409 somewhat relates to this.

Any help and suggestions would be very welcome.

Many thanks in advance!

####################################################
## Scaling and collapsing multiple clades on tree ##
####################################################
## load packages
suppressPackageStartupMessages(library("treeio"))
suppressPackageStartupMessages(library("ggtree"))
suppressPackageStartupMessages(library("ggplot2"))

## load example tree
raxml_file <- system.file("extdata/RAxML", "RAxML_bipartitionsBranchLabels.H3", package="treeio")
tree <- read.raxml(raxml_file)
# tree <- read.newick(raxml_file, node.label = "support")

## print example tree for inspection
# ggtree(tree) + geom_tiplab()

## access root node data for sub-setting and plotting bootstrap support values
root <- rootnode(tree)

## Visualize tree
tree <- ggtree(tree) +
  ## label with internal node numbers for downstream pruning and collapsing of clades
  # geom_label(aes(label=node), size = 2.5, nudge_x = -0.005, nudge_y = 0.0, label.size = 0.05) +
  geom_tiplab(size = 3) +
  ## Display different shaded points for bootstrap values
  geom_point2(aes(subset=!isTip & node != root & bootstrap > 80,
                  fill = cut(bootstrap, breaks = c(80, 90, 95, 100))),
              shape = 21, size = 1.5) +
  scale_fill_manual(values=c("black", "grey", "white"), guide='legend',
                    name='Bootstrap Support (BS)',
                    breaks=c('(95,100]', '(90,95]', '(80,90]'),
                    labels=expression(BS>=95,80 <= BS * " < 95", BS > 80)) +
  theme_tree(legend.position = c(0.2, 0.75),
             legend.title = element_text(size = 10),
             legend.text = element_text(size = 8),
             legend.key = element_rect(fill = NA)) +
  xlim(0, 0.15) +
  geom_treescale(x = 0.02, y = 43, offset = -1, linesize = 1, fontsize = 2.5)
## print tree
tree


## Scale and collapse multiple clades
## scale and collapse first clade 
# p <- scaleClade(tree, 113, .4) %>% collapse(113, "mixed", fill="black", alpha=0.4) %>% expand(113)
p1 <- scaleClade(tree, 113, .4) %>% collapse(113, "mixed", fill="black", alpha=0.4)
p1 # print tree with scaled and collapsed clade
#> Warning: Removed 6 rows containing missing values (geom_point_g_gtree).

## scale and collapse second clade 
p2 <- scaleClade(p1, 81, .4) %>% collapse(81, "mixed", fill="steelblue", alpha=0.4)
#> Error in if (sum(ii) > 0) {: missing value where TRUE/FALSE needed
p2 ## Tree p2 includes no scaling and collapsing of first clade as in tree p1
#> Error in eval(expr, envir, enclos): object 'p2' not found

Created on 2022-09-23 with reprex v2.0.2

morgansobol commented 1 year ago

I am having the same issue, were you able to solve it?

fiuzatayna commented 1 year ago

Same issue here.

fiuzatayna commented 1 year ago

I am having the same issue, were you able to solve it?

Problem apparently solved. I realized scaleClade did not work after a collapse, so I tried scaling all clades first, and then collapsing. It worked! Example where p is my tree:

p1 <- scaleClade(p, 2457, .2) %>% 
  scaleClade(2410, .2) %>% 
  scaleClade(2122, .2) %>%
  ggtree::collapse(2457, 'max', fill="red") %>%
  ggtree::collapse(2410, 'max', fill="red") %>%
  ggtree::collapse(2122, 'max', fill="red") 

p1
acarafat commented 1 year ago

I am having the same issue, were you able to solve it?

Problem apparently solved. I realized scaleClade did not work after a collapse, so I tried scaling all clades first, and then collapsing. It worked! Example where p is my tree:

p1 <- scaleClade(p, 2457, .2) %>% 
  scaleClade(2410, .2) %>% 
  scaleClade(2122, .2) %>%
  ggtree::collapse(2457, 'max', fill="red") %>%
  ggtree::collapse(2410, 'max', fill="red") %>%
  ggtree::collapse(2122, 'max', fill="red") 

p1

Works for me! Thanks a lot!