I read in the documentation that I can facet a multi-layer plot, but somehow the data are lumped together in output plot, and repeated in all facets.
I can facet eac
This is due to a little gotcha that's discussed very briefly at the end of the Facet section in the docs.
You can think of a layered chart in Altair as a hierarchy, with the LayerChart
object as the parent, and each of the individual Chart
objects as children. The children can either inherit data from the parent, or specify their own data, in which case the parent data is ignored.
Now, because you specified data for each child chart separately, they ignore any data or transform coming down from the parent. The way to get around this is to specify the data only in the parent.
As a side note, Altair also has a shortcut for the manual filtering and concatenating you're using here: the facet()
method. Here is an example of putting this all together:
import altair as alt
from vega_datasets import data
cars = data.cars()
horse = alt.Chart().mark_point().encode(
x = 'Weight_in_lbs',
y = 'Horsepower'
)
miles = alt.Chart().mark_point(color='red').encode(
x = 'Weight_in_lbs',
y = 'Miles_per_Gallon'
)
alt.layer(horse, miles, data=cars).facet(column='Origin')