简体   繁体   中英

Rolling average on a layered faceted chart in Altair

I successfully got layers to work in faceted charts and rolling average to work in layered charts. I now want to sort of combine the two ie have a rolling average in a layered faceted chart.

Intuitively combining the two gives me an error -

Javascript Error: Cannot read property 'concat' of undefined
This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

Code (gives the above error):

# Data Preparation
df = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv')
idf = df[df['Country/Region'] == 'India']
idf = idf[df.columns[4:]]
idf = idf.T
idf = idf.reset_index()
idf.columns = ['day', 'case']
idf['country'] = 'india'

gdf = df[df['Country/Region'] == 'Germany']
gdf = gdf[df.columns[4:]]
gdf = gdf.T
gdf = gdf.reset_index()
gdf.columns = ['day', 'case']
gdf['country'] = 'germany'

fdf = pd.concat([idf,gdf])

# Charting
a = alt.Chart().mark_bar(opacity=0.5).encode(
    x='day:T',
    y='case:Q'
)

c = alt.Chart().mark_line().transform_window(
    rolling_mean='mean(case:Q)',
    frame=[-7, 0]
).encode(
    x='day:T',
    y='rolling_mean:Q'
)

alt.layer(a, c, data=fdf).facet(alt.Column('country', sort=alt.EncodingSortField('case', op='max', order='descending')))

If you remove the transform_window and replace y='rolling_mean:Q' with y='case:Q' , you'd get a layered faceted chart. It is this chart on which I want a 7 day rolling average.

You should replace your window transform with this:

.transform_window(
    rolling_mean='mean(case)',
    frame=[-7, 0],
    groupby=['country']
)

There were two issues with your original transform:

  • type shorthands are only used in encodings, never in transforms. When you wrote mean(case:Q) , you were specifying a rolling mean of the field named "case:Q" , which does not exist.

  • since you are faceting by country, you need to group by country when computing the rolling mean.

The result looks like this: 在此处输入图像描述

Try to use transform_window by sort=[{'field': 'date'}] https://vega.github.io/vega-lite/docs/window.html#cumulative-frequency-distribution

Or:https://altair-viz.github.io/gallery/scatter_marginal_hist.html

https://altair-viz.github.io/gallery/layered_chart_with_dual_axis.html#layered-chart-with-dual-axis

https://altair-viz.github.io/gallery/parallel_coordinates.html#parallel-coordinates-example

import altair as alt
from vega_datasets import data

source = data.iris()

alt.Chart(source).transform_window(
    index='count()'
).transform_fold(
    ['petalLength', 'petalWidth', 'sepalLength', 'sepalWidth']
).mark_line().encode(
    x='key:N',
    y='value:Q',
    color='species:N',
    detail='index:N',
    opacity=alt.value(0.5)
).properties(width=500)

https://altair-viz.github.io/user_guide/compound_charts.html?highlight=repeat#horizontal-concatenation

import altair as alt
from vega_datasets import data

iris = data.iris.url

chart1 = alt.Chart(iris).mark_point().encode(
    x='petalLength:Q',
    y='petalWidth:Q',
    color='species:N'
).properties(
    height=300,
    width=300
)

chart2 = alt.Chart(iris).mark_bar().encode(
    x='count()',
    y=alt.Y('petalWidth:Q', bin=alt.Bin(maxbins=30)),
    color='species:N'
).properties(
    height=300,
    width=100
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM