LSYS / forestplot

A Python package to make publication-ready but customizable coefficient plots.
http://forestplot.rtfd.io
MIT License
110 stars 10 forks source link

Labels don't match up correctly in mforestplot #113

Open jdoiii opened 4 months ago

jdoiii commented 4 months ago

Issue: mforestplots do not correspond to (correct) forestplots

Background: I have data for unadjusted and adjusted odds ratios with confidence intervals.

I plot the same data using both forestplot (with repeated variable lables) and mforestplot.

With forestplot, the symbols have the correct ORs and CIs, although order of the variable labels is not consistent within groups.

However with mforestplot, the symbols do not consistently align with the correct labels.

For example: Symbols in Age and Comorbidity groups no longer match the correct label values in forestplot (e.g., Age4 ORs should both be < 1, Obesity ORs should both be > 1) . Sex, Race, Rank, and Status groups correctly match.

The code is the same except fp.mforestplot is used in place of fp.forestplot.

Questions:

  1. Is there a way to fix the mforestplot misalignment?
  2. In forestplot, is there a way to have the repeated variable labels sorted within the groups?

Thanks in advance for any guidance.

title = 'SGLT2i Odds Ratios'
est = 'SGLT2i-OR'
low = 'SGLT2i-OR-LL'
hi = 'SGLT2i-OR-UL'
varlabel = 'Label'
ylabel = '95% CI'
xlabel = 'Odds Ratio (Ref: Age=18-30, Race=White, Sex=Female, Rank=ES)'
groupvar = 'Group'
grouporder = ['Age', 'Sex', 'Race', 'Rank', 'Status', 'Comorbidity' ]
rightannote = ['pval-SGLT2i']
right_ah = ['p']
xticks = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0]
modellabels = ['Unadjusted', 'Adjusted']
model = 'model'
figsize = (8, 20)

xfp = fp.forestplot(xdf,  # the dataframe with results data
                    estimate=est,  # col containing estimated effect size
                    ll=low, hl=hi,  # columns containing CI low and high limits
                    varlabel=varlabel,  # column containing variable label
                    model_col=model,
                    ylabel=ylabel,  # y-label title
                    xlabel=xlabel,  # x-label title
                    blipblop=True,
                    title=title,
                    groupvar=groupvar,
                    group_order=grouporder,
                    color_alt_rows=True,
                    # rightannote=rightannote,
                    # right_annoteheaders=right_ah,
                    modellables=modellabels,
                    xticks=xticks,
                    table=True,
                    figsize=figsize,
                    # Additional kwargs for customizations
                    **{
                        "markersize": 30,
                        # override default vertical offset between models (0.0 to 1.0)
                        "offset": 0.35,
                        `"xline": 1,
                        "xlinestyle": (0, (10, 5)),  # long dash for x-reference line
                        "xlinecolor": ".8",  # gray color for x-reference line
                       },
                    )

FORESTPLOT

image

MFORESTPLOT

image

(UPDATE: corrected minor typo in code)

cmahervir commented 4 months ago

+1 for this. It looks like when the grouping is done to plot the point estimates and CIs, that new row ordering is not tracked and the labels are placed in the order they are in the dataframe?:

ax.set_yticklabels(
            dataframe[yticklabel], fontfamily=fontfamily, fontsize=fontsize, ha="left"
        )