grunwaldlab / metacoder

Parsing, Manipulation, and Visualization of Metabarcoding/Taxonomic data
http://grunwaldlab.github.io/metacoder_documentation
Other
135 stars 28 forks source link

Exclude labels of lowest taxon_rank #367

Open fionnualabulman opened 3 days ago

fionnualabulman commented 3 days ago

Hello and thanks for the great package.

I am producing a heat tree matrix of differential relative abundances in fungal taxa. I would like to display differences in taxa down to the order rank, however including the labels for this rank makes the figure unreadable. Is there a way to exclude all labels for this rank only (keeping labels for class, phyla & kingdom)?

I have read the FAQ "The labels are too small!" but hope there is another option instead of selectively suppressing them with "dont_print".

I have tried to filter "cleaned_names" for taxon_ranks = "o" but this produces an error as the vectors are no longer the same length:

obj %>%
+   metacoder::filter_taxa(taxon_ranks == "o", supertaxa = TRUE, reassign_obs = c(diff_table = FALSE)) %>%
+   mutate_obs("cleaned_names", 
+              gsub(taxon_names, pattern = "\\[|\\]", replacement = "")) %>%
+   filter_obs("cleaned_names", taxon_ranks != "o") %>%
+   metacoder::filter_taxa(grepl(cleaned_names, pattern = "^[a-zA-Z]+$"), reassign_obs = c(diff_table = FALSE)) %>%
+   heat_tree_matrix(data = "diff_table",
+                    node_label = cleaned_names,
+                    node_label_size = 7 * (n_obs / max(n_obs)),  # Adjust based on the number of OTUs
+                    node_size = n_obs,
+                    node_color = log2_median_ratio, # difference between groups
+                    node_color_trans = "linear",
+                    node_color_interval = c(-3, 3), # symmetric interval
+                    edge_color_interval = c(-3, 3), # symmetric interval
+                    node_color_range = c("cyan", "gray", "magenta"), # The color palette used
+                    node_color_axis_label = "Log 2 ratio of median counts",
+                    node_size_axis_label = "Number of OTUs",
+                    node_size_range = c(0.02, 0.04),
+                    node_label_size_range = c(0.02, 0.04),
+                    layout = "da", initial_layout = "re",
+                    key_size = 0.67,
+                    seed = 2)
Adding a new "character" vector of length 162.
Error: TRUE/FALSE vector (length = 57) must be the same length as the number of taxa (162)

Thanks for your help with this.

zachary-foster commented 3 days ago

Hi, thanks! Does this do what you want?

library(metacoder)
#> This is metacoder version 0.3.7 (stable)

x = parse_tax_data(hmp_otus, class_cols = "lineage", class_sep = ";",
                   class_key = c(tax_rank = "taxon_rank", tax_name = "taxon_name"),
                   class_regex = "^(.+)__(.+)$")

heat_tree(
  x, 
  node_label = ifelse(taxon_ranks == "g", '', taxon_names),
  node_size = n_obs,
  node_color = n_obs
)

Created on 2024-10-15 with reprex v2.1.1