Skip to content

Draw groups in a categorical column's own order, so reorder_ident has an effect - #149

Merged
shanikawm merged 2 commits into
mainfrom
fix/plot-group-order-143
Oct 7, 2026
Merged

shanikawm merged 2 commits into
mainfrom
fix/plot-group-order-143

Conversation

@shanikawm

@shanikawm shanikawm commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #143.

In Seurat the order of a factor's levels decides the order of the groups in DotPlot and VlnPlot, and ReorderIdent changes the plots by changing the levels. dot_plot and vln_plot sorted the groups themselves, whatever the categories said, so a lineage-ordered dot plot was not possible and reorder_ident had no effect on them.

What changed

A shared helper, _group_levels, follows a categorical column's categories, or the active identity's, and six plots use it: dot_plot and vln_plot as reported, and dim_plot, feature_scatter, do_heatmap and ridge_plot, which sort their groups with the same line. The issue says the other plots were not checked; they were and they share the defect. Their colours follow, as hue_pal is assigned in level order in R.

The one design decision. obj.idents is always a pd.Categorical, and pd.Categorical(values) and rename_idents give it pandas' default categories: string-sorted, so "1", "10", "2". Honouring those blindly would undo the numbers-first ordering that ident_sort_key and its six existing tests deliberately provide ("a dataset with eleven or more clusters would come back in a different cluster order from Seurat's"). So categories that are only the sorted order every categorical starts with are not treated as an order anyone chose and are sorted as before, numbers first. Any other order is followed. reorder_ident and a reorder_categories call both land on the second branch.

find_clusters already orders its categories numbers then names, so nothing changes for an existing workflow. No tutorial sets a category order. Categories with no cells are not drawn, as ggplot drops unused levels, and a missing value is still drawn last.

Verification

  • The issue's reproduction now gives Zeta, Mu, Alpha for dot_plot, vln_plot, and after reorder_ident.
  • 21 new tests in tests/test_plot_group_order.py: all six plots, through both the active identity and a metadata column, plus reorder_ident, plus the helper's edge cases.
  • Mutation-tested six ways. Ignoring categories (the original bug) fails 18 tests. Honouring the default categories too fails the six existing numbers-first tests, which is the evidence for the exception above rather than an assumption. The rest each fail their own tests: unused categories drawn, a missing value dropped, one site left unconverted, and ridge_plot losing its reversal.
  • "Nothing changes for an existing workflow" checked, not assumed. I regenerated the figures of three tutorials on this branch (PBMC 3k, CITE-seq and SCTransform: 30 figures). git status afterwards was empty, so every figure and tracked handoff file is byte-identical to the committed one.
  • Full suite locally: 1761 passed, 30 skipped. ruff unchanged on plotting.py (133), mypy at its 4 baseline errors.

Not changed

image_dim_plot and spatial_dim_plot use a plain sorted(), so they also ignore a categorical's order and put "10" before "2". That is an older and different ordering from the one above, and changing it would move committed spatial tutorial figures. ROADMAP.md lists it, and replaces its vln_plot group-order item, whose group-order half this fixes; the median-bar half remains.

No release is needed for this change on its own. The CHANGELOG entry is under [Unreleased] → Fixed.

🤖 Generated with Claude Code

shanikawm and others added 2 commits October 7, 2026 21:47
… an effect (#143)

In Seurat the order of a factor's levels decides the order of the groups in
DotPlot and VlnPlot, and ReorderIdent changes the plots by changing the levels.
dot_plot, vln_plot, dim_plot, feature_scatter, do_heatmap and ridge_plot sorted
the groups themselves with ident_sort_key whatever the categories said, so a
lineage-ordered dot plot was not possible and reorder_ident did nothing to them.

A shared helper now follows a categorical column's categories, or the active
identity's. Categories that are only the sorted order every categorical starts
with are not an order anyone chose: pd.Categorical(values) and rename_idents
leave "1", "10", "2", and those still sort numbers first as before. That is what
keeps the six existing numbers-first tests green, and making the helper honour
the default order fails all of them. Categories with no cells are not drawn, and
a missing value, which pandas 2 and 3 spell differently after astype(str), is
still last.

find_clusters already orders its categories numbers then names, and no tutorial
sets a category order, so no existing workflow changes. The two spatial dim plots
use a plain sorted() and are left alone, since changing them would move committed
spatial figures; ROADMAP.md says so.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@shanikawm
shanikawm merged commit d92d160 into main Oct 7, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dot_plot and vln_plot ignore the category order of the group_by column

1 participant