Repository: altair-viz/altair

Issue:
Calling add_selection() on a layered chart results in an invalid spec
Example:
```python
import altair as alt
import pandas as pd

df = pd.DataFrame({
    'x': range(5),
    'y1': [1, 3, 2, 4, 5],
    'y2': [2, 1, 4, 5, 3]
})

alt.layer(
  alt.Chart(df).mark_line().encode(x='x', y='y1'),
  alt.Chart(df).mark_line().encode(x='x', y='y2')
).add_selection(
    alt.selection_interval()
)
```
```err
JavaScript Error: Duplicate signal name: "selector012_x_1"

This usually means there's a typo in your chart specification. See the javascript console for the full traceback.

Discussion:

Patch:
diff --git a/altair/vegalite/v3/api.py b/altair/vegalite/v3/api.py
index 2b372f35..ceef45ef 100644
--- a/altair/vegalite/v3/api.py
+++ b/altair/vegalite/v3/api.py
@@ -1882,8 +1882,7 @@ class LayerChart(TopLevelMixin, _EncodingMixin, core.TopLevelLayerSpec):
         if not selections or not self.layer:
             return self
         copy = self.copy()
-        copy.layer = [chart.add_selection(*selections)
-                        for chart in copy.layer]
+        copy.layer[0] = copy.layer[0].add_selection(*selections)
         return copy
 
 


Test patch:
diff --git a/altair/vegalite/v3/tests/test_api.py b/altair/vegalite/v3/tests/test_api.py
index caaa9fc8..8400b3c6 100644
--- a/altair/vegalite/v3/tests/test_api.py
+++ b/altair/vegalite/v3/tests/test_api.py
@@ -535,7 +535,15 @@ def test_facet_add_selections():
     assert chart1.to_dict() == chart2.to_dict()
 
 
-@pytest.mark.parametrize('charttype', [alt.layer, alt.concat, alt.hconcat, alt.vconcat])
+def test_layer_add_selection():
+    base = alt.Chart('data.csv').mark_point()
+    selection = alt.selection_single()
+    chart1 = alt.layer(base.add_selection(selection), base)
+    chart2 = alt.layer(base, base).add_selection(selection)
+    assert chart1.to_dict() == chart2.to_dict()
+
+
+@pytest.mark.parametrize('charttype', [alt.concat, alt.hconcat, alt.vconcat])
 def test_compound_add_selections(charttype):
     base = alt.Chart('data.csv').mark_point()
     selection = alt.selection_single()