Skip to content

Multiple bars in a single plot #1032

Answered by mbostock
PatrickBuTaxdoo asked this question in Q&A
Discussion options

You must be logged in to vote

I recommend using faceting for this, and using a “tidy” representation of data where there is one row per observation, rather than treating the “new” and “old” observations as separate dimensions. To tidy the data, you could say:

tidy = data.flatMap((d) => [
  { stat: d.stat, series: "old", time: d.old },
  { stat: d.stat, series: "new", time: d.new }
])

Then to use faceting, you could say:

Plot.plot({
  x: { grid: true, label: "Time (seconds)" },
  y: { label: "Metric", axis: null },
  color: { legend: true },
  facet: {
    data: tidy,
    y: "stat",
    marginLeft: 60
  },
  marks: [
    Plot.barX(tidy, {
      x: "time",
      y: "series",
      fill: "series"
    })
  ]
})

Live note…

Replies: 4 comments 12 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@PatrickBuTaxdoo
Comment options

@PatrickBuTaxdoo
Comment options

@Fil
Comment options

@PatrickBuTaxdoo
Comment options

Answer selected by PatrickBuTaxdoo
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
8 replies
@dgwyer
Comment options

@Fil
Comment options

@dgwyer
Comment options

@Fil
Comment options

@dgwyer
Comment options

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

This discussion was converted from issue #1031 on August 24, 2022 20:15.