I subset and melt an Airbnb dataset and try to plot a grouped chart:
from plotnine import *
airbnb_melted = pd.melt(airbnb_newcomers, id_vars =['host_id'], value_vars =['host_identity_verified', 'host_is_superhost'])
print(airbnb_melted)
The melted dataset looks like:
I know my following code is wrong, and the output of the plot is not what I want but it is closest to my idea:
ggplot(airbnb_melted, aes(x='variable', y='value')) +\
geom_bar(stat = 'sum', position=position_dodge())
I have searched online and found lots of plot examples with y
as numerical variable and stat='count'
that could be used. However, y
here is categorical and it shows error PlotnineError: 'stat_count() must not be used with a y aesthetic'
How could I plot a grouped bar chart similar to the following format? The orange words are what I have added as indication. Thank you.
Updates on Jan.20, 2020: Thanks to @StupidWolf help, the coding works as follows:
airbnb_host_count = airbnb_melted.replace(np.NaN, 'NA').groupby(['value', 'variable']).count().reset_index()
'host_id' actually indicates counts here:
ggplot(airbnb_host_count, aes(x='variable', y='host_id', fill='value')) +\
geom_bar(stat='sum', position=position.dodge())