Skip to content

Update plot.means_by_group.R to keep order of categories#451

Open
elinw wants to merge 2 commits into
easystats:mainfrom
elinw:patch-1
Open

Update plot.means_by_group.R to keep order of categories#451
elinw wants to merge 2 commits into
easystats:mainfrom
elinw:patch-1

Conversation

@elinw

@elinw elinw commented Jul 8, 2026

Copy link
Copy Markdown

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes a double assignment typo and converts the Category column to a factor in plot.means_by_group.R. The reviewer noted that using as.factor() will sort the categories alphabetically, potentially losing their original order. They suggested explicitly setting the factor levels using unique(trimmed$Category) to preserve the original order and drop unused levels.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread R/plot.means_by_group.R
x_long <- do.call(rbind, x)
trimmed <- trimmed <- x_long[x_long$Category != "Total", ]
trimmed <- x_long[x_long$Category != "Total", ]
trimmed$Category <- as.factor(trimmed$Category)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Using as.factor(trimmed$Category) will coerce the category column to a factor, but by default, R's as.factor() sorts the levels alphabetically. If the categories in the original data are not in alphabetical order, this will lose their original order of appearance, which defeats the main objective of this pull request.\n\nTo preserve the original order of categories as they appear in the data, you should explicitly set the levels using unique(trimmed$Category). This also has the benefit of dropping any unused levels (such as 'Total' if it was already a factor), preventing empty categories from appearing on the plot.

  trimmed$Category <- factor(trimmed$Category, levels = unique(trimmed$Category))

@strengejacke

Copy link
Copy Markdown
Member

Please see the Gemini suggestion, as as.factor(trimmed$Category) indeed uses alphabetical order.

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.

2 participants