How do I add statistical annotations, e.g. p-values, to graphs?
For example:
Code for bar graph without statistical annotations:
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv')
days=['day1 and day 2', 'day 3 and day 4']
#n_numbers = ['n = 20', 'n = 14']
# Group and calculate the mean and sem
mean = df.groupby('day').mean()
sem = df.groupby('day').sem()
# Extract mean from days for input
mean_thur=df.query("day=='Thur'")['total_bill'].mean()
mean_fri=df.query("day=='Fri'")['total_bill'].mean()
mean_sat=df.query("day=='Sat'")['total_bill'].mean()
mean_sun=df.query("day=='Sun'")['total_bill'].mean()
# Extract sem from days for input
sem_thur=df.query("day=='Thur'")['total_bill'].sem()
sem_fri=df.query("day=='Fri'")['total_bill'].sem()
sem_sat=df.query("day=='Sat'")['total_bill'].sem()
sem_sun=df.query("day=='Sun'")['total_bill'].sem()
# Bar graphs and error bars for top stack only
fig = go.Figure(data=[
go.Bar(name='Thursday and Saturday', x=days, y=[mean_thur, mean_sat], marker_color='#E45746', opacity=0.8),
go.Bar(name='Friday and Sunday', x=days, y=[mean_fri, mean_sun], marker_color='#72B7B2', opacity=0.8,
error_y=dict(
type='data', # value of error bar given in data coordinates
array=[sem_fri, sem_sun], color='rgba(0,0,0,1)', thickness=2, width=10,
visible=True)
)
])
# Error bars for bottom stack
fig.add_trace(go.Scatter(
x=['day1 and day 2'], y=[mean_thur, sem_thur],
mode='markers',
name='error_bars_thursday',
error_y=dict(
type='constant',
value=sem_thur,
color='rgba(0,0,0,1)',
thickness=1.8,
width=10
),
marker=dict(color='rgba(0,0,0,1)', size=10, opacity=0),
showlegend=False
))
fig.add_trace(go.Scatter(
x=['day 3 and day 4'], y=[mean_sat, sem_sat],
mode='markers',
name='error_bars_thursday',
error_y=dict(
type='constant',
value=sem_thur,
color='rgba(0,0,0,1)',
thickness=1.8,
width=10,
),
marker=dict(color='rgba(0,0,0,1)', size=10, opacity=0),
showlegend=False
))
# Add n numbers
fig.add_trace(go.Scatter(
x=['day1 and day 2', 'day 3 and day 4'],
y=[40, 47],
mode="text",
name="n_numbers",
text=['n=20', 'n=50'],
textposition="top center",
showlegend=False
))
# Customization of layout and traces
fig.update_layout(template='simple_white', title='', yaxis_title='Title Y', barmode='stack',
hoverlabel_namelength=-1,
newshape_line_color='magenta', newshape_opacity=0.2)
fig.update_traces(marker_line_color='rgba(0,0,0,0.8)', marker_line_width=1, opacity=0.8)
# Make figure zoomable, hide logo et cetera
config = dict({'scrollZoom':True, 'displaylogo': True,
'modeBarButtonsToAdd':['drawopenpath', 'eraseshape']
})
fig.show()
print(mean)
print(mean_thur)
print(mean_fri)
print(mean_sat)
print(mean_sun)
print(sem)
print(sem_thur)
print(sem_fri)
print(sem_sat)
print(sem_sun)