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

Custom sort order #2915

Closed
kanitw opened this issue Aug 23, 2017 · 14 comments
Closed

Custom sort order #2915

kanitw opened this issue Aug 23, 2017 · 14 comments

Comments

@kanitw
Copy link
Member

kanitw commented Aug 23, 2017

Currently, for unit encoding, we can specify custom order for normal (non-facet) encoding channel by specifying scale.domain directly.

However, we don't have similar support for

  1. facet channel
  2. stack (Allow specifying specific order of stack in stacked charts #1734)

Since facet does not have scale domain, we can't use similar syntax. But facet has sort property like other encoding channel. Thus, I think we should make sort support a custom array, which derives a formula like this so that it's safer than setting domain directly (as domain would still include omitted values).

For stack, we can then also make stack order orders by color/detail (or any other stack-by field) by default.

@kanitw kanitw added this to the 2.x.x Features & Patches milestone Aug 23, 2017
@kanitw kanitw modified the milestones: 2.x.x Features & Patches, 2.x Visual Encoding Patches Sep 28, 2017
@kanitw kanitw changed the title More general solution for custom sort order Custom sort order Sep 28, 2017
@curran
Copy link

curran commented Nov 3, 2017

Great idea! Being able to specify facet ordering seems like an essential feature.

FWIW, as a Vega-Lite noob, I tried the following:

        "column": {
          "field": "variable",
          "type": "ordinal",
          "scale": {
            "type": "ordinal",
            "domain": ["Online news sites","Social media","Printed newspapers","TV"]
          }
        },

Then I tried this (your proposal here):

        "column": {
          "field": "variable",
          "type": "ordinal",
          "sort": ["Online news sites","Social media","Printed newspapers","TV"]
        },

I think the above syntax would be really great.

As a separate thought, if the notion of "ordinal" is there in Vega, and is distinct from "nominal", wouldn't it be nice to be able to specify in the data definition the natural ordering of a column?

@kanitw
Copy link
Member Author

kanitw commented Nov 3, 2017

The same data field can be treated as ordinal or nominal so it makes more sense to specify it as sort of a data field definition like proposed above.

@curran
Copy link

curran commented Nov 3, 2017

Makes sense. Thanks for your reply.

@domoritz
Copy link
Member

domoritz commented Nov 3, 2017

Column used to be implemented with scales but now we use Vega layouts (which is more flexible and produces nicer output). Does the code snippet with scale pass JSON schema validation in the editor?

@curran
Copy link

curran commented Nov 6, 2017

@domoritz No, it does not pass schema validation.

I discovered the validation in the editor only after trying the configuration (rather blindly) in my code.

@domoritz
Copy link
Member

domoritz commented Nov 6, 2017

Okay, that's what I wanted. When we implement this feature, you'll use sort as a top-level property of the encoding definition (see issue description).

@curran
Copy link

curran commented Nov 6, 2017

Sounds good!

If I had some time to dig and try to implement this, where would be the place to start?

@domoritz
Copy link
Member

domoritz commented Nov 6, 2017

I'd first check how to sort facets in Vega, then see where in Vega-Lite we already generate the order property, and then lastly I'd write some code to let users override the default (+tests).

@kanitw
Copy link
Member Author

kanitw commented Nov 6, 2017

I would say start with non-facet channel like x and y first.

Try to see how you can derive a new field by generating a Vega formula transform and then use the new field to sort scale.

For facet channel, then you would similar use the same formula flow, but apply it to facet mark definition groups and headers.

Note that there is also the stack case that you should handle for consistency.

To make review easier, u might want to just tackle the simplest case which is the non-facet channels first.

@curran
Copy link

curran commented Nov 7, 2017

Maybe I'm missing something, but as far as I can tell, non-facet channels like X and Y already support custom sort ordering by explicitly setting the scale domain, right?

@domoritz
Copy link
Member

domoritz commented Nov 7, 2017

Yes! The difference is that faceting doesn't use scales but Vega layouts instead.

@kanitw
Copy link
Member Author

kanitw commented Nov 9, 2017

Yes, we already support scale domain for non-facet channels.

But if sort in facet can be an array of values, I think sort in non-facet channels should support array of values as well for consistency.

Another thing that is better about the proposal for sort array above than scale.domain is that if certain value is accidentally omitted from the sort, the underlying ordinal scales could still contain these values. (For example, sort: ["a", "b", "c"] like you specify that the scale should start with a, b, and c, and other values come later.) Meanwhile, if you set domain directly, only domain values specified would be include in the scale.

@curran
Copy link

curran commented Nov 9, 2017

Aha that's a great point!

@kanitw
Copy link
Member Author

kanitw commented Mar 18, 2018

Closing this as the core issue for custom sort order will be fixed in #3423.

For stack case, I'll re-open #1734 so we can keep track of it.

We also should support sort array for facet as a part of #2176.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants