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 each layer with no problem, here is an example with the cars
dataset:
import altair as alt
from altair import datum
from vega_datasets import data
cars = data.cars()
horse = alt.Chart(cars).mark_point().encode(
x = 'Weight_in_lbs',
y = 'Horsepower'
)
chart = alt.hconcat()
for origin in cars.Origin.unique():
chart |= horse.transform_filter(datum.Origin == origin).properties(title=origin)
chart
miles = alt.Chart(cars).mark_point(color='red').encode(
x = 'Weight_in_lbs',
y = 'Miles_per_Gallon'
)
chart = alt.hconcat()
for origin in cars.Origin.unique():
chart |= miles.transform_filter(datum.Origin == origin).properties(title=origin)
chart
But when combined all the data show up in every plot
combined = horse + miles
chart = alt.hconcat()
for origin in cars.Origin.unique():
chart |= combined.transform_filter(datum.Origin == origin).properties(title=origin)
chart
Am I doing something wrong?
Simply speaking, in Taucharts facet charts group variables using X and Y coordinates. Facet charts help to compare information with many variables. You can use various combinations of axes to have just a single row: Y, X, X or a single column: Y, Y, X. Here is an example of a facet chart.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With