Can I use Plot.text to show the count of a group?

Hi… Was using Plot to do some analysis of PPP loans and I wanted to group by industry sector, which I was able to figure out.

Displaying the count for each sector using Plot.text on the fly proved to be a bit more challenging. Was wondering if there was a more efficient solution than what I arrived at here.

Essentially I mapped the loans by name of the sector in another cell

sector_count = d3.rollup(
  ppp_loans,
  (v) => v.length,
  (d) => d.name
)

And then accessed the appropriate element based on the name

    Plot.text(ppp_loans, {
      x: 0,
      y: "name",
      text: (d) => sector_count.get(d.name),
      dx: 25
    }),

Is there a way to do this right from Plot.text()?

Try this:

Plot.text(ppp_loans, Plot.groupY({text: "count"}, {x: 0, y: "name", dx: 25}))
1 Like

Yeah, that worked @mbostock. Thank you.

So working backwards, in the docs where it says

Each grouped output channel (the keys of the object passed as the first argument to the group transform) has an associated reducer which controls how the summary value for each group is derived.

groupX() and groupY() makes “count” and all the others available to access for free?

1 Like

The “count” here refers to a reducer or aggregation method. It’s a way of deriving a single value, such as a count, from the set of values that correspond to each group (or bin). More details here: