Search code examples
jsonchartsvisualizationvega-litevega

Concatenate Charts in Vega (rows, columns, grid)


I would like to create sub-vizualizations in Vega using the group mark. Below you will find a minimal example where the second plot is overlapping with the first one as it can be seen in the title.

{
  "$schema": "https://vega.github.io/schema/vega/v5.json",
  "description": "A basic bar chart example, with value labels shown upon pointer hover.",
  "width": 400,
  "height": 200,
  "padding": 5,

  "data": [
    {
      "name": "table",
      "values": [
        {"category": "A", "amount": 28},
        {"category": "B", "amount": 55},
        {"category": "C", "amount": 43},
        {"category": "D", "amount": 91},
        {"category": "E", "amount": 81},
        {"category": "F", "amount": 53},
        {"category": "G", "amount": 19},
        {"category": "H", "amount": 87}
      ]
    }
  ],

  "scales": [
    {
      "name": "xscale",
      "type": "band",
      "domain": {"data": "table", "field": "category"},
      "range": "width",
      "padding": 0.05,
      "round": true
    },
    {
      "name": "yscale",
      "domain": {"data": "table", "field": "amount"},
      "nice": true,
      "range": "height"
    }
  ],

  "axes": [
    { "orient": "bottom", "scale": "xscale" },
    { "orient": "left", "scale": "yscale" }
  ],

  "marks": [
    {
      "type": "group",
      "name": "table1",
      "style": "cell",
      "title": {"text": "First Bar Plot", "frame": "group"},
      "encode": {
        "update": {
          "width": {"signal": "width"},
          "height": {"signal": "height"}
        }
      },
    "marks":[
    {
      "type": "rect",
      "from": {"data":"table"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "category"},
          "width": {"scale": "xscale", "band": 1},
          "y": {"scale": "yscale", "field": "amount"},
          "y2": {"scale": "yscale", "value": 0}
        },
        "update": {
          "fill": {"value": "steelblue"}
        },
        "hover": {
          "fill": {"value": "red"}
        }
      }
    },
    ]
    },

    {
      "type": "group",
      "name": "table2",
      "style": "cell",
      "title": {"text": "Second Bar Plot"},

      "encode": {
        "update": {
          "width": {"signal": "width"},
          "height": {"signal": "height"}
        }
      },

    "marks":[
    {
      "type": "rect",
      "from": {"data":"table"},
      "encode": {
        "enter": {
          "x": {"scale": "xscale", "field": "category"},
          "width": {"scale": "xscale", "band": 1},
          "y": {"scale": "yscale", "field": "amount"},
          "y2": {"scale": "yscale", "value": 0}
        },
        "update": {
          "fill": {"value": "steelblue"}
        },
        "hover": {
          "fill": {"value": "red"}
        }
      }
    },
    ]
    },
    
  ]
}

I would have expected the two plots to be next to each other. Furthermore, I would like to try to put these plot below each other which seems to possible if I understand the docs correctly.

Can someone explain/show me:

  1. How to modify the above code so that the two bar plots appear next to each other.

  2. How to modify the above code so that the second bar plot is below the first one.


Solution

  • You need a layout. With an empty layout:

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega/v5.json",
      "description": "A basic bar chart example, with value labels shown upon pointer hover.",
      "width": 400,
      "height": 200,
      "padding": 5,
      "data": [
        {
          "name": "table",
          "values": [
            {"category": "A", "amount": 28},
            {"category": "B", "amount": 55},
            {"category": "C", "amount": 43},
            {"category": "D", "amount": 91},
            {"category": "E", "amount": 81},
            {"category": "F", "amount": 53},
            {"category": "G", "amount": 19},
            {"category": "H", "amount": 87}
          ]
        }
      ],
      "scales": [
        {
          "name": "xscale",
          "type": "band",
          "domain": {"data": "table", "field": "category"},
          "range": "width",
          "padding": 0.05,
          "round": true
        },
        {
          "name": "yscale",
          "domain": {"data": "table", "field": "amount"},
          "nice": true,
          "range": "height"
        }
      ],
      "axes": [
        {"orient": "bottom", "scale": "xscale"},
        {"orient": "left", "scale": "yscale"}
      ],
      "layout": {},
      "marks": [
        {
          "type": "group",
          "name": "table1",
          "style": "cell",
          "title": {"text": "First Bar Plot", "frame": "group"},
          "encode": {
            "update": {"width": {"signal": "width"}, "height": {"signal": "height"}}
          },
          "marks": [
            {
              "type": "rect",
              "from": {"data": "table"},
              "encode": {
                "enter": {
                  "x": {"scale": "xscale", "field": "category"},
                  "width": {"scale": "xscale", "band": 1},
                  "y": {"scale": "yscale", "field": "amount"},
                  "y2": {"scale": "yscale", "value": 0}
                },
                "update": {"fill": {"value": "steelblue"}},
                "hover": {"fill": {"value": "red"}}
              }
            }
          ]
        },
        {
          "type": "group",
          "name": "table2",
          "style": "cell",
          "title": {"text": "Second Bar Plot"},
          "encode": {
            "update": {"width": {"signal": "width"}, "height": {"signal": "height"}}
          },
          "marks": [
            {
              "type": "rect",
              "from": {"data": "table"},
              "encode": {
                "enter": {
                  "x": {"scale": "xscale", "field": "category"},
                  "width": {"scale": "xscale", "band": 1},
                  "y": {"scale": "yscale", "field": "amount"},
                  "y2": {"scale": "yscale", "value": 0}
                },
                "update": {"fill": {"value": "steelblue"}},
                "hover": {"fill": {"value": "red"}}
              }
            }
          ]
        }
      ]
    }
    

    With columns property:

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega/v5.json",
      "description": "A basic bar chart example, with value labels shown upon pointer hover.",
      "width": 400,
      "height": 200,
      "padding": 5,
      "data": [
        {
          "name": "table",
          "values": [
            {"category": "A", "amount": 28},
            {"category": "B", "amount": 55},
            {"category": "C", "amount": 43},
            {"category": "D", "amount": 91},
            {"category": "E", "amount": 81},
            {"category": "F", "amount": 53},
            {"category": "G", "amount": 19},
            {"category": "H", "amount": 87}
          ]
        }
      ],
      "scales": [
        {
          "name": "xscale",
          "type": "band",
          "domain": {"data": "table", "field": "category"},
          "range": "width",
          "padding": 0.05,
          "round": true
        },
        {
          "name": "yscale",
          "domain": {"data": "table", "field": "amount"},
          "nice": true,
          "range": "height"
        }
      ],
      "axes": [
        {"orient": "bottom", "scale": "xscale"},
        {"orient": "left", "scale": "yscale"}
      ],
      "layout": {"columns":1},
      "marks": [
        {
          "type": "group",
          "name": "table1",
          "style": "cell",
          "title": {"text": "First Bar Plot", "frame": "group"},
          "encode": {
            "update": {"width": {"signal": "width"}, "height": {"signal": "height"}}
          },
          "marks": [
            {
              "type": "rect",
              "from": {"data": "table"},
              "encode": {
                "enter": {
                  "x": {"scale": "xscale", "field": "category"},
                  "width": {"scale": "xscale", "band": 1},
                  "y": {"scale": "yscale", "field": "amount"},
                  "y2": {"scale": "yscale", "value": 0}
                },
                "update": {"fill": {"value": "steelblue"}},
                "hover": {"fill": {"value": "red"}}
              }
            }
          ]
        },
        {
          "type": "group",
          "name": "table2",
          "style": "cell",
          "title": {"text": "Second Bar Plot"},
          "encode": {
            "update": {"width": {"signal": "width"}, "height": {"signal": "height"}}
          },
          "marks": [
            {
              "type": "rect",
              "from": {"data": "table"},
              "encode": {
                "enter": {
                  "x": {"scale": "xscale", "field": "category"},
                  "width": {"scale": "xscale", "band": 1},
                  "y": {"scale": "yscale", "field": "amount"},
                  "y2": {"scale": "yscale", "value": 0}
                },
                "update": {"fill": {"value": "steelblue"}},
                "hover": {"fill": {"value": "red"}}
              }
            }
          ]
        }
      ]
    }
    

    Further reading for all the options:

    https://vega.github.io/vega/docs/layout/