Coder Social home page Coder Social logo

Comments (5)

trevismd avatar trevismd commented on July 19, 2024

Yes, statannotations works well with the hue argument in FacetGrid too but pairs are defined at plot level, so the xand hue should be the same across plots, which is not the case for you here.
The "today" solution for you would perhaps be to define subplots to create your desired layout and then use the "regular" plot + stannanotations on each subplot as you'll have different pairs to compare in each one.
(See this post https://www.statology.org/seaborn-subplots/)
Something like this:

annot = Annotator.get_empty_annotator()
plot_params = {
    'x':'receptor',
    'y':'expression',
    'hue':'roi',
    'hue_order':['roi','non-roi'],
}
receptor_groups = expression_long['receptor_group'].unique()
sns.color_palette("Paired")
with sns.plotting_context("paper"):
    fig, axes = plt.subplots(4, 2, figsize=(20,  30))
    for ax_row_idx, ax_row in enumerate(axes):
        for ax_col_idx, ax in enumerate(ax_row):
            ax_idx = ax_row_idx * 2 + ax_col_idx
            if ax_idx >= len(receptor_groups):
                ax.set_axis_off()
                continue
            ax_group = receptor_groups[ax_idx]
            expression_long_group = expression_long.loc[expression_long.receptor_group==ax_group, :]
            group_receptors = expression_long_group['receptor'].unique()

            sns.boxplot(ax=ax, data=expression_long_group, **plot_params)
            annot.new_plot(
                ax,
                data=expression_long_group,
                pairs=[((receptor,'roi'),(receptor,'non-roi')) for receptor in group_receptors],
                plot='boxplot',
                **plot_params
            ).configure(test="Mann-Whitney").apply_and_annotate()
            
            ax.set_title(ax_group)
            if len(group_receptors) > 10:
                ax.set_xticklabels(labels=ax.get_xticklabels(), rotation=45)
plt.show()

Which results in this approximation of your diagram :) Tweaking spacing and legends, groups ordering, maybe using the last row for your larger group (look for add_subplot) should enable you to get there though.

expression

from statannotations.

JohannesWiesner avatar JohannesWiesner commented on July 19, 2024

Perfect, thanks so much for the code! Then only issue that I see right now, is that the multiple comparisons correction is now done within each group and not over all receptors right?

from statannotations.

trevismd avatar trevismd commented on July 19, 2024

Of course!

This is correct, but it is also the case with plot_and_annotate_facet (I should make that clearer).

Depending on the correction method, you can fix this by either

  • passing a num_comparisons option (like for Bonferonni) or
  • running the stats beforehand and then use set_pvalues instead on each subplot.
    In that case, you'll have to
    1. Compute all the pairs you use in the plots
    2. Plot a chart with receptors of all groups, but using the pairs described above
    3. Collect the pvalues for each pair
    4. Use these when you're making the "real" plot as drafted above.

from statannotations.

JohannesWiesner avatar JohannesWiesner commented on July 19, 2024

This is correct, but it is also the case with plot_and_annotate_facet (I should make that clearer).

Ah, interesting! Yes, I think making that clearer would help a lot :)

passing a num_comparisons option (like for Bonferonni)

That sounds like a good idea, but would only work for methods, that do not need to know all the p-values before-hand right?

from statannotations.

JohannesWiesner avatar JohannesWiesner commented on July 19, 2024

Of course!

This is correct, but it is also the case with plot_and_annotate_facet (I should make that clearer).

Depending on the correction method, you can fix this by either

  • passing a num_comparisons option (like for Bonferonni) or

  • running the stats beforehand and then use set_pvalues instead on each subplot.
    In that case, you'll have to

    1. Compute all the pairs you use in the plots
    2. Plot a chart with receptors of all groups, but using the pairs described above
    3. Collect the pvalues for each pair
    4. Use these when you're making the "real" plot as drafted above.

Would love if this would work out-of-the-box! The general idea here is that you often want to plot stuff using facet_grid for better readability but you don't want the multiple comparison to be done within each subplot.

from statannotations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.