I have this dataframe:
df = pd.DataFrame({'Segment': {0: 'A', 1: 'B', 2: 'C', 3: 'D'},
'%Value_S=0': {0: 0.489, 1: 0.429, 2: 0.467, 3: 0.461},
'%Value_S=1': {0: 0.511, 1: 0.571, 2: 0.533, 3: 0.539},
'%Total': {0: 0.148, 1: 0.076, 2: 0.3, 3: 0.477}})
df
Segment %Value_S=0 %Value_S=1 %Total
0 A 0.489 0.511 0.148
1 B 0.429 0.571 0.076
2 C 0.467 0.533 0.300
3 D 0.461 0.539 0.477
And I'm trying to do a stacked bar like this one:
But I need the width of the bars to be from %Total
. So, to plot the graph above I used this:
z = df.melt(id_vars="Segment", value_vars=["%Value_S=0", "%Value_S=1"])
(ggplot(z, aes(x="Segment", y="value")) +
geom_bar(aes(fill="variable"), stat="identity", position="fill"))
and if I add the width
argument:
geom_bar(aes(fill="variable", width= df["%Total"]), stat="identity", position="fill"))
It gives me this error:
PlotnineError: 'Aesthetics must either be length one, or the same length as the data'
I tried the other way also: plotting the width first and then I can't figure out a way to plot the other two variables. The hardest part is this: the four bars have to cover ALL the graph (no spaces between them). Any ideas?