Using binX (using the mean reducer) with line plot

I’m experimenting with Bin Transform and line graphs and have a dataset that looks like this:

data = [
{year: 2000, weight: 3, number: 3},
{year: 2000, weight: 7, number: 7},
{year: 2000, weight: 7, number: 4.3},
{year: 2000, weight: 8, number: 9},
{year: 2000, weight: 8, number: 9.1},
{year: 2000, weight: 9, number: 6},
{year: 2000, weight: 9, number: 6},
{year: 2000, weight: 10, number: 5.1},
{year: 2000, weight: 10, number: 8.24},
{year: 2000, weight: 11, number: 2},
{year: 2000, weight: 11, number: 1},
{year: 2001, weight: 3, number: 5},
{year: 2001, weight: 7, number: 7},
{year: 2001, weight: 7, number: 7},
{year: 2001, weight: 8, number: 7},
{year: 2001, weight: 8, number: 4},
{year: 2001, weight: 9, number: 2},
{year: 2001, weight: 9, number: 4},
{year: 2001, weight: 10, number: 3},
{year: 2001, weight: 10, number: 8},
{year: 2001, weight: 11, number: 9},
{year: 2001, weight: 11, number: 4},
{year: 2001, weight: 16, number: 7.5},
]

What I would like to create is a line graph with the x axis = weight, the y axis = number, and z is year, but I would like to get the average number when the weight is the same for the year. Is this possible with binX? This is what I have tried with little success.

Plot.plot({
  width: 1000,
  marks: [
    Plot.lineY(
      data,
      Plot.binX({ y: "mean" }, { x: "weight", z: "year" })
    )
  ]
})

Looks like you’re just missing the y: "number" for the line to tell the bin transform what values to reduce.

Plot.plot({
  marks: [
    Plot.lineY(data, Plot.binX({ y: "mean" }, { x: "weight", y: "number", z: "year" }))
  ]
})

https://observablehq.com/d/29ddba1365891c01

1 Like