title: Altair - plot circle, ellipse, annotations in faceted chart
body: <p>Is it possible to plot lines and geometric shapes inside an Altair chart that follow the units of measure of the chart x and y variables?  The chart may be faceted,  and the shapes and lines depend on the data in each specific facet.  </p>
  
  <p>A reproducible example:</p>
  
  <pre><code>import pandas as pd
  import numpy as np
  import altair as alt
  alt.renderers.enable('notebook')
  
  # make some data to test
  N = 1000
  df = pd.DataFrame({
      'x1': np.random.normal(0, 1, N),
      'x2': np.random.normal(0, 1, N),
      'facet': np.random.choice(list('ABCDEFGHI'), N),
  })
  
  # derived variables 
  df['y1'] = np.where(np.sqrt(df['x1']**2 + df['x2']**2) &gt; 2, 'F', 'P')
  df['y2'] = 0.5*df['x2'] + 2.0 + np.random.normal(0, .5, N)
  df['color'] = np.where(df['y1'].eq('F'), 'red', 'green')
  
  # custom color map
  domain = ['F', 'P']
  range_ = ['red', 'green']
  
  # create and render the chart
  p1 = alt.Chart(df).mark_circle(opacity=1, size=15).encode(
      alt.X('x1', scale=alt.Scale(domain=(-4, 4))),
      alt.Y('x2', scale=alt.Scale(domain=(-4, 4))),
      color=alt.Color('y1', scale=alt.Scale(domain=domain, range=range_)),
      facet='facet'
  )
  
  # set some additional properties
  p1.properties(width=150, height=150, columns=3).resolve_scale()
  </code></pre>
  
  <p>Which produces the following output:</p>
  
  <p><a href="https://i.stack.imgur.com/wSTGn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wSTGn.png" alt="enter image description here"></a></p>
  
  <p>Q1:  Is is possible to draw a circle centered at 0, 0 with radius=2 in each of the charts as illustrated in the 2nd facet?</p>
  
  <p>The circle units of measure would be the same as the x and y units of measure. In this case,  x and y may represent a physical linear measurement,  where lenght per pixel is uniform in x and y.  i.e. it could be a dart board.</p>
  
  <p>The analogy may be to R lattice xyplot aspect = 'iso'.  See aspect description here:  <a href="https://rdrr.io/cran/lattice/man/xyplot.html" rel="nofollow noreferrer">https://rdrr.io/cran/lattice/man/xyplot.html</a></p>
  
  <p>Q2:  Is it possible to add a text annotation in one corner of the plot that counts the number of "F" (red) in each chart?</p>
  
  <p>Q3: For cases where the axis are not "iso" and have different units of measure,  can an line and ellipse (say,  a 95% density ellipse) be plotted as illustrated in the 2nd facet below?  Perhaps with the slope and intercept of the line of fit annotated on the chart?</p>
  
  <p>Example:</p>
  
  <pre><code># create and render the chart
  p1 = alt.Chart(df).mark_point().encode(
      x='x2',
      y='y2',
      facet='facet'
  )
  
  # set some additional properties
  p1.properties(width=150, height=150, columns=3).resolve_scale()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/5m359.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5m359.png" alt="enter image description here"></a></p>
  
  <p>In R Lattice,  these types of visualizations were accomplished through use of 'aspect', some specific lattice/grid functions and in some cases writing a custom 'panel' (facet) function,  that had access to the indexes of the data in each facet and could run linear models in each facet and display the the results.</p>