Search code examples
jsonchartsvisualizationvega-litevega

Legend Alignment in Vega Lite


I hope you're all doing well.

I'm conducting some tests and would like to know if anyone has experience with a similar situation.

I'm working with a horizontal bar chart and have placed the legends at the top. Currently, with the settings orient="top" and direction="horizontal," the legends align with the Y-axis line. However, my goal is to align the legends with the left margin of the chart.

Here's an illustrative image showing the current alignment. If anyone has any tips or experience in adjusting the legend alignment in this way, I would greatly appreciate the help.

Current version

enter image description here

I would like it to look like this.

Is it possible?

enter image description here

Thank you in advance for your assistance.

I tried the commands orient="top" and direction="horizontal," but it didn't change the position.

{
    "$schema":"https://vega.github.io/schema/vega-lite/v5.json",
    "description":"A simple bar chart with embedded data.",
    "width":630,
    "data":{
        "values":[
            {
                "Local":"ALL",
                "Ano":"2023",
                "Valor":27076
            },
            {
                "Local":"SE",
                "Ano":"2023",
                "Valor":14132
            },
            {
                "Local":"CW",
                "Ano":"2023",
                "Valor":6434
            },
            {
                "Local":"SO",
                "Ano":"2023",
                "Valor":1036
            },
            {
                "Local":"NO",
                "Ano":"2023",
                "Valor":2684
            },
            {
                "Local":"NW",
                "Ano":"2023",
                "Valor":2790
            },
            {
                "Local":"ALL",
                "Ano":"2024*",
                "Valor":55584
            },
            {
                "Local":"SE",
                "Ano":"2024*",
                "Valor":29507
            },
            {
                "Local":"CW",
                "Ano":"2024*",
                "Valor":11190
            },
            {
                "Local":"SO",
                "Ano":"2024*",
                "Valor":10961
            },
            {
                "Local":"NO",
                "Ano":"2024*",
                "Valor":2454
            },
            {
                "Local":"NW",
                "Ano":"2024*",
                "Valor":1472
            }
        ]
    },
    "layer":[
        {
            "mark":"bar"
        },
        {
            "mark":{
                "type":"text",
                "align":"left",
                "baseline":"middle",
                "font":"Arial",
                "dx":4
            },
            "encoding":{
                "text":{
                    "field":"Valor",
                    "type":"quantitative"
                }
            }
        }
    ],
    "encoding":{
        "y":{
            "field":"Local",
            "type":"nominal",
            "axis":{
                "title":null,
                "labelFont":"Arial",
                "labelFontSize":14
            }
        },
        "x":{
            "field":"Valor",
            "type":"quantitative",
            "axis":{
                "title":null,
                "labelFont":"Arial",
                "labelFontSize":12
            }
        },
        "yOffset":{
            "field":"Ano"
        },
        "color":{
            "field":"Ano"
        }
    },
    "config":{
        "autosize":{
            "type":"fit-x",
            "contains":"padding"
        },
        "background":"#fff",
        "font":"Arial",
        "axis":{
            "domainColor":"#cbcbcb",
            "grid":false,
            "gridColor":"#cbcbcb",
            "gridWidth":1,
            "labelColor":"#000",
            "labelFontSize":10,
            "titleColor":"#333",
            "tickColor":"#cbcbcb",
            "tickSize":10,
            "titleFontSize":14,
            "titlePadding":10,
            "labelPadding":4,
            "title":null
        },
        "text":{
            "fontSize":14
        },
        "axisBand":{
            "grid":false
        },
        "legend":{
            "labelColor":"#333",
            "labelFontSize":11,
            "padding":1,
            "symbolSize":30,
            "symbolType":"square",
            "titleColor":"#333",
            "titleFontSize":14,
            "titlePadding":10,
            "title":null,
            "direction":"horizontal",
            "orient":"top",
            "layout":{
                "top":{
                    "bounds":"full",
                    "margin":10,
                    "center":false
                }
            }
        },
        "view":{
            "stroke":null
        },
        "bar":{
            "binSpacing":2,
            "fill":"#135aa3",
            "stroke":null
        },
        "range":{
            "category":[
                "#135aa3",
                "#f47080"
            ]
        }
    }
}

Solution

  • Here you go.

    enter image description here

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "description": "A simple bar chart with embedded data.",
      "width": 630,
      "padding": {"top": 30},
      "data": {
        "values": [
          {"Local": "ALL", "Ano": "2023", "Valor": 27076},
          {"Local": "SE", "Ano": "2023", "Valor": 14132},
          {"Local": "CW", "Ano": "2023", "Valor": 6434},
          {"Local": "SO", "Ano": "2023", "Valor": 1036},
          {"Local": "NO", "Ano": "2023", "Valor": 2684},
          {"Local": "NW", "Ano": "2023", "Valor": 2790},
          {"Local": "ALL", "Ano": "2024*", "Valor": 55584},
          {"Local": "SE", "Ano": "2024*", "Valor": 29507},
          {"Local": "CW", "Ano": "2024*", "Valor": 11190},
          {"Local": "SO", "Ano": "2024*", "Valor": 10961},
          {"Local": "NO", "Ano": "2024*", "Valor": 2454},
          {"Local": "NW", "Ano": "2024*", "Valor": 1472}
        ]
      },
      "layer": [
        {"mark": "bar"},
        {
          "mark": {
            "type": "text",
            "align": "left",
            "baseline": "middle",
            "font": "Arial",
            "dx": 4
          },
          "encoding": {"text": {"field": "Valor", "type": "quantitative"}}
        }
      ],
      "encoding": {
        "y": {
          "field": "Local",
          "type": "nominal",
          "axis": {"title": null, "labelFont": "Arial", "labelFontSize": 14}
        },
        "x": {
          "field": "Valor",
          "type": "quantitative",
          "axis": {"title": null, "labelFont": "Arial", "labelFontSize": 12}
        },
        "yOffset": {"field": "Ano"},
        "color": {"field": "Ano"}
      },
      "config": {
        "autosize": {"type": "fit-x", "contains": "padding"},
        "background": "#fff",
        "font": "Arial",
        "axis": {
          "domainColor": "#cbcbcb",
          "grid": false,
          "gridColor": "#cbcbcb",
          "gridWidth": 1,
          "labelColor": "#000",
          "labelFontSize": 10,
          "titleColor": "#333",
          "tickColor": "#cbcbcb",
          "tickSize": 10,
          "titleFontSize": 14,
          "titlePadding": 10,
          "labelPadding": 4,
          "title": null
        },
        "text": {"fontSize": 14},
        "axisBand": {"grid": false},
        "legend": {
          "labelColor": "#333",
          "labelFontSize": 11,
          "padding": 0,
          "symbolSize": 30,
          "symbolType": "square",
          "titleColor": "#333",
          "titleFontSize": 14,
          "titlePadding": 10,
          "title": null,
          "direction": "horizontal",
          "orient": "none",
          "legendX": -40,
          "legendY": -20
        },
        "view": {"stroke": null},
        "bar": {"binSpacing": 2, "fill": "#135aa3", "stroke": null},
        "range": {"category": ["#135aa3", "#f47080"]}
      }
    }