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

Replace the broken example "Cumulative Wikipedia Donations" #2424

Closed
harabat opened this issue Mar 2, 2021 · 3 comments · Fixed by #3440
Closed

Replace the broken example "Cumulative Wikipedia Donations" #2424

harabat opened this issue Mar 2, 2021 · 3 comments · Fixed by #3440

Comments

@harabat
Copy link
Contributor

harabat commented Mar 2, 2021

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 harabat added the bug label Mar 2, 2021
@jakevdp
Copy link
Collaborator

jakevdp commented Mar 2, 2021

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?

@joelostblom
Copy link
Contributor

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

@dsmedia
Copy link
Contributor

dsmedia commented Jun 18, 2024

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.

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

Successfully merging a pull request may close this issue.

5 participants