I would like to plot a stacked barchart using altair. The data is a time series. The expected output is to count the IDs per date (and per group, which is "type") and to calculate its percentage of the total of the group.This is given in the columns "total" The example dataframe looks as this:
df = pd.DataFrame({'date': ['2020-04-08','2020-04-08','2020-04-08','2020-04-23','2020-04-23','2020-04-23','2020-04-08','2020-04-08','2020-04-23','2020-04-23','2020-04-28'],
'type': ['h','h','h','h','h','h','s','s','s','s','s'],
'ID': ['a','b','c', 'a','e','f', 'a','e','a','c','d'],
'total': [5,5,5,5,5,5,4,4,4,4,4]})
I managed to show a stacked barchart using count but not to calculate the percentage.
This is what I tried so far, but the result is not correct.
alt.Chart(df).transform_joinaggregate(
xxx='count(ID)'
).transform_calculate(
Prc='datum.xxx / datum.total'
).mark_bar().encode(
alt.X('monthdate(date):T'),
y=alt.Y('Prc:Q'), color=alt.Color('type:N'))
The first bar in the chart (2020-04-08) would show 3/5 --> "0,6" (type 'h') and 2/4 --> "0,5" (type 's')
I am obviously calculating something else.
Any hints how to get it right?
Thanks!