I'm using plotnine to draw some plots. When I try to display a bar chart of proportion rather than count, the fill
argument becomes useless. I noticed that removing the group=1
arguments helps to get the fill
argument "active" again. However, without the group=1
argument, the proportions are not correctly calculated.
Here is my function:
def plot_churn(df_):
color_dict = {
'Stayed': 'green',
'Churned': 'red'
}
myplot = ggplot(data=df_, mapping=aes(x='Flag_Churned', fill='Flag_Churned'))
myplot += geom_bar(mapping=aes(y="stat(prop)", group=1))
myplot += theme(subplots_adjust={'right': 0.71})
myplot += facet_wrap('Flag_Treat')
myplot += scale_fill_manual(color_dict)
myplot += scale_y_continuous(labels=percent_format())
print(myplot)
For example, when using the following pandas DataFrame:
data = {'Churn': [0,0,0,1,1,0,1,1], 'Flag_Treat': ['treated','treated','treated','treated','not treated','not treated','not treated','not treated'],
'Flag_Churned': ['Stayed', 'Stayed', 'Stayed', 'Churned', 'Churned', 'Stayed', 'Churned', 'Churned']}
df = pd.DataFrame(data=data)
the resulting output is not filled by 'Flag_Churned':
What am I doing wrong?