Altair: data transform in a repeated chart

Created on 6 Jan 2020  路  3Comments  路  Source: altair-viz/altair

Hello!

I'm trying to generate a repeated chart with data transformation.

This works just fine:
```Python
alt.Chart(data_sample).mark_bar().encode(
alt.X(alt.repeat("column"), type='quantitative'),
alt.Y(alt.repeat("row"), type='nominal')
).properties(
width=200,
height=200
).repeat(
row=['race', 'gender'],
column=['number_diagnoses','num_medications'],
)
````

visualization

But this doesn't work:

```Python
alt.Chart(data_sample).mark_bar().encode(
alt.X(alt.repeat("column"), type='quantitative'),
alt.Y(alt.repeat("row"), type='nominal')
).properties(
width=200,
height=200
).repeat(
row=['race', 'gender'],
column=['mean(number_diagnoses)','mean(num_medications)'],
)
````
visualization (1)

I'm believe that Altair reads the 'mean(number_diagnoses)' as the literal name of the column, so I can't find any data. My question is how to incorporate the transform in the chart (looks like alt.repeat() doesn't take more arguments than column/row and type).

I'm using Altair 4.0, Python 3.6, in a Linux-Ubuntu 18.0 Machine, with Jupyter Lab.

question

All 3 comments

The repeat operator only accepts field names, not aggregates. If you want aggregations in the subcharts, you can specify them within the encodings:

alt.Chart(data_sample).mark_bar().encode(
    alt.X(alt.repeat("column"), type='quantitative', aggregate='mean'),
    alt.Y(alt.repeat("row"), type='nominal', aggregate='mean')
).properties(
    width=200,
    height=200
).repeat(
    row=['race', 'gender'],
    column=['number_diagnoses','num_medications'],
)

(note that an encoding like x='mean(num_medications)' is just a shorthand for x=alt.X('num_medications', aggregate='mean'))

Amazing, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dzonimn picture dzonimn  路  3Comments

galloramiro picture galloramiro  路  3Comments

tonylee3399 picture tonylee3399  路  3Comments

mroswell picture mroswell  路  4Comments

breadbaron picture breadbaron  路  4Comments