1

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:

enter image description here

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?

krassowski
  • 13,598
  • 4
  • 60
  • 92
Chris
  • 2,019
  • 5
  • 22
  • 67

1 Answers1

0

You passed the data from melted data frame (z), but tried to adjust the bar width by a column of the original data frame (df). You need to include the column %Total in the melted df:

z = df.melt(
    id_vars=["Segment", "%Total"],
    value_vars=["%Value_S=0", "%Value_S=1"]
)

And then simply map to the width to %Total column:

from plotnine import geom_bar, ggplot, aes
(
    ggplot(z, aes(x="Segment", y="value"))
    + geom_bar(
        aes(fill="variable", width="%Total"),
        stat="identity", position="fill"
    )
)

which produces:

enter image description here

krassowski
  • 13,598
  • 4
  • 60
  • 92