I’m trying to make an interactive plot that has a regression line on top of it. Here is the plot in the Vega editor and here is a gist of the Altair source (MRE). This is heavily influenced by this very nice SO example (thanks Andrii Syd).
In summary, I want to plot a bunch of points (and be able to toggle them on/off by clicking on the legend) and also a regression line on top of them (and be able to toggle that on/off as well by clicking on the legend). I would prefer to have it toggle in this way rather than what I see as less elegant user inputs (checkboxes, etc.) that are placed beneath the chart. I have both conceptual and practical questions.
- I simply don’t understand the apparently necessary use of the
transform_fold
call. It works, it’s necessary, and I just don’t get what it’s supposed to be doing and why it’s necessary. The SO post is great in solving OP’s problem. It just doesn’t explain why. - I have a base plot (of points) and then the regression plot on top of it. I don’t get, conceptually, how I use
selection_point
to have similar behavior with both points and line. I have used it to control opacity of both points and lines, and added it to both layers, and it does what I expect for points but not for the line (the line won’t toggle on/off). I’m trying to understand where I should addselection_point
(and other similar calls likeinteractive
) when I have multiple layers (on each layer individually? on one layer only? on the result when they are all layered on top of each other?). - The points and line are mixed together (alphabetically) in the legend; it’d be nicer to have all the points up top under a Series heading, then the line next under a Fit heading.
- It would also be nice to have the line with a different “icon” (say, a line, not a circle).