YuLab-SMU / ggtree

:christmas_tree:Visualization and annotation of phylogenetic trees
https://yulab-smu.top/treedata-book/
830 stars 172 forks source link

groupOTU adds an extra group called 0 #533

Open MarekGierlinski opened 1 year ago

MarekGierlinski commented 1 year ago

Prerequisites

Describe you issue

Ask in right place

Here is a reproducible example to illustrate the issue.

library(tidyverse)
library(ggtree)
library(ape)

set.seed(32)

# 10 nodes, 3 groups
info <- tibble(
  label = paste0("S", 1:10),
  group = c(rep("A", 3), rep("B", 3), rep("C", 4)),
  mean =  c(rep(1, 3), rep(3, 3), rep(5, 4))
)
# group information, a list of three groups, contains node labels
group_info <- split(info$label, info$group)

# create data for clustering: matrix with 10 rows and 30 columns, each row
# contains normal distribution with the mean taken from info tibble
x <- expand_grid(i = 1:30, label = info$label) |> 
  left_join(info, by = "label") |> 
  rowwise() |> 
  mutate(value = rnorm(1, mean)) |> 
  pivot_wider(id_cols = label, names_from = i, values_from = value) |> 
  column_to_rownames("label")

# clustering
hc <- x |> 
  dist() |> 
  hclust()

# convert to phylo, add group information
ph <- ape::as.phylo(hc) 
ph_gr <- groupOTU(ph, group_info)

ggtree(ph_gr, aes(colour = group), layout = "circular", branch.length = "none")

There are three groups, A, B and C, but there is an additional group called 0 in the resulting plot.

I tried to unroot the tree

ph <- ape::unroot(ph)

but this does not help:

YUE90 commented 1 year ago

Hello, have you solved it? I also met the same problem now.

DrZhangjilin commented 2 months ago

Hello, have you solved it? I also met the same problem now, too.