【问题标题】:Slider for chloropeth map Vega-lite等值线地图 Vega-lite 的滑块
【发布时间】:2022-01-02 16:24:53
【问题描述】:

我正在尝试在 vega-lite 中为我的欧洲 chloropeth 地图添加一个滑块,以按年份过滤数据。我目前有一张仅显示 2019 年数据(颜色编码)的地图,我正在尝试制作一个滑块,以便我可以更改年份并查看颜色随时间的变化情况。

到目前为止,这是我的代码:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "width": 300,
  "height": 300,
  "data": {
    "url": "https://raw.githubusercontent.com/leakyMirror/map-of-europe/master/TopoJSON/europe.topojson",
    "format": {"type": "topojson", "feature": "europe"}
  },
  "transform": [
    {
      "lookup": "properties.NAME",
      "from": {
        "data": {
          "url": "https://raw.githubusercontent.com//jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv"
        },
        "key": "country",
        "fields": ["percentage"]
      }
    }
  ],
    "params": [
    {
      "name": "year",
      "value": 2019,
      "bind": {
        "input": "range",
        "min": 1985,
        "max": 2019,
        "step": 1,
        "name": "Select the year:"
      }
    }
  ],
  "projection": {"type": "naturalEarth1"},
  "mark": "geoshape",
  "encoding": {
    "color": {
      "field": "percentage", 
      "type": "quantitative"},
    "tooltip": [
      {"field": "properties.NAME",      "type": "nominal", "title": "country"},
      {"field": "percentage", "type": "quantitative"}
    ]
  }
}

我已经能够使用随着我移动滑块而更新的相同数据制作条形图。{"filter": "datum.year==year"} 使我的条形图能够做到这一点,但是它在我的 chloropeth 地图上不起作用(我试图在两者的“转换”数组中添加它,我的条形图成功)。这是我的条形图的代码,以防万一。

{
    "$schema": "https://vega.github.io/schema/vega-lite/v5.json",

    "description": "",

    "title": {
      "text": "Thisisatest",
      "subtitle":
        "hellohello Source: OurWorldInData",
      "subtitleFontStyle": "italic",
      "subtitleFontSize": 10,
      "anchor": "start",
      "color": "black"
    },
  
        "data": {
          "url": "https://raw.githubusercontent.com//jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv"
        },   
  
    "height": 300,
    "width": 350,

  "mark": {"type": "bar", "color": "skyblue"},
     
    "transform": [
      {"filter": "datum.year==year"},
      {"filter": {
          "field": "country",
          "oneOf": [
            "United Kingdom", "Spain", "France", "Netherlands", "Portugal", "Italy", "Poland", "Albania", "Germany", "Belgium", "Austria", "Denmark"]}
      }
  ],
  "params": [
    {
      "name": "year",
      "value": 2019,
      "bind": {
        "input": "range",
        "min": 1985,
        "max": 2019,
        "step": 1,
        "name": "Select the year:"
      }
    }
  ],
      "encoding": {
      "y": {
        "field": "percentage",
        "type": "quantitative",
        "title": "Percentage of low carbon energy",
        "axis": {"grid": false}
      },
      "x": {
        "field": "country",
        "type": "nominal",
        "title": "",
        "axis": {"grid": false, "labelAngle": 20},
        "sort": "-y"
      },
      "tooltip": [
        {"field": "country", "title": "Country"},
        {"field": "percentage", "title": "percentage of low carbon energy"}
      ]
    }
    
}

我做错了什么?将不胜感激任何帮助! :)

谢谢

【问题讨论】:

    标签: vega-lite vega vega-embed


    【解决方案1】:

    这几乎是正确的,但是在您的 lookup 转换中,您需要提供最终数据中需要的字段,例如 year 字段。由于没有年份字段,filter 转换不起作用。

    以下是修改后的配置或参考editor:

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "width": 300,
      "height": 300,
      "data": {
        "url": "https://raw.githubusercontent.com//jamesjeffery77/jamesjeffery77.github.io/main/share-electricity-low-carbon_fullDataset.csv"
      },
      "transform": [
        {
          "lookup": "country",
          "from": {
            "data": {
              "url": "https://raw.githubusercontent.com/leakyMirror/map-of-europe/master/TopoJSON/europe.topojson",
              "format": {"type": "topojson", "feature": "europe"}
            },
            "key": "properties.NAME",
            "fields": ["properties", "type", "geometry"]
          }
        },
        {"filter": "datum.year==year"}
      ],
      "params": [
        {
          "name": "year",
          "value": 2019,
          "bind": {
            "input": "range",
            "min": 1985,
            "max": 2030,
            "step": 1,
            "name": "Select the year:"
          }
        }
      ],
      "projection": {"type": "naturalEarth1"},
      "mark": "geoshape",
      "encoding": {
        "color": {"field": "percentage", "type": "quantitative"},
        "tooltip": [
          {"field": "properties.NAME", "type": "nominal", "title": "country"},
          {"field": "percentage", "type": "quantitative"}
        ]
      }
    }
    

    编辑

    我已经倒转了主要数据和查找数据,这似乎为你们的国家带来了所有的岁月。让我知道这是否有效。

    【讨论】:

    • 感谢您的回复!它似乎对我不起作用 - 我只有其中一年(2020 年)的地图,而其他年份没有出现:(
    • Other 不会显示,因为您正在执行 loopup,因为您没有所有可用的国家/地区。因此,这就是您获得欧洲国家数据有限的原因。例如:如果您检查非洲或阿富汗,那么它不是您的数据 source_0 的一部分,但它在 source_1 中,并且您的图表中使用了 souce_0。
    • 啊,好吧,有什么办法可以解决这个问题 - 我的地图只需要欧洲国家,但我只需要所有年份的数据?
    • 如果您的欧洲国家没有除 2019 年和 2020 年以外的年份日期,那么其他年份将如何显示?
    • 他们确实有数据,只是在 source_1 而不是 source_0。
    猜你喜欢
    • 1970-01-01
    • 2020-09-27
    • 2022-08-10
    • 2020-12-06
    • 1970-01-01
    • 2021-05-28
    • 2019-05-29
    • 2020-03-19
    • 2019-01-28
    相关资源
    最近更新 更多