microeco icon indicating copy to clipboard operation
microeco copied to clipboard

Trying to fix colors in plot_bar

Open cdeschamps63 opened this issue 2 years ago • 1 comments

Hello,

First of all I would like to thank you for this incredible package which allow me to gain much time and to performed quickly new analysis ! This is a true goldmine !

I have three questions concerning "plot_bar" function.

1) Is it possible to move the "Others" at the end of the list instead of the beginning ? It would be more relevant on my results and I failed to move it...

Plot: plotbar_phylum_feces_1.pdf

2) In order to performed different graphs on my different donors, I've subseted my dataset into different datasets. However, I didn't always have the same legends colors between two graphs depending on the number of phylum, for example (see plots before and below) : dog1_phylum_lum.pdf

I've tried to fixed it with the following lines :

col.phylum = as.factor(sapply(dog1lum, function(x) switch (as.character(x),"Actinobacteriota"="#BC80BD", "Bacteroidota"="gray85", "Bdellovibrionota"="#FCCDE5", "Campylobacterota"="B3DE69", 
"Desulfobacterota"="#FDB462", "Firmicutes"="#80B1D3", "Firmicutes_C"= "#FB8072", "Fusobacteriota"="#BEBADA", "Proteobacteria"="#FFFFB3", "unassigned"="#8DD3C7")))

d1l <- d1l$plot_bar(others_color = "grey70", facet = "sample.type", 
                   xtext_keep = FALSE, legend_text_italic = TRUE,
                   color_values =col.phylum)

and also

d1l$plot_bar(others_color = "grey70", facet = "sample.type", 
                   xtext_keep = FALSE, legend_text_italic = TRUE,
                   color_values = c("Actinobacteriota"="#BC80BD", "Bacteroidota"="gray85", "Bdellovibrionota"="#FCCDE5", "Campylobacterota"="B3DE69", "Desulfobacterota"="#FDB462", "Firmicutes"="#80B1D3", "Firmicutes_C"= "#FB8072", "Fusobacteriota"="#BEBADA", "Proteobacteria"="#FFFFB3", "unassigned"="#8DD3C7"))

but this doesn't works : "Erreur : tentative d'appliquer un objet qui n'est pas une fonction". Do you have an idea of the way I can solve it ?

3) Do you know if I can changed my x labels by using another factor from the sample_table instead of the sample name ? It would be more relevant for me to keep the day of sampling below the bar.

Thank you very much for your help !

cdeschamps63 avatar Jun 10 '22 13:06 cdeschamps63

Hi @cdeschamps63

  1. Would you like to just remove the "Others" in legend? If so, please try to use bar_type parameter like this:
library(microeco)
data(dataset)
t1 <- trans_abund$new(dataset = dataset, taxrank = "Phylum", ntaxa = 10)
t1$plot_bar(bar_type = "notfull")
  1. Do you mean to keep all the legend same order and colors in your different plot? If so, please try to use input_taxaname parameter in trans_abund$new and also see this interesting topic (https://github.com/ChiLiubio/microeco/discussions/45) (Note that use_taxanames in the trans_abund object has been renamed to data_taxanames). Here is my example:
d1 <- clone(dataset)
d1$sample_table <- d1$sample_table[1:5, ]
d1$tidy_dataset()
t1 <- trans_abund$new(d1, input_taxaname = c("Bacteroidetes", "Acidobacteria"))
t1$plot_bar(bar_type = "notfull")
d2 <- clone(dataset)
d2$sample_table <- d2$sample_table[30:35, ]
d2$tidy_dataset()
t2 <- trans_abund$new(d2, input_taxaname = c("Bacteroidetes", "Acidobacteria"))
t2$plot_bar(bar_type = "notfull")
  1. Now there is no such parameter to replace labels for the x axis. I will think about it. Is it possible for you to directly change the ggplot2 object using scale_x_discrete like this:
library(ggplot2)
# use the previous plot
g2 <- t2$plot_bar(bar_type = "notfull")
g2 + scale_x_discrete(labels = paste0("new", 1:6))

Best Chi

ChiLiubio avatar Jun 11 '22 02:06 ChiLiubio