Skip to content
OpenChart

How to make a beeswarm plot

A beeswarm plot draws one dot per data point, so you can see which states fell and by how much. This post builds one and then splits it into groups.

−20%−10%0%10%20%Change in payIndiana-22.4%Washington+19.3%Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal fora rise.−20%−10%0%10%20%Change in payIndiana-22.4%Washington+19.3%Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal fora rise.
Change in teacher pay by state, as a beeswarm. Source: NCES, Digest of Education Statistics, Table 211.60.

Made with OpenChart. How to build this beeswarm plot

Which states cut teacher pay the most? Between 1999-00 and 2021-22 the average public-school teacher salary, adjusted for inflation, fell in 36 of 51 jurisdictions (the 50 states and DC), according to the National Center for Education Statistics. A histogram of those changes shows how many states fell by how much, but it cannot name a single one of them.

−20%−10%0%10%20%Change in pay0246810121416Teacher pay fell in 36 of 50 statesChange in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. Each bar counts states.−20%−10%0%10%20%Change in pay0246810121416Teacher pay fell in 36 of 50 statesChange in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. Each bar counts states.
Change in teacher pay by state, as a histogram. Source: NCES, Digest of Education Statistics, Table 211.60.

Made with OpenChart. How to build this histogram

{
  "mark": {
    "type": "histogram",
    "binCount": 10
  },
  "data": [
    {
      "change": -8.7
    },
    {
      "change": -4.8
    },
    {
      "change": -11.2
    },
    {
      "change": -5.6
    },
    {
      "change": 9.9
    },
    {
      "change": -8
    },
    // … 45 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. Each bar counts states."
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "histogram",
    "binCount": 10
  },
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. Each bar counts states."
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "histogram",
    "binCount": 10
  },
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. Each bar counts states."
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "histogram",
    "binCount": 10
  },
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. Each bar counts states."
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": {
    "type": "histogram",
    "binCount": 10
  },
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    }
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. Each bar counts states."
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

The tallest bar is the group of states whose pay fell by about 10%. Which states those are, the chart cannot say, and an unusual state is just a short bar at the edge.

How a beeswarm plot works

A beeswarm plot draws each observation as its own dot along one axis, and nudges dots apart, off the axis, when they would overlap. The clusters that form are the shape of the distribution, and every state is still there to point at.

−20%−10%0%10%20%Change in payIndiana-22.4%Washington+19.3%Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal fora rise.−20%−10%0%10%20%Change in payIndiana-22.4%Washington+19.3%Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal fora rise.
Live example. Source: NCES, Digest of Education Statistics, Table 211.60.

Made with OpenChart. How to build this beeswarm plot

{
  "mark": "beeswarm",
  "data": [
    {
      "state": "Alabama",
      "change": -8.7,
      "direction": "Fell",
      "lane": "States"
    },
    {
      "state": "Alaska",
      "change": -4.8,
      "direction": "Fell",
      "lane": "States"
    },
    {
      "state": "Arizona",
      "change": -11.2,
      "direction": "Fell",
      "lane": "States"
    },
    {
      "state": "Arkansas",
      "change": -5.6,
      "direction": "Fell",
      "lane": "States"
    },
    {
      "state": "California",
      "change": 9.9,
      "direction": "Rose",
      "lane": "States"
    },
    {
      "state": "Colorado",
      "change": -8,
      "direction": "Fell",
      "lane": "States"
    },
    // … 45 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    },
    "y": {
      "field": "lane",
      "type": "nominal",
      "axis": false
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Fell",
          "Rose"
        ]
      }
    },
    "detail": {
      "field": "state",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "change",
        "type": "quantitative",
        "title": "Change in pay (%)",
        "format": "+.1f"
      }
    ]
  },
  "annotations": [
    {
      "type": "text",
      "x": -22.4,
      "y": "States",
      "text": "**Indiana**\n-22.4%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    },
    {
      "type": "text",
      "x": 19.3,
      "y": "States",
      "text": "**Washington**\n+19.3%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    }
  ],
  "responsive": {
    "compactAnnotationLimit": 2
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#c2364a",
          "dark": "#f07686"
        },
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal for a rise."
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "beeswarm",
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    },
    "y": {
      "field": "lane",
      "type": "nominal",
      "axis": false
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Fell",
          "Rose"
        ]
      }
    },
    "detail": {
      "field": "state",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "change",
        "type": "quantitative",
        "title": "Change in pay (%)",
        "format": "+.1f"
      }
    ]
  },
  "annotations": [
    {
      "type": "text",
      "x": -22.4,
      "y": "States",
      "text": "**Indiana**\n-22.4%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    },
    {
      "type": "text",
      "x": 19.3,
      "y": "States",
      "text": "**Washington**\n+19.3%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    }
  ],
  "responsive": {
    "compactAnnotationLimit": 2
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#c2364a",
          "dark": "#f07686"
        },
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal for a rise."
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "beeswarm",
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    },
    "y": {
      "field": "lane",
      "type": "nominal",
      "axis": false
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Fell",
          "Rose"
        ]
      }
    },
    "detail": {
      "field": "state",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "change",
        "type": "quantitative",
        "title": "Change in pay (%)",
        "format": "+.1f"
      }
    ]
  },
  "annotations": [
    {
      "type": "text",
      "x": -22.4,
      "y": "States",
      "text": "**Indiana**\n-22.4%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    },
    {
      "type": "text",
      "x": 19.3,
      "y": "States",
      "text": "**Washington**\n+19.3%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    }
  ],
  "responsive": {
    "compactAnnotationLimit": 2
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#c2364a",
          "dark": "#f07686"
        },
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal for a rise."
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "beeswarm",
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    },
    "y": {
      "field": "lane",
      "type": "nominal",
      "axis": false
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Fell",
          "Rose"
        ]
      }
    },
    "detail": {
      "field": "state",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "change",
        "type": "quantitative",
        "title": "Change in pay (%)",
        "format": "+.1f"
      }
    ]
  },
  "annotations": [
    {
      "type": "text",
      "x": -22.4,
      "y": "States",
      "text": "**Indiana**\n-22.4%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    },
    {
      "type": "text",
      "x": 19.3,
      "y": "States",
      "text": "**Washington**\n+19.3%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    }
  ],
  "responsive": {
    "compactAnnotationLimit": 2
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#c2364a",
          "dark": "#f07686"
        },
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal for a rise."
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 51 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "beeswarm",
  "encoding": {
    "x": {
      "field": "change",
      "type": "quantitative",
      "axis": {
        "title": "Change in pay",
        "format": ".0f",
        "labelSuffix": "%"
      }
    },
    "y": {
      "field": "lane",
      "type": "nominal",
      "axis": false
    },
    "color": {
      "field": "direction",
      "type": "nominal",
      "scale": {
        "domain": [
          "Fell",
          "Rose"
        ]
      }
    },
    "detail": {
      "field": "state",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "change",
        "type": "quantitative",
        "title": "Change in pay (%)",
        "format": "+.1f"
      }
    ]
  },
  "annotations": [
    {
      "type": "text",
      "x": -22.4,
      "y": "States",
      "text": "**Indiana**\n-22.4%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    },
    {
      "type": "text",
      "x": 19.3,
      "y": "States",
      "text": "**Washington**\n+19.3%",
      "anchor": "top",
      "offset": {
        "dx": 0,
        "dy": -12
      },
      "connector": false,
      "dot": false
    }
  ],
  "responsive": {
    "compactAnnotationLimit": 2
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#c2364a",
          "dark": "#f07686"
        },
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        }
      ]
    }
  },
  "chrome": {
    "title": "Teacher pay fell in 36 of 50 states, most in Indiana (22.4%)",
    "subtitle": "Change in average public-school teacher salary, constant dollars, 1999-00 to 2021-22. One dot per state, red for a fall and teal for a rise."
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Indiana fell furthest, down 22.4%, and Washington rose most, up 19.3%. Hover over a dot to see its state.

Read the swarm the way you would read a histogram: where dots crowd together, many states share a value. Of the 36 states that fell, 23 lost between 5% and 12%, and the 14 states and DC that gained are spread thinly along the right.

Only the position along the axis means anything. The nudge off the axis exists to keep dots from covering each other, so a dot sitting higher than its neighbor is not larger or later. Add a caption saying so for readers new to the chart.

Splitting a swarm into groups

A second field turns one swarm into one lane per group. Here, each dot is one of the 125 college majors with at least 5,000 recent graduates, placed by its graduates’ median pay, and each lane is a field of study.

$30k$40k$50k$60kMedian early-career payEngineeringComputers & MathBusinessPhysical SciencesSocial ScienceLaw & Public PolicyHealthAgricultureBiologyCommunicationsInterdisciplinaryEducationIndustrial ArtsHumanitiesArtsPsychologyEngineering majors have the widest pay range, $40,000 to $65,000Median pay of recent graduates, one dot per major, for all 125 majors with at least 5,000 graduates$30k$40k$50k$60kMedian early-career payEngineeringComputers & MathBusinessPhysical SciencesSocial ScienceLaw & Public PolicyHealthAgricultureBiologyCommunicationsInterdisciplinaryEducationIndustrial ArtsHumanitiesArtsPsychologyEngineering majors have the widest pay range, $40,000 to $65,000Median pay of recent graduates, one dot per major, for all 125 majors with at least 5,000 graduates
Median early-career pay of 125 college majors, by field. Source: FiveThirtyEight, College Majors (recent graduates).

Made with OpenChart. How to build this beeswarm plot

{
  "mark": "beeswarm",
  "data": [
    {
      "major": "Chemical Engineering",
      "field": "Engineering",
      "pay": 65000,
      "group": "Engineering"
    },
    {
      "major": "Aerospace Engineering",
      "field": "Engineering",
      "pay": 60000,
      "group": "Engineering"
    },
    {
      "major": "Biomedical Engineering",
      "field": "Engineering",
      "pay": 60000,
      "group": "Engineering"
    },
    {
      "major": "Computer Engineering",
      "field": "Engineering",
      "pay": 60000,
      "group": "Engineering"
    },
    {
      "major": "Electrical Engineering",
      "field": "Engineering",
      "pay": 60000,
      "group": "Engineering"
    },
    {
      "major": "Mechanical Engineering",
      "field": "Engineering",
      "pay": 60000,
      "group": "Engineering"
    },
    // … 119 more rows; Copy includes them
  ],
  "encoding": {
    "x": {
      "field": "pay",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Median early-career pay",
        "format": "$~s",
        "values": [
          30000,
          40000,
          50000,
          60000
        ]
      }
    },
    "y": {
      "field": "field",
      "type": "nominal",
      "sort": {
        "field": "pay",
        "op": "median",
        "order": "descending"
      }
    },
    "color": {
      "field": "group",
      "type": "nominal",
      "scale": {
        "domain": [
          "Engineering",
          "Other fields"
        ]
      }
    },
    "detail": {
      "field": "major",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "field",
        "type": "nominal",
        "title": "Field"
      },
      {
        "field": "pay",
        "type": "quantitative",
        "title": "Median pay",
        "format": "$,.0f"
      }
    ]
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#94a3b8",
          "dark": "#7c8aa0"
        }
      ]
    }
  },
  "chrome": {
    "title": "Engineering majors have the widest pay range, $40,000 to $65,000",
    "subtitle": "Median pay of recent graduates, one dot per major, for all 125 majors with at least 5,000 graduates"
  }
}
import { Chart } from "@opendata-ai/openchart-react";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 125 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "beeswarm",
  "encoding": {
    "x": {
      "field": "pay",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Median early-career pay",
        "format": "$~s",
        "values": [
          30000,
          40000,
          50000,
          60000
        ]
      }
    },
    "y": {
      "field": "field",
      "type": "nominal",
      "sort": {
        "field": "pay",
        "op": "median",
        "order": "descending"
      }
    },
    "color": {
      "field": "group",
      "type": "nominal",
      "scale": {
        "domain": [
          "Engineering",
          "Other fields"
        ]
      }
    },
    "detail": {
      "field": "major",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "field",
        "type": "nominal",
        "title": "Field"
      },
      {
        "field": "pay",
        "type": "quantitative",
        "title": "Median pay",
        "format": "$,.0f"
      }
    ]
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#94a3b8",
          "dark": "#7c8aa0"
        }
      ]
    }
  },
  "chrome": {
    "title": "Engineering majors have the widest pay range, $40,000 to $65,000",
    "subtitle": "Median pay of recent graduates, one dot per major, for all 125 majors with at least 5,000 graduates"
  }
};

export function Example() {
  return <Chart spec={spec} />;
}
<script setup lang="ts">
import { Chart } from "@opendata-ai/openchart-vue";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 125 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "beeswarm",
  "encoding": {
    "x": {
      "field": "pay",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Median early-career pay",
        "format": "$~s",
        "values": [
          30000,
          40000,
          50000,
          60000
        ]
      }
    },
    "y": {
      "field": "field",
      "type": "nominal",
      "sort": {
        "field": "pay",
        "op": "median",
        "order": "descending"
      }
    },
    "color": {
      "field": "group",
      "type": "nominal",
      "scale": {
        "domain": [
          "Engineering",
          "Other fields"
        ]
      }
    },
    "detail": {
      "field": "major",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "field",
        "type": "nominal",
        "title": "Field"
      },
      {
        "field": "pay",
        "type": "quantitative",
        "title": "Median pay",
        "format": "$,.0f"
      }
    ]
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#94a3b8",
          "dark": "#7c8aa0"
        }
      ]
    }
  },
  "chrome": {
    "title": "Engineering majors have the widest pay range, $40,000 to $65,000",
    "subtitle": "Median pay of recent graduates, one dot per major, for all 125 majors with at least 5,000 graduates"
  }
};
</script>

<template>
  <Chart :spec="spec" />
</template>
<script lang="ts">
import { Chart } from "@opendata-ai/openchart-svelte";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 125 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "beeswarm",
  "encoding": {
    "x": {
      "field": "pay",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Median early-career pay",
        "format": "$~s",
        "values": [
          30000,
          40000,
          50000,
          60000
        ]
      }
    },
    "y": {
      "field": "field",
      "type": "nominal",
      "sort": {
        "field": "pay",
        "op": "median",
        "order": "descending"
      }
    },
    "color": {
      "field": "group",
      "type": "nominal",
      "scale": {
        "domain": [
          "Engineering",
          "Other fields"
        ]
      }
    },
    "detail": {
      "field": "major",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "field",
        "type": "nominal",
        "title": "Field"
      },
      {
        "field": "pay",
        "type": "quantitative",
        "title": "Median pay",
        "format": "$,.0f"
      }
    ]
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#94a3b8",
          "dark": "#7c8aa0"
        }
      ]
    }
  },
  "chrome": {
    "title": "Engineering majors have the widest pay range, $40,000 to $65,000",
    "subtitle": "Median pay of recent graduates, one dot per major, for all 125 majors with at least 5,000 graduates"
  }
};
</script>

<Chart {spec} />
import { createChart } from "@opendata-ai/openchart-vanilla";
import type { ChartSpec } from "@opendata-ai/openchart-core";
import "@opendata-ai/openchart-core/styles.css";

const data = [/* 125 rows; Copy includes them */];

const spec: ChartSpec = {
  data,
  "mark": "beeswarm",
  "encoding": {
    "x": {
      "field": "pay",
      "type": "quantitative",
      "scale": {
        "zero": false
      },
      "axis": {
        "title": "Median early-career pay",
        "format": "$~s",
        "values": [
          30000,
          40000,
          50000,
          60000
        ]
      }
    },
    "y": {
      "field": "field",
      "type": "nominal",
      "sort": {
        "field": "pay",
        "op": "median",
        "order": "descending"
      }
    },
    "color": {
      "field": "group",
      "type": "nominal",
      "scale": {
        "domain": [
          "Engineering",
          "Other fields"
        ]
      }
    },
    "detail": {
      "field": "major",
      "type": "nominal"
    },
    "tooltip": [
      {
        "field": "field",
        "type": "nominal",
        "title": "Field"
      },
      {
        "field": "pay",
        "type": "quantitative",
        "title": "Median pay",
        "format": "$,.0f"
      }
    ]
  },
  "legend": {
    "show": false
  },
  "theme": {
    "colors": {
      "categorical": [
        {
          "light": "#0e7490",
          "dark": "#22a3c4"
        },
        {
          "light": "#94a3b8",
          "dark": "#7c8aa0"
        }
      ]
    }
  },
  "chrome": {
    "title": "Engineering majors have the widest pay range, $40,000 to $65,000",
    "subtitle": "Median pay of recent graduates, one dot per major, for all 125 majors with at least 5,000 graduates"
  }
};

const container = document.getElementById("chart");
if (!container) throw new Error('Add <div id="chart"></div> to the page first.');
createChart(container, spec);

Engineering, in teal, has the widest spread, with majors across a $25,000 range. Many majors share the same pay, because the source rounds it, and identical values stack into columns of dots instead of covering each other.

Seventeen majors share a median of exactly $35,000, spread across 13 fields, and the column of dots at $35,000 shows each of them. A box plot draws the middle half of the values as one box, and it can’t show that 17 majors share exactly $35,000.

A beeswarm shows one number at a time. To ask whether two numbers move together, such as pay and the share of women in a major, use a scatter plot instead.

How many points to plot

Every row gets a dot, so the plot runs out of room as the rows multiply. With thousands of points the dots either shrink until they are specks or stack so far off the axis that neighboring lanes overlap. A few hundred points is a practical ceiling. Past that, use a histogram or a density curve and give up the individual dots.

At the other end, a handful of points that never overlap do not need dodging. A tick strip draws one tick per value on a shared axis and is simpler.

The spec

Set mark to "beeswarm" and put the value field on encoding.x. The teacher chart also puts a field with one value, lane, on y with axis: false, so all the dots share one lane and the two text annotations have a y position to label. Its color field is direction, which each row carries as Fell or Rose, worked out from the change before charting. theme.colors.categorical gives the two colors in domain order, each a { light, dark } pair so the dots stay clear on a dark page.

detail: { field: "state" } puts the state’s name at the top of each tooltip, and tooltip lists the rows under it, with format: "+.1f" printing the change to one decimal. legend: { show: false } drops the legend because the subtitle names the colors. On a phone a chart moves annotations into tooltips, and responsive.compactAnnotationLimit: 2 keeps both labels on the chart.

For lanes, add a nominal field on encoding.y, as the majors chart does with its field of study. Its sort object, { field: "pay", op: "median", order: "descending" }, orders the lanes by median pay, highest at the top because y lists categories from top to bottom. Swap x and y to turn the swarm vertical.

To size each dot by a second number, such as the number of graduates, see the beeswarm page.