'Type error when using alt.condition for mark point angle

I am trying to plot arrows pointing left or right and being green or red, depending on the condition. It works for the color, but not for the angle of the triangle (mark point) which I'm using for the head of the arrow. Here is the data and code: enter image description here

color=alt.condition("datum.Current >= datum.Previous",alt.value("green"),alt.value("red"))
angle=alt.condition("datum.Current >= datum.Previous",alt.value(210),alt.value(30))
alt.Chart(df_chart).mark_point(size=200,shape='triangle'
                    ,angle=angle).encode(alt.X('Current'),alt.Y('Group'),color=color)

I'm getting this error: enter image description here

This is what I get if I change the angle to a number, it works without error, except that I don't get the red arrow pointing to the left:enter image description here



Solution 1:[1]

You can't use a conditional as for a mark parameter, only for encoding parameters as per https://github.com/altair-viz/altair/issues/1976. Now, for some reason, it seems like that conditional does not work when passed to the angle encoding either (see Jake's answer for a working solution, I must have had a typo when trying that), but you could work around that by using transform_calculate to compute the new field values and reference that field:

alt.Chart(df_chart).mark_point(size=400, shape='triangle').encode(
    alt.X('Current'),
    alt.Y('Group'),
    angle=alt.Angle('angle:Q', scale=alt.Scale(domain=[0, 360])),
    color='Group:N'
).transform_calculate(
    angle="datum.Current >= datum.Previous ? 210 : 30"
)

enter image description here

It is important to define the domain when using an angle value from a field, you can see another example here https://altair-viz.github.io/gallery/wind_vector_map.html?highlight=wind. Generally I would avoid passing a condition to color just to control the values and instead use the range parameter with an existing field value as explained in the docs https://altair-viz.github.io/user_guide/customization.html#color-domain-and-range

Sources

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

Source: Stack Overflow

Solution Source
Solution 1