2

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]})

dataframe
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')
barchart
I am obviously calculating something else.
Any hints how to get it right? Thanks!

Aham
  • 113
  • 6
  • Can you give an example of the expected output? I don't know what "count the IDs per date" means (all IDs? number of unique IDs?) or what "calculate its percentage as a total of the group" means (percentage of what? the number of IDs? as a fraction of what? the total number of IDs in the dataset? The number of IDs that appear as part of the group on every date?) – jakevdp Jun 09 '21 at 18:06
  • e.g. 28.04.2020, for that date the ID count would be 3 (type=h) and 2 (type=s). but I would like to show the percentage of the total number of IDs instead. The total number of unique IDs is 5 for type =h and 4 (type=s). I added a column with that number ('total') since I did not know how to count this. That means I would like a stacked barchart like above, but instead of 3 and 2 it would show (3/5 =) 0,6 and (2/4 =) 0,5. – Aham Jun 09 '21 at 20:22

0 Answers0