vega / altair

Declarative statistical visualization library for Python
https://altair-viz.github.io/
BSD 3-Clause "New" or "Revised" License
9.35k stars 794 forks source link

Make it possible to layer a facet chart #2878

Open mattijn opened 1 year ago

mattijn commented 1 year ago

Suggestion for enhancement based on the discussion that started here: https://github.com/altair-viz/altair/pull/2874:

The following altair specification will fail:

import altair as alt
from vega_datasets import data
stocks = data.stocks.url

c1 = alt.Chart(stocks, height=40, width=80).encode(
    x='date:T',
    y='price:Q',
    column='symbol:N'
)

c1.mark_line() + c1.mark_point()

But this works:

c1.mark_line(points=True)

The last one works, because Vega-Lite is actually¹ translating this into:

c2 = alt.Chart(stocks, height=40, width=80).encode(
    x='date:T',
    y='price:Q'
)

(c2mod.mark_line() + c2.mark_point()).facet(column='symbol:N')

Is it feasible or desirable to do the same lifting on the Altair side?

¹ In the Vega-Lite specification it is just mark_line(point=True), but if you observe the Extended Vega-Lite spec in the Vega-Editor it does creates a layered chart and shifts the column encoding channel to a top-level facet operator, see Open the Chart in the Vega Editor, click bottom-left next to Compiled Vega.

Originally posted by @mattijn in https://github.com/altair-viz/altair/issues/2874#issuecomment-1421462825:

ChristopherDavisUCI commented 1 year ago

Hi @mattijn, my first impression is to be pretty hesitant to add this kind of extra feature to Altair, just because these extras seem to cause the most difficulties in regards to maintenance (like allowing parameters to be added in more places than Vega-Lite does). Do you see any reason this would be easier to accomplish in Altair vs in Vega-Lite? If not, I'd vote for trying to add it to Vega-Lite instead if it's a feature we'd like.

I believe this is where the translation you mentioned for point=True happens in Vega-Lite: https://github.com/vega/vega-lite/blob/next/src/normalize/pathoverlay.ts

mattijn commented 1 year ago

Even better if we can solve it on the vega-lite side.

I still have to get used to a short vega-lite specification and the possibility of expanding or extending a vega-lite specification.

In hindsight maybe we could have exploited that more for the parameters as well.

ChristopherDavisUCI commented 1 year ago

I agree! Would you like to open an issue in Vega-Lite, partially to hear if this is something they see as plausible?

joelostblom commented 1 year ago

My vote would also be to try to solve things like this that are within the scope of Vega-Lite on their side.

ChristopherDavisUCI commented 1 year ago

I'll repost in this message if I find a more fleshed out explanation, but here is a brief one by Jake: https://stackoverflow.com/a/64422039

A layer chart is not allowed to contain a facet encoding... (the reason for this is that the semantics of layers containing incompatible facets is unclear).

kanitw commented 1 year ago

+1 to the answer above. (I'm a co-author of Vega-Lite.)

mattijn commented 1 year ago

Thanks @ChristopherDavisUCI for bringing info why this is currently not supported. Thanks @kanitw for chiming in!

I hope you don't mind if I ask one more question. I understand this will not work when there are different incompatible facets defined within the subcharts (that should raise an error) but in the case of above, the facets are identical and compatible. Will this trigger the same semantic issue?