Skip to content

How to convert a stacked bar chart into a grouped bar chart? #7524

Answered by domoritz
kevreth asked this question in Q&A
Discussion options

You must be logged in to vote

Open the Chart in the Vega Editor

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"values":
      [
        {
          "Year": 2002,
          "Rentals": 184,
          "VHS": 84
        },
        {
          "Year": 2003,
          "Rentals": 180,
          "VHS": 67
        }
      ]
  },
 "transform": [{
   "fold": ["Rentals", "VHS"],
   "as": ["type", "sales"]
  }],
  "mark": "bar",
  "encoding": {
    "x": {"field": "type", "type": "ordinal"},
    "y": {"field": "sales", "type": "quantitative"},
    "color": {"field": "type", "type": "nominal"},
    "column": {
      "field": "Year", "type": "ordinal"
    }
  }
}

Note: please use code fences, not <code>.

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
@kevreth
Comment options

@jakevdp
Comment options

@domoritz
Comment options

@kevreth
Comment options

@ezmiller
Comment options

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