Simple bar chart with multiple series

How can I create bar chart that has multiple series?

Eg. I would like to have for each date (those date numbers are hourly points eg. ‘2024-03-05 10:00:00’) four bars:

  1. this course - started
  2. this course - submitted
  3. other courses - started
  4. other course - submitted

Or, even easier and more general, if i have two measurements (e.g. y1 and y2 for some date point) how can I create two bars, one for y1 and one for y2 (I don’t want stacked bars)

Thanks.

One way you could do it is by faceting, which we can use to provide “inner x axis” in this case. Here I set the normal x channel as the category/type combination, and set the fx channel (the “outer” x axis) to the date.

plot = Plot.plot({
  x: { ticks: [] },
  y: { grid: true },
  fx: { type: "utc" },
  color: { legend: true },
  width,
  marks: [
    Plot.barY(data, {
      x: (d) => `${d.category} - ${d.type}`,
      y: "cnt",
      fx: "date",
      fill: (d) => `${d.category} - ${d.type}`,
      tip: true
    }),
    Plot.ruleY([0])
  ]
})

Another thing you could explore is using multiple Plot.barY marks, one for each bar you want to draw. That has difficulties with bars overlapping though.

1 Like