teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
534 stars 32 forks source link

Remove x-axis labels with independent = "x" if they are identical #155

Closed DanaBlu closed 1 week ago

DanaBlu commented 2 months ago

Hey @teunbrand,

thank you for your great package!

I was wondering if there is an option to use the remove_labels="x" option together with independent="x" from facet_grid2, if the labels are identical.

I will describe my issue from the very beginning:

I'm working with comp_barplot from the microVIZ package (https://github.com/david-barnett/microViz) and I encountered a problem when using facet_grid from ggplot2. Each x-axis label was repreated four times, but three times without the corresponding data plotted.

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

Then I found your package and was able to only plot the data on the x-axis were data points are actually available with the independent="x" option.

comp_barplot(phy.merged.rep, tax_level = "Phylum", n_taxa = 11, label = "sample_short", sample_order = sample_names(phy.merged.rep)) + facet_grid2(id ~ racon, scales = "free", independent="x") + 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

Now I would like to get rid of the x-axis labels and only display them at the outer margins (remove_labels="x"), as the labels are identical anyways. However, I learned that remove_labels cannot be used together with independent.

Is there any solution for me to get rid of the identical x-axis labels?

Best,

Dana

teunbrand commented 2 months ago

Hi there, thanks for the report! Could you give a minimal reprocible example? At the time of writing, I'm too tired to go through the source code of comp_barplot() to see where the problem might originate.

DanaBlu commented 2 months ago

Thank your for your fast reply!

So here are my example sample_data, taxa_table and otu_table.

inf_test.csv tax_test.csv otu_test.csv

I have created the phyloseq object and the taxonomy representation as follows:

phy.info <- sample_data(inf) phy.otu <- otu_table(t(otu), taxa_are_rows=T) phy.taxonomy <- tax_table(as.matrix(tax)) phy <- phyloseq(phy.otu, phy.taxonomy, phy.info)

comp_barplot(phy, tax_level = "Phylum", n_taxa = 11, label = "inf.sample_short", sample_order = sample_names(phy)) + facet_grid2(inf.id ~ inf.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)))

plot_test

teunbrand commented 1 week ago

Sorry for bothering with this again, but can this problem be shown with just base R + ggplot2 + ggh4x? I'm aim to have ggh4x broadly compatible with ggplot2 and its extension systems, but I cannot guarantee compatibility with every function that wraps ggplot2 like comp_barplot().

DanaBlu commented 1 week ago

Hello @teunbrand, the problem has already been solved! Sorry, I completely forgot to close the topic! By adding x=sample_short the problem is gone. So ggplot2 + gg4hx + comp_barplot are all compatible with each other. Thank you for your support, I will close this issue now. Best, Dana