altair icon indicating copy to clipboard operation
altair copied to clipboard

Replace the broken example "Cumulative Wikipedia Donations"

Open harabat opened this issue 4 years ago • 2 comments

Issue 1

The example "Cumulative Wikipedia Donations" is broken, and goes against the Contributing guidelines, namely Example code should not require downloading external datasets, so I'd suggest replacing it with another chart.

Issue 2

I spent more than an hour yesterday reading through a number of StackOverflow questions trying to style a facet plot (anchoring of titles and order of plots). Believing that my issue was not unique and that finding the solution was not straightforward, I wanted to post a question with my own answer on StackOverflow, to consolidate the information in one place, but maybe I can include it into the new chart?

Suggested solution

The following code provides an illustration of a cumulative sum (to replace the broken chart), but also clarifies the styling of titles, headers, order, and marks of a facet plot, and gives an example of combining predicates with the filter transform.

import altair as alt
import vega_datasets as data

source = data.disasters()
columns_sorted = ['Drought', 'Epidemic', 'Earthquake', 'Flood']

alt.Chart(source)\
.mark_line(interpolate='basis')\
.encode(
    alt.X('Year:O', title=None),
    alt.Y('cumulative:Q', title=None),
    alt.Color('Entity:N', legend=None)
)\
.properties(width=300, height=150)\
.facet(
    facet=alt.Facet(
        'Entity:N',
        title=None,
        sort=columns_sorted,
        header=alt.Header(labelAnchor='start', labelFontStyle='italic')
    ),
    title={
        'text': ['Cumulative casualties by type of disaster', 'in the 20th century'],
        'anchor': 'middle'
    },
    columns=2
)\
.resolve_scale(y='independent')\
.transform_filter(
    {'and': [
        alt.FieldOneOfPredicate(field='Entity', oneOf=columns_sorted), 
        alt.FieldRangePredicate(field='Year', range=[1900, 2000])
    ]}
)\
.transform_window(cumulative='sum(Deaths)', groupby=['Entity'])

faceted_plot_with_cumsums

Can I make a PR, including this into Other Charts maybe, with the title Faceted plot of cumulative sums with custom order and title styling?

harabat avatar Mar 02 '21 13:03 harabat

Looks great! A PR in the case studies category would be perfect.

Would a quantitative x-encoding lead to better axis labels, perhaps with a label format to preven commas added in the years?

jakevdp avatar Mar 02 '21 20:03 jakevdp

This Wikipedia chart was removed in #2625 . I am happy to review PR if you want to add your chart to the gallery @harabat

joelostblom avatar Sep 21 '23 16:09 joelostblom

I've submitted a pull request (#3440) to resolve this issue. The work on this fix was done by @harabat. I made the final tweaks to complete the solution.

dsmedia avatar Jun 18 '24 19:06 dsmedia