Skip to content

Reuse channel values in a text mark after a transformation #2271

Answered by Fil
ltostes asked this question in Q&A
Discussion options

You must be logged in to vote

Yes; here's how it would work:

{
  const numformat = d3.format(".0f");
  const options = Plot.groupX(
    { y: "mean", text: (v) => `Mean: ${numformat(d3.mean(v))}` }, // reducers
    { x: "species", y: "body_mass_g", text: "body_mass_g" }
  );

  return Plot.plot({
    marks: [
      Plot.barY(penguins, {
        ...options,
        stroke: "navy"
      }),
      Plot.text(penguins, {
        ...options,
        lineAnchor: "bottom",
        dy: -5
      })
    ]
  });
}

The text reducer is applied for each series (here, penguins) on the text channel. Note that your current code computes a mean for every penguin, only to retain the first for each series.

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@ltostes
Comment options

@Fil
Comment options

Fil Jan 15, 2025
Collaborator

Answer selected by ltostes
@ltostes
Comment options

@Fil
Comment options

Fil Jan 15, 2025
Collaborator

@ltostes
Comment options

@Fil
Comment options

Fil Jan 15, 2025
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants