title: Labelling Layered Charts in Altair (Python)
body: <p>I am attempting to create two layered histograms in Altair (and a vertical mean ruler for each). I would like a legend to label each of these four.</p>
  
  <p>I am using the first 'Birth weight I' data that can be found <a href="https://www.stat.berkeley.edu/users/statlabs/labs.html#babiesI" rel="nofollow noreferrer">here</a></p>
  
  <p>My code (real long, apologies) looks something like this:</p>
  
  <pre><code>from altair import datum
  
  # This histogram for baby weights of mothers who dont smoke
  dont = alt.Chart(babyData).mark_bar().encode(
      alt.X("bwt-oz:Q", axis=alt.Axis(title='Birth Weight (Ounces)'), bin=True),
      alt.Y('count()', axis=alt.Axis(title='Count'), scale=alt.Scale(domain=[0, 350]))
  ).properties(
      width=400,
      height=400
  ).transform_filter(
      datum.smoke == 0,
  )
  
  mean = alt.Chart(babyData).mark_rule(color='red').encode(
      x='mean(bwt-oz):Q',
      size=alt.value(4)
  ).transform_filter(
      datum.smoke == 0
  )
  
  dontSmokeChart = dont + mean
  
  # This histogram for baby weights of mothers who smoke
  do = alt.Chart(babyData).mark_bar().encode(
      alt.X("bwt-oz:Q", axis=alt.Axis(title='Birth Weight (Ounces)'), bin=True),
      alt.Y('count()', axis=alt.Axis(title='Count'), scale=alt.Scale(domain=[0, 350]))
  ).transform_filter(
      datum.smoke == 1
  ).properties(
      width=400,
      height=400
  )
  
  mean2 = alt.Chart(babyData).mark_rule(color='red').encode(
      x='mean(bwt-oz):Q',
      size=alt.value(4)
  ).transform_filter(
      datum.smoke == 1
  )
  
  doSmokeChart = do + mean2
  
  # This layers, and puts them all together
  
  layer = alt.layer(
      dont,
      mean,
      do,
      mean2
  ).properties(
      title="Layered Histogram of Baby Weights of Mothers Who smoke Vs. Who Don't",
  ).configure_mark(
      opacity=0.5,
      color='blue',
  )
  layer
  </code></pre>
  
  <p>The final layered chart looks something like this:
  <a href="https://i.stack.imgur.com/OR9f4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/OR9f4.png" alt="https://i.stack.imgur.com/OR9f4.png"></a></p>
  
  <p><strong>I would simply like a legend specifying which histogram/mean belongs to what.</strong></p>
  
  <p>If I could color them too, and perhaps add a legend that way, that would be nice as well, but I am unsure how to do so.</p>
  
  <p>Thanks for any insight!</p>