Highlighting points on a faceted chart
See original GitHub issuethe following is adapted from https://stackoverflow.com/a/54965673/2981639
I want to highlight specific points on a chart. The way I’ve seen this done is to create 2 layers with the first plotting all points and the 2nd plotting a subset in a different style i.e. cluster members colored by cluster and the cluster centers as black points in the linked example.
I also want to facet the chart, i.e. using columns=
and I understand from https://github.com/altair-viz/altair/issues/668 that the data should be passed to the facet() function and this gets forwarded to the layers.
But if you want two layers with different data sources and a facet per layer what do you do?
import altair as alt
import pandas as pd
from sklearn.datasets import make_blobs
X, labels = make_blobs(20, random_state=1)
points = pd.DataFrame({
'x': X[:, 0],
'y': X[:, 1],
'labels': labels
})
centers = points.groupby('labels').mean()
data = pd.concat([points , centers.reset_index()])
chart1 = alt.Chart(data).mark_point(filled=True, size=150).encode(
x='x',
y='y',
color='labels:N',
column='labels:N',
)
chart2 = alt.Chart(centers).mark_point(filled=True, size=50).encode(
x='x',
y='y',
color=alt.value('black'),
column='labels:N',
)
chart1 + chart2
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
How to highlight points in a facet grid with ggplot?
I would now like to highlight with a different colour one point for each subplot. I cannot figure out how to add it...
Read more >Introduction to gghighlight - GitHub Pages
If you want to highlight each facet individually, set calculate_per_facet to TRUE . Note that gghighlight() affects the plot before ...
Read more >7.8 Adding Annotations to Individual Facets
You want to add annotations to each facet in a plot. 7.8.2 Solution. Create a new data frame with the faceting variable(s), and...
Read more >Faceted Search Enhancements in APEX 20.2 - Oracle Blogs
Some of the new region attributes are: Show Charts. Available options include: Popup: A Show Chart button is added to the facet header....
Read more >Introduction to gghighlight
gghighlight() adds direct labels for some geoms. Currently, the following geoms are supported: point : add labels at each highlighted point.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As of Altair 3.1, the information in #668 is out of date: you need not specify the data in the facet method itself, so long as the layers both contain the same dataset.
As to your question: if you want to facet a layered chart, you’ll need to use a single dataset for both layers. This could be done by joining the two datasets and plotting different columns, or by concatenating the datasets and filtering on the appropriate property within each layer, or by using transforms to modify the single base dataset within subcharts. Here I think the third option is probably the cleanest approach. It might look something like this:
It is possible, but the datasets need to be joined in the top layer…