Altair forestplot how to vertically offset errorbars

18 views Asked by At

I want to create a forestplot (aka coefplot) in Altair showing 2 confidence intervals per variable, but not having them overlap.

I got as far as the example below, but I really want to move the errorbars apart vertically. A small vertical offset would do the trick, but I cannot seem to figure out how to add this.

NB: With row facet I can hack an ugly solution, but that is not the solution I am looking for.

import altair as alt
import pandas as pd

source = pd.DataFrame({
    "ll": [0.9, 1, 3.8, 1.3],
    "hl": [3.1, 2, 2.2, 2.7],
    "coef": [2, 1.5, 3, 2],
    "variable": ["Age", "Age", "Gender", "Gender"],
    "subgroup": ['High-risk', 'Medium-risk', 'High-risk', 'Medium-risk'],
})

bar = alt.Chart(source).mark_errorbar(ticks=True).encode(
    alt.X("hl:Q", scale=alt.Scale(zero=False)),
    alt.X2("ll:Q"),
    alt.Y("variable:N"),
    color='subgroup:N'
)

point = alt.Chart(source).mark_point(
    filled=True,
    color="subgroup:N"
).encode(
    alt.X("coef:Q"),
    alt.Y("variable:N")
)

point + bar

enter image description here

This is what it looks like now, but I want the bars vertically apart, like in the example below:

enter image description here

0

There are 0 answers