2

I want to display counts of a certain feature according to the year with an Altair Chart and I want to do it by selecting year as an option of a dropdown selector. It works fine with the 'None' option (so it selects the whole dataset) but nothing is displayed when I switch year.

Issue

You can see below when it works and when it does not : When it works When it doesn't work

Code

Here is my code :

year_dropdown = alt.binding_select(
    options = [None] + list(africa_metadata.year.unique()),
    labels = ["All"] +list(africa_metadata.year.unique()),
    name="Année:"
)


year_selector = alt.selection_point(
    fields = ['year'],
    bind = year_dropdown,
    name="year_selection"
)
(
    alt.Chart(africa_metadata).transform_aggregate(
        count='count()',
        groupby=['lineage']
    ).transform_window(
        rank_lineage="rank(count)",
        sort=[alt.SortField("count", order="descending")]
    ).add_params(
        year_selector
    ).transform_filter(
        year_selector 
    ).transform_filter(
        'datum.rank_lineage<=10'
    ).mark_bar(
        
    ).encode(
       alt.X('lineage:N', sort=alt.EncodingSortField(op='count', order='descending')),
       alt.Y('count:Q'),
        alt.Color("lineage:N")
    )
)

Dataset

Here is a fraction of my dataset : dataset.head(3)

    accession   continent   completeness    length  lineage     date    year    month   country     region
0   PP518589.1  Africa  COMPLETE    29822   A   2020-05-09  2020    05  South Africa    None
242     OQ050567.1  Africa  COMPLETE    29859   A   2020-05-08  2020    05  Nigeria     None
243     OQ050560.1  Africa  COMPLETE    29823   A   2020-05-10  2020    05  Nigeria     None
244     OQ050510.1  Africa  COMPLETE    29875   A   2020-05-08  2020    05  Nigeria     None
245     OQ050499.1  Africa  COMPLETE    29824   A   2020-04-26  2020    04  Nigeria     None
246     OQ050495.1  Africa  COMPLETE    29841   A   2020-04-19  2020    04  Nigeria     None
247     OQ050456.1  Africa  COMPLETE    29845   A   2020-04-23  2020    04  Nigeria     None
248     OQ050409.1  Africa  COMPLETE    29855   A   2020-03-29  2020    03  Nigeria     None
249     OQ050383.1  Africa  COMPLETE    29831   A   2020-03-25  2020    03  Nigeria     None
250     OQ050376.1  Africa  COMPLETE    29768   A   2020-03-29  2020    03  Nigeria     None

Can you help me please ? I don't know why it does not work. Tell me if you need more info.

1 Answer 1

1

The first aggregate transform returns a new aggregated dataset, which only containing the columns referenced in the transform. This means that the year column is removed from the data at that step, which is why it can't be used in the filter transform afterwards. Move the filter transform ahead of the aggregate transform instead:

    alt.Chart(africa_metadata).transform_filter(
        year_selector 
    ).transform_aggregate(
        count='count()',
        groupby=['lineage']
    )...
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.