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

docs: Adds example Calculate Residuals #3625

Merged
merged 2 commits into from
Oct 3, 2024

Conversation

dangotbanned
Copy link
Member

@dangotbanned dangotbanned commented Oct 2, 2024

Recreation of vega-lite example https://vega.github.io/vega-lite/examples/joinaggregate_residual_graph.html

I noticed the last example on Vega Theme Test didn't have an altair equivalent.
This PR will also bring us one step closer to #3519 (comment)

Really like how concise the methods syntax version ended up

calculate_residuals

import altair as alt
from vega_datasets import data

imdb_rating = alt.datum["IMDB_Rating"]
source = data.movies.url

chart = (
    alt.Chart(source)
    .mark_point()
    .transform_filter(imdb_rating != None)
    .transform_filter(
        alt.FieldRangePredicate("Release_Date", [None, 2019], timeUnit="year")
    )
    .transform_joinaggregate(Average_Rating="mean(IMDB_Rating)")
    .transform_calculate(Rating_Delta=imdb_rating - alt.datum.Average_Rating)
    .encode(
        x=alt.X("Release_Date:T").title("Release Date"),
        y=alt.Y("Rating_Delta:Q").title("Rating Delta"),
        color=alt.Color("Rating_Delta:Q").title("Rating Delta").scale(domainMid=0),
    )
)
chart

Note

Wasn't able to use the url from vega_datasets.data.movies.url for this, unsure why exactly

Edit

Fixed in docs: Use vega_datasets instead of url

@mattijn
Copy link
Contributor

mattijn commented Oct 2, 2024

Thanks! This will become even prettier once #3505 is in.

@mattijn
Copy link
Contributor

mattijn commented Oct 2, 2024

oh btw, you have to use underscores instead of spaces when using vega_datasets:

import altair as alt
from vega_datasets import data

imdb_rating = alt.datum["IMDB_Rating"]

chart = (
    alt.Chart(data.movies.url)
    .mark_point()
    .transform_filter(imdb_rating != None)
    .transform_filter(
        alt.FieldRangePredicate("Release_Date", [None, 2019], timeUnit="year")
    )
    .transform_joinaggregate(AverageRating="mean(IMDB_Rating)")
    .transform_calculate(RatingDelta=imdb_rating - alt.datum.AverageRating)
    .encode(
        x="Release_Date:T",
        y=alt.Y("RatingDelta:Q").title("Rating Delta"),
        color=alt.Color("RatingDelta:Q").title("Rating Delta").scale(domainMid=0),
    )
)
chart

See #2213 and #2310

@dangotbanned
Copy link
Member Author

oh btw, you have to use underscores instead of spaces when using vega_datasets:

🤦
Thanks @mattijn, I'll try that out tomorrow.
Feel free to edit this if you can confirm that works before I get a chance to

@dangotbanned dangotbanned enabled auto-merge (squash) October 3, 2024 13:58
@dangotbanned dangotbanned merged commit add5822 into main Oct 3, 2024
26 checks passed
@dangotbanned dangotbanned deleted the gallery-calculate-residuals branch October 6, 2024 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants