HelenaLC / CATALYST

Cytometry dATa anALYsis Tools
66 stars 30 forks source link

plotAbundances Adjusts Stacked Bar Width According to Grouping #388

Closed LukaTandaric closed 5 months ago

LukaTandaric commented 5 months ago

Dear Helena,

I would like to use plotAbundances to visualize the the overall differences in the metacluster proportions between multiple groups of samples. If I do not make group_by NULL, i.e. when I use this function:

plotAbundances(
    my_SCE,
    k = "my_metacluster_merging",
    by = "sample_id",
    group_by = "survival",
    col_clust = FALSE
    )

the plot comes out like this:

image

where each of the N groups gets one Nth of the width of the plot, resulting in the stacked bar plots in each of the groups having different widths. This gives ridiculously disproportioned graphs the more unequal the numbers of group members is, e.g.:

image

Is it possible to keep the grouping, but have all stacked bar plots have the same width? In other words, is it possible to keep the increased spacing between groups, but not make the width of the plot each group takes proportional to the number of groups?

Kind regards. Luka

HelenaLC commented 5 months ago

Not very "elegant", but you could try temporarily setting sample_ids to group_ids to achieve the desired result (if I understood correctly). Minimal example below:

library(CATALYST)
data(PBMC_fs, PBMC_panel, PBMC_md)
sce <- prepData(PBMC_fs, PBMC_panel, PBMC_md)
sce <- cluster(sce, verbose=FALSE)

plotAbundances(sce, 
    k="meta8", 
    by="sample_id",
    group_by="condition")

image

tmp <- sce
tmp$sample_id <- tmp$condition
plotAbundances(tmp, 
    k="meta8", 
    by="sample_id",
    group_by="condition")

image

LukaTandaric commented 5 months ago

Dear Helena,

thank you for your prompt response. While your suggestion does indeed make the width of the stacked bar plots equal between groups, it does so at the cost of unifying all of the data within each group, removing insight into intra-group variability. What I am looking for is a way to keep the stacked bar of each sample separate and visible, but have all stacked bars of equal width. Something like this I threw together in MS Paint:

image

HelenaLC commented 5 months ago

Aha, apologies, misunderstood the Q - so this is a purely aesthetically thing if I understand correctly. It's actually a ggplot-related issue; by default facet_wrap(..., scales="free_x"), which is used here, adjusts bar widths such that each panel has the same overall width. You can overwrite the faceting as follows to achieve the desired result (space="free" is the key here):

library(ggplot2)
library(CATALYST)

data(PBMC_fs, PBMC_panel, PBMC_md)
sce <- prepData(PBMC_fs, PBMC_panel, PBMC_md)
sce <- cluster(sce, verbose=FALSE)

sub <- filterSCE(sce, !sample_id %in% c("Ref1", "Ref3")) # subset for demo
plt <- plotAbundances(sub, k="meta8", by="sample_id", group_by="condition") 
plt + facet_grid(~ condition, scales="free_x", space="free")

image