如何在图例中添加不同的标记类型

huangapple go评论111阅读模式
英文:

How to add different mark types in the legend

问题

I'd like to provide a name for a mark rule via some mark's property (which one), and then add it to the legend? Is it possible?

我想通过某个标记的属性(哪一个)为标记规则提供一个名称,然后将其添加到图例中。是否可能?

英文:

I'd like to provide a name for a mark rule via some mark's property (which one), and then add it to the legend? Is it possible?

I need to provide a few such rules on a chart, so it would be useful to see them all on the legend.

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"X": 1, "Y": 2, "Series": "A"}, 
      {"X": 1, "Y": 3, "Series": "B"}, 
      {"X": 2, "Y": 3, "Series": "A"}, 
      {"X": 2, "Y": 4, "Series": "B"}, 
      {"X": 3, "Y": 4, "Series": "A"}, 
      {"X": 3, "Y": 5, "Series": "B"},
      {"X": 4, "Y": 3, "Series": "A"}, 
      {"X": 4, "Y": 4, "Series": "B"},
      {"X": 5, "Y": 2, "Series": "A"}, 
      {"X": 5, "Y": 3, "Series": "B"}
    ]
  },
  "encoding": {
    "x": {"field": "X", "type": "nominal"},
    "y": {"field": "Y", "aggregate": "sum", "type": "quantitative"},
    "color": {"field": "Series"}
  },
  "layer":[
    {
      "mark":"bar"
    },
    {
      "mark": {
        "type": "rule",
        "stroke": "red",
        "strokeWidth": 1,
        "strokeDash": [8,4]
      },
      "encoding":{
        "x": {},
        "y": {"datum": 6}
      }     
    }    
  ]
}

Vega Lite Editor

如何在图例中添加不同的标记类型

答案1

得分: 0

以下是翻译好的内容,代码部分未翻译:

"Here you go. You might be better off using Vega instead of VL once your viz gets more complicated.

如何在图例中添加不同的标记类型

{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"encoding": {
"x": {"field": "X", "type": "nominal"},
"y": {"field": "Y", "aggregate": "sum", "type": "quantitative"}
},
"layer": [
{
"data": {
"name": "a",
"values": [
{"X": 1, "Y": 2, "Series": "A"},
{"X": 1, "Y": 3, "Series": "B"},
{"X": 2, "Y": 3, "Series": "A"},
{"X": 2, "Y": 4, "Series": "B"},
{"X": 3, "Y": 4, "Series": "A"},
{"X": 3, "Y": 5, "Series": "B"},
{"X": 4, "Y": 3, "Series": "A"},
{"X": 4, "Y": 4, "Series": "B"},
{"X": 5, "Y": 2, "Series": "A"},
{"X": 5, "Y": 3, "Series": "B"}
]
},
"mark": "bar",
"encoding": {
"fill": {"field": "Series", "legend": {"symbolType": "square"}}
}
},
{
"data": {"name": "b", "values": [{"Y": 6, "Series": "Goal", "C": "red"}]},
"mark": {"type": "rule", "strokeWidth": 1, "strokeDash": [8, 4]},
"encoding": {
"x": {},
"y": {"field": "Y"},
"color": {
"field": "Series",
"legend": {"symbolType": "stroke"},
"scale": {"range": {"field": "C"}}
}
}
}
]
}"

英文:

Here you go. You might be better off using Vega instead of VL once your viz gets more complicated.

如何在图例中添加不同的标记类型

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "encoding": {
    "x": {"field": "X", "type": "nominal"},
    "y": {"field": "Y", "aggregate": "sum", "type": "quantitative"}
  },
  "layer": [
    {
      "data": {
        "name": "a",
        "values": [
          {"X": 1, "Y": 2, "Series": "A"},
          {"X": 1, "Y": 3, "Series": "B"},
          {"X": 2, "Y": 3, "Series": "A"},
          {"X": 2, "Y": 4, "Series": "B"},
          {"X": 3, "Y": 4, "Series": "A"},
          {"X": 3, "Y": 5, "Series": "B"},
          {"X": 4, "Y": 3, "Series": "A"},
          {"X": 4, "Y": 4, "Series": "B"},
          {"X": 5, "Y": 2, "Series": "A"},
          {"X": 5, "Y": 3, "Series": "B"}
        ]
      },
      "mark": "bar",
      "encoding": {
        "fill": {"field": "Series", "legend": {"symbolType": "square"}}
      }
    },
    {
      "data": {"name": "b", "values": [{"Y": 6, "Series": "Goal", "C": "red"}]},
      "mark": {"type": "rule", "strokeWidth": 1, "strokeDash": [8, 4]},
      "encoding": {
        "x": {},
        "y": {"field": "Y"},
        "color": {
          "field": "Series",
          "legend": {"symbolType": "stroke"},
          "scale": {"range": {"field": "C"}}
        }
      }
    }
  ]
}

huangapple
  • 本文由 发表于 2023年3月7日 06:55:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75656574.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定