'Plotting multi-level/group barplot with seaborn

I am attempting to recreate the barplot here:

barplot

The code I have so far is:

trends = pd.read_csv('January 8-February 7, 2019 - Core Trends Survey - CSV.csv')
trends = trends[['sns2a','sns2b','sns2c','sns2d','sns2e']]
trends = trends.rename(columns = {'sns2a':"Twitter",'sns2b':'Instagram','sns2c':'Facebook','sns2d':'Snapchat','sns2e':'Youtube'})
trends = trends.melt(value_vars=['Twitter', 'Instagram', 'Facebook', 'Snapchat','Youtube'])
trends = trends[trends['value'] !=  ' ']
trends = trends.groupby(['variable', 'value']).agg({'value': 'count'})
trends_pcts = trends.groupby(level= 0).apply(lambda x: 100 * x / float(x.sum()))
trends_pcts

Which yields a dataframe grouped by social media networks and the percentage of each type of response:

                   value
variable    value   
Facebook    
            1      47.960199
            2      23.383085
            3      18.407960
            4      3.880597
            5      5.870647
            8      0.298507
            9      0.199005
Instagram   
            1      38.945233
            2      20.892495
            3      21.703854
            4      6.896552
            5      11.561866
Snapchat    
            1      42.807018
            2      15.087719
            3      18.245614
            4      7.368421
            5      16.491228
Twitter 
            1      24.464832
            2      17.431193
            3      29.051988
            4      11.314985
            5      17.431193
            9      0.305810
Youtube 
            1      28.011204
            2      19.047619
            3      33.520075
            4      10.084034
            5      9.150327
            8      0.186741

Response number corresponding to:

1   Several times a day
2   About once a day
3   A few times a week
4   Every few weeks
5   Less often
8   (VOL.) Don't know
9   (VOL.) Refused

However, I am still very new to pandas and seaborn and am lost as to how to plot this multilevel dataframe with seaborn?



Solution 1:[1]

The data provided has been partially modified and introduced. The main data frame and the data frame for the legend are combined. In the combined data frame, we select the bar chart for the category plot. Label names and titles are added to the created objects.

import pandas as pd
import numpy as np
import io

data = '''  
variable    freq value
Facebook 1      47.960199
Facebook 2      23.383085
Facebook 3      18.407960
Facebook 4      3.880597
Facebook 5      5.870647
Facebook 8      0.298507
Facebook 9      0.199005
Instagram 1      38.945233
Instagram 2      20.892495
Instagram 3      21.703854
Instagram 4      6.896552
Instagram 5      11.561866
Snapchat  1      42.807018
Snapchat  2      15.087719
Snapchat  3      18.245614
Snapchat  4      7.368421
Snapchat  5      16.491228
Twitter   1      24.464832
Twitter   2      17.431193
Twitter   3      29.051988
Twitter   4      11.314985
Twitter   5      17.431193
Twitter   9      0.305810
Youtube   1      28.011204
Youtube   2      19.047619
Youtube   3      33.520075
Youtube   4      10.084034
Youtube   5      9.150327
Youtube   8      0.186741
'''

df = pd.read_csv(io.StringIO(data), delim_whitespace=True)

data2 = '''
freq name
1   "Several times a day"
2   "About once a day"
3   "A few times a week"
4   "Every few weeks"
5   "Less often"
8   "(VOL.) Don't know"
9   "(VOL.) Refused"
'''
df2 = pd.read_csv(io.StringIO(data2), delim_whitespace=True)

df3 = df.merge(df2, on='freq', how='left')

import seaborn as sns
g = sns.catplot(data=df3, kind='bar', x='variable', y='value', hue='name', palette='viridis')
g.despine(left=True)
g.set_ylabels('Percent of users per network(%)')
g.set_xlabels('Social Networks')
g.legend.set_title('Use Frequency')
g.fig.suptitle('Majority of Facebook, Instagram and Snapchat users visit these social networks daily', x=0.5, y=1.01)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 r-beginners