gmteunisse / fantaxtic

Fantaxtic - Nested Bar Plots for Phyloseq Data
26 stars 3 forks source link

How to make taxa colors congruent along plots? #9

Closed Valentin-Bio-zz closed 1 year ago

Valentin-Bio-zz commented 1 year ago

Hello developer, I'm working a set of more than 200 samples separated in different study conditions. From my 3 experimental conditions (with different number of samples )I elaborated three phyloseq objects and made plots of them . The problem is that a same ASV in one plot is showed in different color on the other plots. Is there a way in which I can maintain the same color for a respective taxon along the three plots?

This is what I did:

get top 15 ASVs

top_asvs_nasal_RN = get_top_taxa(physeq_obj = refiltered_nasal_RN, n = 15 , relative = TRUE, discard_other = FALSE, other_label = "Other")

top_asvs_nasal_AR = get_top_taxa(physeq_obj = refiltered_nasal_AR, n = 15, relative = TRUE, discard_other = FALSE, other_label = "Other")

top_asvs_nasal_NO = get_top_taxa(physeq_obj = refiltered_nasal_NO, n = 15, relative = TRUE, discard_other = FALSE, other_label = "Other")

create labels for missing taxonomic ranks

top_asvs_nasal_RN = name_taxa(top_asvs_nasal_RN, label = "Unknown", species = T, other_label = "Other") top_asvs_nasal_AR = name_taxa(top_asvs_nasal_AR, label = "Unknown", species = T, other_label = "Other") top_asvs_nasal_NO = name_taxa(top_asvs_nasal_NO, label = "Unknown", species = T, other_label = "Other")

generate fantaxtic plots

fantaxtic_bar(top_asvs_nasal_RN, color_by = "Phylum", label_by = "Species", other_label = "Other") fantaxtic_bar(top_asvs_nasal_AR, color_by = "Phylum", label_by = "Species", other_label = "Other") fantaxtic_bar(top_asvs_nasal_NO, color_by = "Phylum", label_by = "Species", other_label = "Other")

the three above plots have different colors for the same taxon

gmteunisse commented 1 year ago

Hi Valentin,

That is a good question. In fantaxtic_bar, you can specify a palette with the desired colours. However, if the Phyla in your example are not the same, then this might require a bit of playing around to get right.

I have two other solutions. Firstly, if you have some time, I am currently working on an update that, among other things, will make it possible to provide a named palette. I expect to release the update in the upcoming week.

Alternatively, if you want to be able to do this right now, consider using a package that I am currently developing: ggnested. This package provides the core functionality for the upcoming update of Fantaxtic, and allows you to pass a named palette to the function. The workflow would look as follows for you:

devtools::install_github("gmteunisse/ggnested")
devtools::install_github("gmteunisse/fantaxtic")
install.packages("forcats")
install.packages("tidyverse")
require("ggnested")
require("fantaxtic")
require("forcats")
require("tidyverse")
data("GlobalPatterns")

# Prepare the data for plotting
ps_tmp <- get_top_taxa(physeq_obj = GlobalPatterns, n = 10, relative = TRUE,
                       discard_other = FALSE, other_label = "Other")
ps_tmp <- name_taxa(ps_tmp, label = "Unkown", species = T, other_label = "Other")

# Convert phyloseq to dataframe
psdf <- psmelt(ps_tmp)

# Create a named palette
pal <- gen_colors(length(unique(psdf$Phylum)))
names(pal) <- unique(psdf$Phylum)

# Set Other to grey and make it appear first in the plot
pal["Other"] <- "grey90"
psdf <- psdf %>%
  mutate(Phylum = fct_relevel(Phylum, "Other", after = 0))

# Plot a relative barplot with a named palette
ggnested(psdf, aes(x = Sample, y = Abundance, main_group = Phylum, sub_group = Species), main_palette = pal) + 
  geom_col(position = "fill")
gmteunisse commented 1 year ago

I've implemented a major update that resolves your issue. The functions that you used before are now deprecated. Instead, run the code below. Notice that you can now provide a (partial) named palette to ensure that phyla have the same colours over different plots:

data(GlobalPatterns)
top_asv <- top_taxa(GlobalPatterns, n_taxa = 10)
plot_nested_bar(ps_obj = top_asv$ps_obj,
                top_level = "Phylum",
                nested_level = "Species",
                palette = c(Bacteroidetes = "red", 
                            Proteobacteria = "blue"))