如何在 Deneb/VegaLite 哑铃图中添加垂直线

问题描述 投票:0回答:1

我正在 Power BI 中的 Deneb 创建哑铃图,并且我想添加一条垂直线来表示所有值的平均值。我遵循了使用柱形图here进行类似操作的建议,但我得到的是一系列显示平均值的水平线,而不是显示平均值的一条垂直线:

enter image description here

所以我想看到的是一条代表整体平均值 0.73 的垂直线。

这是一个简单的数据集来重现此问题:

行ID 类别 因素 价值 大平均数
1 类别1 一个 0.5 0.73
2 类别 2 一个 0.75 0.73
3 类别1 两个 0.7 0.73
4 类别 2 两个 0.9 0.73

这是我用来生成此图像的代码:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Dumbbell chart with labels",
  "data":  {"name": "dataset"},
    "transform": [
    {"joinaggregate": [{
      "op": "mean", "field": "Grand Mean", "as": "Grand Mean"
    }]
    }],
  "encoding": {
    "x": {
      "field": "Value",
      "type": "quantitative",
      "title": "Factor Value"//,
    },
    "y": {
      "field": "Categories",
      "type": "nominal",
      "title": null//,
    }
  },
  "layer": [
        {
      "mark": "line",
      "encoding": {
        "detail": {
          "field": "Categories",
          "type": "nominal"
        },
        "color": {"value": "gray"}
      }
    },

    {
      "mark": {
        "type": "point",
        "filled": true
      },
      "encoding": {
        "color": {
          "field": "Factor",
          "type": "ordinal",
           "legend": {
                 "title": null,
                 "offset": 0
            },
          "scale": {
            "domain": ["One", "Two"],
            "range": ["#6ba5cd", "#8e946a"]
          },
          "title": "Legend"
        },
        "size": {"value": 300},
        "opacity": {"value": 1}
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "center",
        "baseline": "bottom",
        "dy": -15
      },

      "encoding": {
        "detail": {
          "field": "Categories",
          "type": "nominal"
        },
        "text": {
          "field": "Factor",
          "type": "ordinal"
        }
      }
    },

    // adding in a red line:
   {"mark": {"type": "rule", "color": "red"
   }, 
    "encoding": {
      "x": {
        "field": "Grand Mean", "type": "quantitative"}
        }
    }
  ]
}

我哪里出错了?

powerbi visualization powerbi-desktop vega-lite deneb
1个回答
0
投票

试试这个:

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Dumbbell chart with labels",
  "data": {
    "name": "dataset"
  },
  "encoding": {
    "x": {
      "field": "Value",
      "type": "quantitative",
      "title": "Factor Value" //,
    },
    "y": {
      "field": "Categories",
      "type": "nominal",
      "title": null //,
    }
  },
  "layer": [
    {
      "mark": "line",
      "encoding": {
        "detail": {
          "field": "Categories",
          "type": "nominal"
        },
        "color": {
          "value": "gray"
        }
      }
    },
    {
      "mark": {
        "type": "point",
        "filled": true
      },
      "encoding": {
        "color": {
          "field": "Factor",
          "type": "ordinal",
          "legend": {
            "title": null,
            "offset": 0
          },
          "scale": {
            "domain": [
              "One",
              "Two"
            ],
            "range": [
              "#6ba5cd",
              "#8e946a"
            ]
          },
          "title": "Legend"
        },
        "size": {
          "value": 300
        },
        "opacity": {
          "value": 1
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "center",
        "baseline": "bottom",
        "dy": -15
      },
      "encoding": {
        "detail": {
          "field": "Categories",
          "type": "nominal"
        },
        "text": {
          "field": "Factor",
          "type": "ordinal"
        }
      }
    },
    // adding in a red line:
    {
      "mark": {
        "type": "rule",
        "color": "red"
      },
      "encoding": {
        "x": {
          "field": "Grand Mean",
          "type": "quantitative",
          "aggregate": "min"
        },
        "y": {}
      }
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.