Skip to content

Fix legend ordering in br_show_survival_curves to show groups in logical order#48

Merged
ShixiangWang merged 7 commits into
mainfrom
copilot/fix-47
Aug 9, 2025
Merged

Fix legend ordering in br_show_survival_curves to show groups in logical order#48
ShixiangWang merged 7 commits into
mainfrom
copilot/fix-47

Conversation

Copilot AI commented Aug 9, 2025

Copy link
Copy Markdown
Contributor

The legend groups in br_show_survival_curves were displaying in alphabetical order instead of the intended logical order. This caused confusing displays where:

  • Chinese labels: "中风险" (Medium Risk) appeared before "低风险" (Low Risk)
  • Q labels: "Q10", "Q11" appeared before "Q2", "Q3"
  • Default labels: "High Risk" appeared before "Low Risk" and "Medium Risk"

Root Cause

The issue was in line 658 of R/04-show.R where factor levels were converted to character strings:

plot_data$group <- gsub(".*=", "", plot_data$group)

This lost the intended factor level ordering, causing ggplot2 to sort legend items alphabetically.

Solution

After cleaning group names, the fix preserves the original factor level order:

# Clean group names while preserving factor level order
plot_data$group <- gsub(".*=", "", plot_data$group)

# Convert back to factor with the same level order as the original group_labels
# to ensure correct legend ordering
plot_data$group <- factor(plot_data$group, levels = group_labels)

Results

Now legends display in the correct logical order:

  • 3 groups: "Low Risk" → "Medium Risk" → "High Risk"
  • 5+ groups: "Q1" → "Q2" → "Q3" → "Q4" → "Q5"
  • Chinese labels: "低风险" → "中风险" → "高风险"

Testing

Added comprehensive test coverage in test-survival-legend-order.R that validates:

  • Correct factor level ordering for default 3-group scenarios
  • Proper Q-label ordering for 5+ group scenarios
  • All existing survival functionality continues to work

Fixes #47.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits August 9, 2025 08:13
…l_curves

Co-authored-by: ShixiangWang <25057508+ShixiangWang@users.noreply.github.com>
Co-authored-by: ShixiangWang <25057508+ShixiangWang@users.noreply.github.com>
Co-authored-by: ShixiangWang <25057508+ShixiangWang@users.noreply.github.com>
Copilot AI changed the title [WIP] groups in result plot of br_show_survival_curves in figure legend not shown as order of the factor Fix legend ordering in br_show_survival_curves to show groups in logical order Aug 9, 2025
Copilot AI requested a review from ShixiangWang August 9, 2025 08:21
Updated progress bar and coverage output in README.md, refreshed the associated figure, and deleted an unused Rplots.pdf file from tests.
@ShixiangWang ShixiangWang marked this pull request as ready for review August 9, 2025 08:32
@ShixiangWang ShixiangWang merged commit fd103f8 into main Aug 9, 2025
6 checks passed
@ShixiangWang ShixiangWang deleted the copilot/fix-47 branch January 18, 2026 05:37
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.

groups in result plot of br_show_survival_curves in figure legend not shown as order of the factor

2 participants