'How to make the mark_rule in Altair change based on user input?

I would like to make the mark_rule (significance level) to be adjustable. I have tried to do it using user input code and change the value in the rule from 0.05 to 'user input' but the chart turned out weird.

There are two things that I would like to ask for help with:

  1. Make the mark_rule change through user input (top priority)
  2. Make the color of the bars (factors) below the mark_rule change (optional)

I have tried many codes in this, by far, I can only make the mark_rule move using mouseover but it is not exactly what I want to do.

Any help would be very much appreciated.

import pandas as pd
import altair as alt

Sheet2 = 'P-value'
df = pd.read_excel('Life Expectancy Data- Clean.xlsx', sheet_name=Sheet2)

highlight = alt.selection(type='single', on='mouseover',
                          fields=['Factor'], nearest=True, empty="none")

bar = alt.Chart(df).mark_bar(strokeWidth=5, stroke="steelblue", strokeOpacity=0.1).encode(
        x = alt.X('Factor:O', sort='y'),
        y = alt.Y('P-value:Q'),
        tooltip = [alt.Tooltip('Factor:O'),alt.Tooltip('P-value:Q',format='.4f')],
        color= alt.condition(
            highlight,
            alt.value("orange"),
            alt.value("steelblue"))
    ).add_selection(
        highlight
    )

rule = alt.Chart(pd.DataFrame({'y': [0.05]})).mark_rule(color='red').encode(y='y')

alt.layer(
    bar, rule
).properties(
    title='Factors that Contribute to Life Expectancy in Malaysia',
    width=500, height=300
)

Current graph



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source