question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Highlighting points on a faceted chart

See original GitHub issue

the 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:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jakevdpcommented, Jul 10, 2019

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:

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
})

chart1 = alt.Chart(points).mark_point(filled=True, size=150).encode(
    x='x',
    y='y',
    color='labels:N',
)

chart2 = alt.Chart(points).transform_aggregate(
    x='mean(x)',
    y='mean(y)',
    groupby=['labels']
).mark_point(filled=True, size=50).encode(
    x='x:Q',
    y='y:Q',
    color=alt.value('black'),
)

alt.layer(chart1, chart2).facet('labels:N')

visualization - 2019-07-09T222903 420

0reactions
jakevdpcommented, Jul 23, 2019

It is possible, but the datasets need to be joined in the top layer…

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found