Skip to content

How can I add data labels to Horizontal stacked bar chart? #1781

Answered by Fil
winner106 asked this question in Q&A
Discussion options

You must be logged in to vote

here's a solution:

Plot.plot({
  marginLeft: 90,
  marks: [
    Plot.barX(
      congress,
      Plot.groupY(
        { x: "count" },
        { fill: "gender", y: "party", sort: { y: "x", reverse: true } }
      )
    ),
    Plot.text(
      congress,
      Plot.stackX(
        Plot.groupY({ x: "count", text: "count" }, { z: "gender", y: "party" })
      )
    )
  ]
})

note that you need to pas the data before the options, and you need to be explicit about stacking when using the text mark (whereas the bar mark stacks implicitly).

Replies: 1 comment 2 replies

Comment options

Fil
Aug 2, 2023
Collaborator

You must be logged in to vote
2 replies
@winner106
Comment options

@Fil
Comment options

Fil Aug 2, 2023
Collaborator

Answer selected by winner106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants