Plot Group Transform Behavior: Default Channel vs. Array of Objects

Plot’s group’s transform appears to behave differently depending on the format of the data input; default array vs array of objects.

Should we be taking into account the format of the data when specifying the parameter to the title option?

See Plot Group Transform with Array of Objects Issue / Mario Delgado / Observable for a reproduction of the issue.

From my testing, it appears that the function you’re using to determine the title prop receives one of the elements from the data array. Therefore, if the elements are arrays, you can use map. If they’re objects, you can’t (but you can access the properties). This should work in the second example from your notebook:

Plot.plot({
  marks: [
    Plot.barY(
      [{ a: "Y" }, { a: "N" }],
      Plot.groupX(
        { y: "count" },
        {
          x: "a",
          title: (group) =>  group.a
        }
      )
    )
  ]
})

In a Plot.group (or Plot.bin) transform, you can set the title in two places:

  • in the first part (the outputs), the title property describes a reducer that defines how a summary value is computed for each group. The reducer is applied on the grouped values from the title channel—or, by default, to the grouped values from the data array.
  • in the second part (the options), the title property describes the input title channel, indexed on the data.

So if you have, e.g.:

Plot.group({
    title: group => d3.mode(group)
  },
  {
    title: "a"
  }
)

it is equivalent to:

Plot.group({
    title: group => d3.mode(group, d => d.a)
  },
  {}
)

In your notebook, however, no reducer is defined for the title group option, so it defaults to a function that counts the occurrences in each group and returns the top 5, together with a count. Which is why you see “Y (1)”. This function is a special case (only used as the default title reducer), which I realize has not been documented yet.

For reference:

1 Like

Thanks for detailed explanation.