Skip to content

Text in facets #514

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
irenedelatorre opened this issue Aug 20, 2021 · 7 comments
Closed

Text in facets #514

irenedelatorre opened this issue Aug 20, 2021 · 7 comments
Labels
question Further information is needed

Comments

@irenedelatorre
Copy link

irenedelatorre commented Aug 20, 2021

When I use facets and include a Text mark, all the items in the array are plotted instead of filtering by the faceting variable. I'm not sure if this is because of how I'm adding the text mark, or if it's a bug. I haven't seen an example of Plot with a facet and annotations that change position and value either.

I've added an example in observablehq.com/d/a4546964387cf1c1

image

Thanks,
Irene

@mbostock
Copy link
Member

Faceting currently requires that the data of the faceted mark is strictly equal to (===) the facet data. Otherwise, the mark will be repeated across all facets. It looks like you have different data for your faceted mark here (["test 1", "test 2", …]).

You can workaround this limitation by using the select transform (e.g., Plot.selectFirst) to give the faceted mark the same data as the facet (here, penguins), and then pull out a specific datum for each facet to label.

Plot 0.2 will introduce a facet option that will allow you to turn faceting on explicitly for a given mark. However in this case you will still be required to give the faceted mark data that is parallel to the facet data (the same length and order) so that Plot can correctly assign the data to each facet. So, if you only want to label certain points in your scatterplot on each facet, you may need the select transform anyway.

If you’d like us to look at your notebook, please publish (unlisted) so we can see. My response above is speculative but hopefully it’s helpful! 😄

@mbostock mbostock added the question Further information is needed label Aug 20, 2021
@mbostock
Copy link
Member

(Please reply to this issue, even though it’s closed, if you’d like further assistance. If this does turn out to be a bug, I will reopen.)

@Fil
Copy link
Contributor

Fil commented Aug 20, 2021

I've sent you a suggestion https://observablehq.com/compare/a4546964387cf1c1...87d393e4f6081444
We hope to make faceted labels easier in the future (see #501).

@mbostock
Copy link
Member

To add to Fil’s suggestion, here is an example of using select:

Plot.text(data, Plot.selectMaxY({x: "flipper_length_mm", y: "body_mass_g", text: "species"}))

@irenedelatorre
Copy link
Author

Hi Mike and Phil,
My question is then how can you use select if you want only specific points?
You might not be interested in the max or min value but, for example, the value in one specific date, and that input might depend on the facet (ie. add dates of lockdowns for each country in facets visualizing coronavirus rates).
Is there a way when you use select to get the key of the facet you're in and use it for filtering for example? That could help to add annotations without having to repeat them in each of the small multiples.

My notebook is unlisted, although it's just an example with dummy annotations 😅

@mbostock
Copy link
Member

My question is then how can you use select if you want only specific points?

You can approach it as filtering: imagine that all of your data points have an associated label, but that most of the time that label is the empty string. See the nyt_display column here:

https://observablehq.com/@observablehq/plot-link

@irenedelatorre
Copy link
Author

Oh, so you pass it through the whole array included in the facet. I hadn't thought about that, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is needed
Projects
None yet
Development

No branches or pull requests

3 participants