Faceted layered charts in Altair
Starting from the following example from the documentation (link):
import altair as alt
from vega_datasets import data
source = data.barley()
alt.Chart(source).mark_bar().encode(
x='year:O',
y='sum(yield):Q',
color='year:N',
column='site:N'
)
It is tempting to layer faceted charts. However the following exception is raised:
base = alt.Chart(source).encode(
x='year:O',
y='sum(yield):Q',
color='year:N',
column='site:N',
text="sum(yield):Q"
)
alt.layer(base.mark_bar(), base.mark_text())
Traceback (most recent call last):
...
ValueError: Faceted charts cannot be layered.
The solution is to facet the layered chart rather than layering facetted charts:
base = alt.Chart(barley).encode(
alt.X("year:O"),
alt.Y("sum(yield):Q"),
alt.Color("year:N"),
alt.Text("sum(yield):Q", format=".0f"),
)
alt.layer(
base.mark_bar(),
base.mark_text(dy=-10)
).facet(
alt.Column("site:N", title=None)
).configure_facet(spacing=0)