david-barnett / microViz

R package for microbiome data visualization and statistics. Uses phyloseq, vegan and the tidyverse. Docker image available.
https://david-barnett.github.io/microViz/
GNU General Public License v3.0
101 stars 11 forks source link

Strange behaviour when using comp_barplot with facet_grid #149

Closed DanaBlu closed 5 months ago

DanaBlu commented 5 months ago

Hi @david-barnett,

thank your for your great package! I already saved a lot of time while using it!

I encountered a problem when using comp_barplot together with facet_grid. Each x-axis label was repreated four times, but three times without the corresponding data plotted. This also happend even though using the scales ="free" option. Without scales = "free", there were even more "empty" repetitions.

comp_barplot(phy.merged.rep, tax_level = "Phylum", n_taxa = 11, label = "sample_short", sample_order = sample_names(phy.merged.rep)) + facet_grid(id ~ racon, scales = "free") + theme_bw() + theme(panel.grid.major = element_blank(), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1), axis.title.y = element_text(size = 10, vjust = 3, face = "plain")) + guides(col = FALSE, shape = guide_legend(override.aes = list(size = 2.5)))

V9_phylum_wrong

My sample_data(phy.merged.rep) looks like this:

row_name | sample_short | racon | id C_S1_V9_1_95 | C_S1 | 1 | 95 C_S1_V9_1_96 | C_S1 | 1 | 96 C_S1_V9_1_97 | C_S1 | 1 | 97 C_S1_V9_1_98 | C_S1 | 1 | 98 C_S1_V9_2_95 | C_S1 | 2 | 95 C_S1_V9_2_96 | C_S1 | 2 | 96 C_S1_V9_2_97 | C_S1 | 2 | 97 C_S1_V9_2_98 | C_S1 | 2 | 98 C_S1_V9_3_95 | C_S1 | 3 | 95 C_S1_V9_3_96 | C_S1 | 3 | 96 C_S1_V9_3_97 | C_S1 | 3 | 97 C_S1_V9_3_98 | C_S1 | 3 | 98 C_S1_V9_4_95 | C_S1 | 4 | 95 C_S1_V9_4_96 | C_S1 | 4 | 96 C_S1_V9_4_97 | C_S1 | 4 | 97 C_S1_V9_4_98 | C_S1 | 4 | 98 C_S1_V9_5_95 | C_S1 | 5 | 95 C_S1_V9_5_96 | C_S1 | 5 | 96 C_S1_V9_5_97 | C_S1 | 5 | 97 C_S1_V9_5_98 | C_S1 | 5 | 98 C_S5_V9_1_95 | C_S5 | 1 | 95 C_S5_V9_1_96 | C_S5 | 1 | 96 C_S5_V9_1_97 | C_S5 | 1 | 97 C_S5_V9_1_98 | C_S5 | 1 | 98 C_S5_V9_2_95 | C_S5 | 2 | 95 C_S5_V9_2_96 | C_S5 | 2 | 96 C_S5_V9_2_97 | C_S5 | 2 | 97 .....

So I think the problem is, it is taking the x-axis label information from the exact sample_short row where it finds the corresponding racon and id variable. But there should be an option if it checks if the labels are identical and only displays the unique x-axis labels.

Is there any solution to get rid of the "empty" x-axis labels?

Thanks and best,

Dana

david-barnett commented 5 months ago

Hi Dana, glad to hear you find microViz helpful :)

For your problem, I think you need to set the x argument, e.g. try this?

comp_barplot(
  phy.merged.rep, tax_level = "Phylum", n_taxa = 11, 
  x = "sample_short", label = "sample_short" 
  # otherwise the x aesthetic ggplot uses is still the unique sample name (but just hidden behind the relabelling)
) +
facet_grid(id ~ racon, scales = "free")
DanaBlu commented 5 months ago

That solved it! Thank your for your fast reply and help with my issue!