vega / altair

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

Error concatenating grouped bar charts: Javascript Error: Undefined data set name #1727

Closed zhanjiezhu closed 1 year ago

zhanjiezhu commented 4 years ago

When I tried to concatenate two grouped bar charts, it throws error "Javascript Error: Undefined data set name: "scale_concat_1_child_main"

Example can be reproduced using the grouped bar chart example (https://altair-viz.github.io/gallery/grouped_bar_chart.html)

import altair as alt
from vega_datasets import data
source = data.barley()
test = alt.Chart(source).mark_bar().encode(
    x='year:O',
    y='sum(yield):Q',
    color='year:N',
    column='site:N'
)
test & test
jakevdp commented 4 years ago

This is a known bug in Vega-Lite; see https://github.com/vega/vega-lite/issues/2760

torresweb commented 4 years ago

Hi, I am using vega-lite 4.13.1 and with the same problem. I am not sure if #2760 should fix it or not. Any workaround?

jakevdp commented 4 years ago

vega/vega-lite#2760 has not been fixed, so it is still an issue.

jesnie commented 4 years ago

I'm running into a problem that looks very much like this. I must admit I don't understand why #2760 relates to this. #2760 talks about generic specs inside facets, but I want to concatenate bar charts. I don't suppose you can give me a hint about what's going on here, so that I might work around it?

jakevdp commented 4 years ago

I don't think there's any workaround: Vega-Lite currently does not support concatenation or faceting of some compound charts.

torresweb commented 4 years ago

I found a workaround for that. You need to create each group of bar columns with a graph and then concatenate these graphs, sharing Y scale and hiding/unhiding this axis.

ibayer-ny commented 3 years ago

@torresweb

I don't understand your workaround description. Could you please provide an example? Maybe transforming the initial example in this issue would be a quick way to demonstrate it.

joelostblom commented 3 years ago

Details of why this is not working can be found in https://github.com/vega/vega-lite/issues/4680, specifically this comment. It only happens when VegaLite tries to reference the same data set in two concatenated charts. A workaround is to resort the data (e.g. df.sample(df.shape[0])), so that VegaLite creates to separate data source references:

import altair as alt
from vega_datasets import data
source = data.barley()
test = alt.Chart(source).mark_bar().encode(
    x='year:O',
    y='sum(yield):Q',
    color='year:N',
    column='site:N'
)
test & test.properties(data=source.sample(source.shape[0]))

image

The obvious caveat is that if you are doing something that depends on the order of your data, this is not a good idea, but for concatenated charts this should be less of an issue as long as the dimensions are shared.

@zhanjiezhu Would you mind closing this issues as it is already tracked upstreams in Vega-Lite in the linked issues and there is no fix needed in Altair?

joelostblom commented 1 year ago

This will be fixed once https://github.com/altair-viz/altair/pull/2871 is merged

binste commented 1 year ago

Fixed in Altair 5.