import sys
sys.path.append('../')
import pandas as pd
import numpy as np
import forestplot as fp
pd.set_option('display.max_rows', 100)
df = (pd.read_excel('../examples/data/seg-multmodel-group0.xls')
.reset_index(drop=True)
.query("group!='poi'")
)
df.head(3)
var | r | ll | hl | pval | formatted_pval | model | group | source | |
---|---|---|---|---|---|---|---|---|---|
0 | agegroup1 | 0.027286 | -0.107541 | 0.161126 | 0.703717 | 0.704 | p2w | age | 2015 census |
1 | agegroup2 | -0.037114 | -0.170694 | 0.097806 | 1.305214 | 1 | p2w | age | 2015 census |
2 | agegroup3 | -0.213919 | -0.338047 | -0.082476 | 0.119311 | 0.119 | p2w | age | 2015 census |
std_opts = dict(
dataframe = df,
estimate = "r",
ll ="ll",
hl = "hl",
varlabel = "var",
model_col = "model",
)
# Vanilla
_df, ax = fp.mforestplot(**std_opts)
# Vanilla
# Color rows and add borders
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True, despine=False)
# Vanilla
# Color rows
# Add annotation and make it a table
# Add right annotations
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True,
annote=["group"], annoteheaders=["Group"],
rightannote=["source"],
right_annoteheaders=["Data source"],
table=True)
# With groups
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True,
groupvar="group",
rightannote=["source"],
right_annoteheaders=["Data source"],
variable_header="Covariate",
table=True)
# With groups and group ordering
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True,
groupvar="group",
group_order=["census", "fnb", "age"],
rightannote=["source"],
right_annoteheaders=["Data source"],
variable_header="Covariate",
capitalize=None,
table=True)
# With groups and group ordering
# Capitalize
_df, ax = fp.mforestplot(**std_opts, color_alt_rows=True,
groupvar="group",
group_order=["census", "fnb", "age"],
rightannote=["source"],
right_annoteheaders=["Data source"],
variable_header="Covariate",
capitalize="capitalize",
table=True)
# Add groups
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
return_df=True,
)
# Add groups
# With border + color alternate rows
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
despine=False,
color_alt_rows=True,
return_df=True,
)
# Add groups
# Add annotation on right
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
rightannote=['source'],
right_annoteheaders=['Data Source'],
return_df=True,
)
# No groups
# Add annotation on right
_df, ax = fp.mforestplot(**std_opts,
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
return_df=True,
)
# No groups
# Add annotation on right
# No flush
_df, ax = fp.mforestplot(**std_opts,
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
flush=False,
return_df=True,
)
# No groups
# Add annotation on right
# Add row colors
# No flush
_df, ax = fp.mforestplot(**std_opts,
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
flush=False,
color_alt_rows=True,
return_df=True,
)
# W/ groups
# Add annotation on right
# Add row colors
# No flush
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
flush=False,
color_alt_rows=True,
return_df=True,
)
# No groups
_df, ax = fp.mforestplot(**std_opts,
annote=['group','source'],
annoteheaders=['Group', 'Data Source'],
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
return_df=True,
)
# Add group
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
annote=['group','source'],
annoteheaders=['Group', 'Data Source'],
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
return_df=True,
)
# Add group
# Color row
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
annote=['group','source'],
annoteheaders=['Group', 'Data Source'],
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
color_alt_rows=True,
return_df=True,
)
# Add group
# Keep borders
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
annote=['group','source'],
annoteheaders=['Group', 'Data Source'],
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
color_alt_rows=True,
return_df=True,
**{'despine':False, "figsize":(4,8)}
)
# Add group
# Color row
# Change legend label
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
annote=['group','source'],
modellabels=["Model 1", "Model 2"],
annoteheaders=['Group', 'Data Source'],
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
color_alt_rows=True,
return_df=True,
)
# Add group
# Color row
# Change legend label
# Add x-label
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
annote=['group','source'],
modellabels=["Model 1", "Model 2"],
xlabel="X-label",
annoteheaders=['Group', 'Data Source'],
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
color_alt_rows=True,
return_df=True,
)
# Add group
# Color row
# Change legend label
# Add x-label
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
annote=['group','source'],
modellabels=["Model 1", "Model 2"],
xlabel="X-label",
annoteheaders=['Group', 'Data Source'],
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
color_alt_rows=True,
return_df=True,
bbox_to_anchor=(.5,.95)
)
# Add group
# Color row
# Change legend label
# Add x-label
_df, ax = fp.mforestplot(**std_opts,
groupvar='group',
annote=['group','source'],
modellabels=["Model 1", "Model 2"],
xlabel="X-label",
annoteheaders=['Group', 'Data Source'],
rightannote=['group','source'],
right_annoteheaders=['Group', 'Data Source'],
color_alt_rows=True,
return_df=True,
# bbox_to_anchor=None,
)
import forestplot as fp
df = fp.load_data("sleep") # companion example data
df.head(3)
n | r | CI95% | p-val | BF10 | power | var | hl | ll | moerror | group | label | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 706 | 0.090373 | [0.02 0.16] | 0.016309 | 0.839 | 0.67 | age | 0.16 | 0.02 | 0.069627 | age | in years |
1 | 706 | 0.048081 | [-0.03 0.12] | 0.201948 | 0.106 | 0.25 | clerical | 0.12 | -0.03 | 0.071919 | occupation | =1 if clerical worker |
2 | 706 | 0.041229 | [-0.03 0.11] | 0.273948 | 0.086 | 0.19 | construc | 0.11 | -0.03 | 0.068771 | occupation | =1 if construction worker |
fp.forestplot(df, # the dataframe with results data
estimate="r", # col containing estimated effect size
ll="ll", hl="hl", # columns containing conf. int. lower and higher limits
varlabel="label", # column containing variable label
xline=.15
)
<AxesSubplot:>
ax = fp.forestplot(df, # the dataframe with results data
estimate="r", # col containing estimated effect size
ll="ll", hl="hl", # columns containing conf. int. lower and higher limits
varlabel="label", # column containing variable label
ylabel="Confidence interval", # y-label title
xlabel="Pearson correlation", # x-label title
)
ax.axvline(.15)
<matplotlib.lines.Line2D at 0x2040e7bc580>