Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature to change x-axis order for boxplots #483

Merged
merged 7 commits into from
Jul 17, 2023
Merged

add feature to change x-axis order for boxplots #483

merged 7 commits into from
Jul 17, 2023

Conversation

Tobias-Thomas
Copy link
Contributor

Fixes #409, by adding by as a new optional parameter to boxplots. This by is the same, one would use for sort. It's default parameter is the identity.
Before merge, can someone please tell me, where I can add the documentation for this new parameter?
Thanks in advance.

@sethaxen
Copy link
Member

@Tobias-Thomas thank you for the contribution, and I'm very sorry for the late review! I know I recommended the keyword by, but in retrospect, I think that's too generic of a name. What about something like sort_labels_by, or perhaps you have a better suggestion?

@Tobias-Thomas
Copy link
Contributor Author

Oh, no problem. The only other idea, I would have is sort_x_by, since the parameter it sorts is called x.
What do you think?

@indymnv
Copy link

indymnv commented Nov 15, 2022

I was searching for a way to sort boxplots, but still is not solved or I am wrong?, is there a way I can help to fix this?

@Tobias-Thomas
Copy link
Contributor Author

The fix I implemented was essentially quite easy, but as you can see above, we were still unsure about the name of this parameter, and I think we need a JuliaPlots member to decide on that.

@indymnv
Copy link

indymnv commented Nov 16, 2022

What about order to keep a similar idea like seaborn, sort_labels_by also can be OK, sort_x_by can be a little bit confused if later there is an implementation using horizontal boxplots.

@Tobias-Thomas
Copy link
Contributor Author

Tobias-Thomas commented Nov 16, 2022

Yea, I understand that point. Out of those options, I would prefer sort_labels_by.
I will do a commit in my branch (switching to this name) later this day and maybe we could ping Seth again, to see if he approves.

@codecov-commenter
Copy link

codecov-commenter commented Nov 16, 2022

Codecov Report

Patch and project coverage have no change.

Comparison is base (90b5f4f) 26.52% compared to head (3164468) 26.52%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #483   +/-   ##
=======================================
  Coverage   26.52%   26.52%           
=======================================
  Files          20       20           
  Lines        1248     1248           
=======================================
  Hits          331      331           
  Misses        917      917           
Impacted Files Coverage Δ
src/boxplot.jl 0.00% <0.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@Tobias-Thomas
Copy link
Contributor Author

@sethaxen, I updated the name and merged it into the current master. Is there anything left to do for me in this PR?

@jonathan-durbin
Copy link

Hi, any updates on this? I'm running into this issue while trying to figure out how to sort the x axis in a boxplot.

@Tobias-Thomas
Copy link
Contributor Author

I think the fix would be ready for merge, but I do not know who to ping, to review the PR..

@indymnv
Copy link

indymnv commented Jul 14, 2023

Maybe @sethaxen , can help us with this PR

src/boxplot.jl Outdated Show resolved Hide resolved
Copy link
Member

@sethaxen sethaxen left a comment

Choose a reason for hiding this comment

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

Sorry @Tobias-Thomas for the delay! Everything looks good to me! Thanks!

@sethaxen sethaxen merged commit 338740e into JuliaPlots:master Jul 17, 2023
11 checks passed
@Vaeryus
Copy link

Vaeryus commented Jul 24, 2023

So how do I use sort_labels_by now? Can I just put in an order for the x axis like:
sort_labels_by= [ "B" "C" "A"] or how would I use it?

@Tobias-Thomas
Copy link
Contributor Author

Tobias-Thomas commented Jul 24, 2023

So how do I use sort_labels_by now? Can I just put in an order for the x axis like: sort_labels_by= [ "B" "C" "A"] or how would I use it?

No, the function uses the same interface as Base.sort. That means that values can be sorted by a transformation of their values. So you need to write a function, which transforms the values into a space where they are sorted the way you want it.

@artkel
Copy link

artkel commented Sep 25, 2023

Can someone please explain how the category sorting for boxplots now works? It is not quite clear to me from the previous comment. Would be nice to see an example. Thanks!

@artkel
Copy link

artkel commented Sep 25, 2023

Can someone please explain how the category sorting for boxplots now works? It is not quite clear to me from the previous comment. Would be nice to see an example. Thanks!

Ok, I think I got it. The example below explains the logic. It worked for me.

# Define a function to map the categories to an order
function order_label(label::String)
    order_dict = Dict("B" => 1, "C" => 2, "A" => 3, "D" => 4)
    return get(order_dict, label, 0)  # Return 0 for any unexpected labels
end

# Now use this function in the sort_labels_by parameter
@df dataset_name boxplot(
    string.(:CategorcalVar),
    :NumericalVar,
    outliers=true,
    sort_labels_by=order_label,
    fillalpha=0.75,
    linewidth=1,
    legend=false
)

It would be nice to have a similar parameter for the violin plot.

@mkborregaard
Copy link
Member

Before this PR the way to do that was to use levels! on the CategoricalArray x vector to define the sorting order. Is this really an improvemet on that?

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.

Expose sorting options for boxplots (and other plots?)
8 participants