{
  "$schema": "https://vega.github.io/schema/vega-lite/v6.json",
  "title": {
    "text": "Who leads in batting average?",
    "anchor": "start"
  },
  "description": "Compare Derek Jeter and David Justice by season, by all at-bats, or by equally weighted seasons. The final control changes Jeter’s 1996 at-bats for a hypothetical comparison.",
  "data": {
    "values": [
      {
        "Player": "Derek Jeter",
        "Season": 1995,
        "Hits": 12,
        "AtBats": 48
      },
      {
        "Player": "Derek Jeter",
        "Season": 1996,
        "Hits": 183,
        "AtBats": 582
      },
      {
        "Player": "David Justice",
        "Season": 1995,
        "Hits": 104,
        "AtBats": 411
      },
      {
        "Player": "David Justice",
        "Season": 1996,
        "Hits": 45,
        "AtBats": 140
      }
    ]
  },
  "width": 280,
  "height": 85,
  "params": [
    {
      "name": "comparison",
      "value": "1995",
      "bind": {
        "input": "select",
        "options": [
          "1995",
          "1996",
          "Pool both seasons",
          "Weight each season equally"
        ],
        "name": "Compare: "
      }
    },
    {
      "name": "jeterAB",
      "value": 582,
      "bind": {
        "input": "range",
        "min": 183,
        "max": 850,
        "step": 1,
        "name": "Jeter 1996 at-bats (actual 582): "
      }
    }
  ],
  "transform": [
    {
      "filter": "comparison === '1995' ? datum.Season === 1995 : comparison === '1996' ? datum.Season === 1996 : true"
    },
    {
      "calculate": "datum.Player === 'Derek Jeter' && datum.Season === 1996 ? jeterAB : datum.AtBats",
      "as": "UsedAtBats"
    },
    {
      "calculate": "datum.Hits / datum.UsedAtBats",
      "as": "SeasonRate"
    },
    {
      "aggregate": [
        {
          "op": "sum",
          "field": "Hits",
          "as": "Hits"
        },
        {
          "op": "sum",
          "field": "UsedAtBats",
          "as": "AtBats"
        },
        {
          "op": "mean",
          "field": "SeasonRate",
          "as": "EquallyWeighted"
        }
      ],
      "groupby": [
        "Player"
      ]
    },
    {
      "calculate": "comparison === 'Weight each season equally' ? datum.EquallyWeighted : datum.Hits / datum.AtBats",
      "as": "Rate"
    }
  ],
  "encoding": {
    "y": {
      "field": "Player",
      "type": "nominal",
      "title": null,
      "sort": [
        "Derek Jeter",
        "David Justice"
      ]
    },
    "x": {
      "field": "Rate",
      "type": "quantitative",
      "title": "Hits per at-bat",
      "scale": {
        "domain": [
          0,
          1
        ]
      },
      "axis": {
        "format": ".1f",
        "tickCount": 5
      }
    }
  },
  "layer": [
    {
      "mark": {
        "type": "bar",
        "height": 24,
        "color": "#18635a",
        "aria": true
      },
      "encoding": {
        "tooltip": [
          {
            "field": "Player"
          },
          {
            "field": "Rate",
            "type": "quantitative",
            "format": ".3f"
          },
          {
            "field": "Hits",
            "type": "quantitative"
          },
          {
            "field": "AtBats",
            "type": "quantitative"
          }
        ]
      }
    },
    {
      "mark": {
        "type": "text",
        "align": "left",
        "dx": 5,
        "color": "#171917",
        "fontSize": 15,
        "fontWeight": "bold"
      },
      "encoding": {
        "text": {
          "field": "Rate",
          "type": "quantitative",
          "format": ".3f"
        }
      }
    }
  ],
  "config": {
    "view": {
      "stroke": null
    },
    "axis": {
      "labelFontSize": 12,
      "titleFontSize": 12
    }
  }
}
