1

I would like to overlap a swarmplot on a barplot with seaborn. Let's generate my kind of data (3 conditions, with 3 replicates each, at 3 timepoints) :

condition_list = []
experiment_list = []
day_list = []
result_list = []


for condition in ['A', 'B', 'C']:
        
    for experiment in range(1,4):

        for day in range(1,4):

            condition_list.append(condition)
            experiment_list.append(experiment)
            day_list.append(day)
            result_list.append(random.random())
            
            
datas = list(zip(condition_list,experiment_list,day_list,result_list))

df = pd.DataFrame(data=datas, columns=['Condition','Experiment','Day','Result'])

I am fine with the barplot :

sns.catplot(x="Condition", y="Result", hue='Day', data=df, kind="bar", ci='sd')

which looks like that : enter image description here

But if I want to add corresponding values as dots on this figure, they are all aligned on the same abscissa :

sns.catplot(x="Condition", y="Result", hue='Day', data=df, kind="bar", ci='sd')
sns.swarmplot(x="Condition", y="Result", hue='Day', data=df)

enter image description here

Here is my problem ! For each bar, I would like to plot as dots the three corresponding values.

For example, if we restrain datas for the first day, I can plot bars AND dots on the same figure :

new_df = df[df['Day'] == 1].reset_index(drop=True)

sns.barplot(x="Condition", y="Result", data=new_df, ci="sd")
sns.swarmplot(x="Condition", y="Result",  color='black',data=new_df, zorder=10)

enter image description here

Is it possible to do the same on the first figure with seaborn ? Thank you very much for your help ^^

chalbiophysics
  • 313
  • 4
  • 15
  • 2
    Did you try `sns.swarmplot(..., dodge=True)`? [swarmplot docs and examples](https://seaborn.pydata.org/generated/seaborn.swarmplot.html) – JohanC Oct 06 '20 at 11:09

1 Answers1

1

Thanks to JohanC's comment, dodge function of swarm plot splits dots according to their category.

ax = sns.catplot(x="Condition", y="Result", hue='Day', data=df, kind="bar", ci='sd')
ax = sns.swarmplot(x="Condition", y="Result", hue='Day', color='black', dodge=True,data=df)
ax.get_legend().set_visible(False)

enter image description here

chalbiophysics
  • 313
  • 4
  • 15