# Graph

> Draw a network of nodes and edges with a force layout, then encode size, color and labels and mark a seed node.

A graph draws a network: the things in it as nodes and the connections between them as edges. A force layout pulls connected nodes together and pushes the rest apart, so clusters, bridges and hubs show up without anyone placing them. Use it when the reader needs the shape of the connections.

When the connections carry amounts that flow from stage to stage, a [sankey](/docs/flow/sankey/) shows them better. When the reader needs exact values for each node, put them in a [data table](/docs/tables/data-table/).

## A basic graph

Set `type` to `"graph"`, list the `nodes` (each with an `id`) and the `edges` (each with a `source` and a `target` id). `layout.type: "force"` is the default layout, `chargeStrength` sets how hard nodes repel each other and `linkDistance` sets the distance an edge pulls toward. `layout.seed` makes the layout repeatable: the same spec and seed settle into the same shape.

```json
{
  "type": "graph",
  "nodes": [
    {
      "id": "Ava",
      "label": "Ava",
      "team": "Platform"
    },
    {
      "id": "Liam",
      "label": "Liam",
      "team": "Platform"
    },
    {
      "id": "Mia",
      "label": "Mia",
      "team": "Platform"
    },
    {
      "id": "Noah",
      "label": "Noah",
      "team": "Platform"
    },
    {
      "id": "Zoe",
      "label": "Zoe",
      "team": "Web"
    },
    {
      "id": "Ethan",
      "label": "Ethan",
      "team": "Web"
    },
    {
      "id": "Chloe",
      "label": "Chloe",
      "team": "Web"
    },
    {
      "id": "Leo",
      "label": "Leo",
      "team": "Web"
    },
    {
      "id": "Isla",
      "label": "Isla",
      "team": "Data"
    },
    {
      "id": "Owen",
      "label": "Owen",
      "team": "Data"
    },
    {
      "id": "Nora",
      "label": "Nora",
      "team": "Data"
    },
    {
      "id": "Kai",
      "label": "Kai",
      "team": "Data"
    }
  ],
  "edges": [
    {
      "source": "Ava",
      "target": "Liam"
    },
    {
      "source": "Liam",
      "target": "Mia"
    },
    {
      "source": "Mia",
      "target": "Ava"
    },
    {
      "source": "Noah",
      "target": "Ava"
    },
    {
      "source": "Noah",
      "target": "Liam"
    },
    {
      "source": "Zoe",
      "target": "Ethan"
    },
    {
      "source": "Ethan",
      "target": "Chloe"
    },
    {
      "source": "Chloe",
      "target": "Zoe"
    },
    {
      "source": "Leo",
      "target": "Chloe"
    },
    {
      "source": "Leo",
      "target": "Ethan"
    },
    {
      "source": "Isla",
      "target": "Owen"
    },
    {
      "source": "Owen",
      "target": "Nora"
    },
    {
      "source": "Nora",
      "target": "Isla"
    },
    {
      "source": "Kai",
      "target": "Nora"
    },
    {
      "source": "Kai",
      "target": "Owen"
    },
    {
      "source": "Mia",
      "target": "Zoe"
    },
    {
      "source": "Chloe",
      "target": "Isla"
    },
    {
      "source": "Owen",
      "target": "Ava"
    },
    {
      "source": "Leo",
      "target": "Kai"
    },
    {
      "source": "Noah",
      "target": "Isla"
    }
  ],
  "encoding": {
    "nodeLabel": {
      "field": "label"
    }
  },
  "layout": {
    "type": "force",
    "chargeStrength": -120,
    "linkDistance": 40,
    "seed": 7
  },
  "chrome": {
    "title": "Twelve engineers and who reviewed whose code",
    "subtitle": "An edge means one engineer reviewed the other's pull request"
  }
}
```

Graphs draw on a canvas once the page's script runs, so the figure reserves its height until then. Drag a node to move it; it rejoins the layout when you let go. Scroll or pinch to zoom, and drag the background to pan.

## Clusters

`layout.clustering` names a field, and the layout pulls nodes with the same value together. Here the field is each engineer's team, and `nodeColor` on the same field colors the teams. The legend is interactive by default: clicking a team isolates it, and each entry shows how many nodes it has.

```json
{
  "type": "graph",
  "nodes": [
    {
      "id": "Ava",
      "label": "Ava",
      "team": "Platform"
    },
    {
      "id": "Liam",
      "label": "Liam",
      "team": "Platform"
    },
    {
      "id": "Mia",
      "label": "Mia",
      "team": "Platform"
    },
    {
      "id": "Noah",
      "label": "Noah",
      "team": "Platform"
    },
    {
      "id": "Zoe",
      "label": "Zoe",
      "team": "Web"
    },
    {
      "id": "Ethan",
      "label": "Ethan",
      "team": "Web"
    },
    {
      "id": "Chloe",
      "label": "Chloe",
      "team": "Web"
    },
    {
      "id": "Leo",
      "label": "Leo",
      "team": "Web"
    },
    {
      "id": "Isla",
      "label": "Isla",
      "team": "Data"
    },
    {
      "id": "Owen",
      "label": "Owen",
      "team": "Data"
    },
    {
      "id": "Nora",
      "label": "Nora",
      "team": "Data"
    },
    {
      "id": "Kai",
      "label": "Kai",
      "team": "Data"
    }
  ],
  "edges": [
    {
      "source": "Ava",
      "target": "Liam"
    },
    {
      "source": "Liam",
      "target": "Mia"
    },
    {
      "source": "Mia",
      "target": "Ava"
    },
    {
      "source": "Noah",
      "target": "Ava"
    },
    {
      "source": "Noah",
      "target": "Liam"
    },
    {
      "source": "Zoe",
      "target": "Ethan"
    },
    {
      "source": "Ethan",
      "target": "Chloe"
    },
    {
      "source": "Chloe",
      "target": "Zoe"
    },
    {
      "source": "Leo",
      "target": "Chloe"
    },
    {
      "source": "Leo",
      "target": "Ethan"
    },
    {
      "source": "Isla",
      "target": "Owen"
    },
    {
      "source": "Owen",
      "target": "Nora"
    },
    {
      "source": "Nora",
      "target": "Isla"
    },
    {
      "source": "Kai",
      "target": "Nora"
    },
    {
      "source": "Kai",
      "target": "Owen"
    },
    {
      "source": "Mia",
      "target": "Zoe"
    },
    {
      "source": "Chloe",
      "target": "Isla"
    },
    {
      "source": "Owen",
      "target": "Ava"
    },
    {
      "source": "Leo",
      "target": "Kai"
    },
    {
      "source": "Noah",
      "target": "Isla"
    }
  ],
  "encoding": {
    "nodeColor": {
      "field": "team",
      "type": "nominal"
    },
    "nodeLabel": {
      "field": "label"
    }
  },
  "layout": {
    "type": "force",
    "clustering": {
      "field": "team"
    },
    "chargeStrength": -120,
    "linkDistance": 40,
    "seed": 7
  },
  "chrome": {
    "title": "Three teams of four, linked by five cross-team reviews",
    "subtitle": "Colored and clustered by team"
  }
}
```

## Encoding size, color and edge width

The encoding channels map node and edge fields to marks. `nodeSize` sets the radius from a number (3 to 12 pixels by default), `nodeColor` the fill, and `edgeWidth` the stroke width from 0.5 to 4 pixels. `nodeLabel` names the field the labels show. Labels run on a budget: the graph draws the labels that fit at the current zoom, best-connected nodes first, so more appear as you zoom in.

```json
{
  "type": "graph",
  "nodes": [
    {
      "id": "lab-vision",
      "label": "Vision Lab",
      "citations": 92,
      "kind": "Lab"
    },
    {
      "id": "lab-language",
      "label": "Language Lab",
      "citations": 88,
      "kind": "Lab"
    },
    {
      "id": "lab-robotics",
      "label": "Robotics Lab",
      "citations": 61,
      "kind": "Lab"
    },
    {
      "id": "p-chen",
      "label": "Wei Chen",
      "citations": 74,
      "kind": "Researcher"
    },
    {
      "id": "p-okafor",
      "label": "Ada Okafor",
      "citations": 68,
      "kind": "Researcher"
    },
    {
      "id": "p-silva",
      "label": "Marco Silva",
      "citations": 55,
      "kind": "Researcher"
    },
    {
      "id": "p-hoffman",
      "label": "Ruth Hoffman",
      "citations": 49,
      "kind": "Researcher"
    },
    {
      "id": "p-nakamura",
      "label": "Yuki Nakamura",
      "citations": 44,
      "kind": "Researcher"
    },
    {
      "id": "p-bello",
      "label": "Ines Bello",
      "citations": 38,
      "kind": "Researcher"
    },
    {
      "id": "p-dubois",
      "label": "Luc Dubois",
      "citations": 33,
      "kind": "Researcher"
    },
    {
      "id": "p-ferrari",
      "label": "Gia Ferrari",
      "citations": 27,
      "kind": "Researcher"
    },
    {
      "id": "proj-atlas",
      "label": "Atlas",
      "citations": 80,
      "kind": "Project"
    },
    {
      "id": "proj-lumen",
      "label": "Lumen",
      "citations": 66,
      "kind": "Project"
    },
    {
      "id": "proj-harbor",
      "label": "Harbor",
      "citations": 52,
      "kind": "Project"
    },
    {
      "id": "proj-cobalt",
      "label": "Cobalt",
      "citations": 41,
      "kind": "Project"
    },
    {
      "id": "proj-tessera",
      "label": "Tessera",
      "citations": 29,
      "kind": "Project"
    },
    {
      "id": "ds-imagenet",
      "label": "ImageNet",
      "citations": 96,
      "kind": "Dataset"
    },
    {
      "id": "ds-commoncrawl",
      "label": "Common Crawl",
      "citations": 84,
      "kind": "Dataset"
    },
    {
      "id": "ds-openstreet",
      "label": "OpenStreetMap",
      "citations": 58,
      "kind": "Dataset"
    },
    {
      "id": "ds-census",
      "label": "US Census",
      "citations": 47,
      "kind": "Dataset"
    },
    {
      "id": "ds-arxiv",
      "label": "arXiv",
      "citations": 39,
      "kind": "Dataset"
    }
  ],
  "edges": [
    {
      "source": "p-chen",
      "target": "lab-vision",
      "confidence": 0.98
    },
    {
      "source": "p-okafor",
      "target": "lab-language",
      "confidence": 0.96
    },
    {
      "source": "p-silva",
      "target": "lab-robotics",
      "confidence": 0.95
    },
    {
      "source": "p-hoffman",
      "target": "lab-vision",
      "confidence": 0.93
    },
    {
      "source": "p-nakamura",
      "target": "lab-language",
      "confidence": 0.91
    },
    {
      "source": "p-bello",
      "target": "lab-robotics",
      "confidence": 0.88
    },
    {
      "source": "p-dubois",
      "target": "lab-vision",
      "confidence": 0.72
    },
    {
      "source": "p-ferrari",
      "target": "lab-language",
      "confidence": 0.64
    },
    {
      "source": "lab-vision",
      "target": "proj-atlas",
      "confidence": 0.9
    },
    {
      "source": "lab-vision",
      "target": "proj-cobalt",
      "confidence": 0.61
    },
    {
      "source": "lab-language",
      "target": "proj-lumen",
      "confidence": 0.87
    },
    {
      "source": "lab-language",
      "target": "proj-tessera",
      "confidence": 0.55
    },
    {
      "source": "lab-robotics",
      "target": "proj-harbor",
      "confidence": 0.83
    },
    {
      "source": "lab-robotics",
      "target": "proj-cobalt",
      "confidence": 0.48
    },
    {
      "source": "p-chen",
      "target": "proj-atlas",
      "confidence": 0.86
    },
    {
      "source": "p-hoffman",
      "target": "proj-atlas",
      "confidence": 0.7
    },
    {
      "source": "p-okafor",
      "target": "proj-lumen",
      "confidence": 0.82
    },
    {
      "source": "p-nakamura",
      "target": "proj-lumen",
      "confidence": 0.59
    },
    {
      "source": "p-silva",
      "target": "proj-harbor",
      "confidence": 0.78
    },
    {
      "source": "p-bello",
      "target": "proj-harbor",
      "confidence": 0.51
    },
    {
      "source": "p-dubois",
      "target": "proj-cobalt",
      "confidence": 0.43
    },
    {
      "source": "p-ferrari",
      "target": "proj-tessera",
      "confidence": 0.37
    },
    {
      "source": "proj-atlas",
      "target": "ds-imagenet",
      "confidence": 0.94
    },
    {
      "source": "proj-atlas",
      "target": "ds-openstreet",
      "confidence": 0.46
    },
    {
      "source": "proj-lumen",
      "target": "ds-commoncrawl",
      "confidence": 0.92
    },
    {
      "source": "proj-lumen",
      "target": "ds-arxiv",
      "confidence": 0.68
    },
    {
      "source": "proj-harbor",
      "target": "ds-openstreet",
      "confidence": 0.81
    },
    {
      "source": "proj-harbor",
      "target": "ds-census",
      "confidence": 0.57
    },
    {
      "source": "proj-cobalt",
      "target": "ds-imagenet",
      "confidence": 0.65
    },
    {
      "source": "proj-cobalt",
      "target": "ds-census",
      "confidence": 0.4
    },
    {
      "source": "proj-tessera",
      "target": "ds-arxiv",
      "confidence": 0.53
    },
    {
      "source": "proj-tessera",
      "target": "ds-commoncrawl",
      "confidence": 0.35
    }
  ],
  "encoding": {
    "nodeSize": {
      "field": "citations"
    },
    "nodeColor": {
      "field": "kind"
    },
    "nodeLabel": {
      "field": "label"
    },
    "edgeWidth": {
      "field": "confidence"
    }
  },
  "layout": {
    "type": "force",
    "chargeStrength": -150,
    "linkDistance": 35,
    "seed": 3
  },
  "chrome": {
    "title": "Labs fund projects, and projects use datasets",
    "subtitle": "Size is citations, color is the kind of entity, edge width is link confidence"
  }
}
```

## Edge style and a seed node

`edgeStyle` maps a field to solid, dashed or dotted lines; here it separates local service from express service on a subway line. `seedNode` marks one node. The seed gets a ring and a label that always shows, and it stays lit when a hover, a selection or a legend filter dims the rest. Its size still comes from `nodeSize`.

```json
{
  "type": "graph",
  "nodes": [
    {
      "id": "brooklyn-bridge",
      "label": "Brooklyn Bridge",
      "ridership": 21000,
      "express": true
    },
    {
      "id": "canal",
      "label": "Canal St",
      "ridership": 12000,
      "express": false
    },
    {
      "id": "spring",
      "label": "Spring St",
      "ridership": 8000,
      "express": false
    },
    {
      "id": "bleecker",
      "label": "Bleecker St",
      "ridership": 14000,
      "express": false
    },
    {
      "id": "astor",
      "label": "Astor Pl",
      "ridership": 11000,
      "express": false
    },
    {
      "id": "union-square",
      "label": "14 St-Union Sq",
      "ridership": 45000,
      "express": true
    },
    {
      "id": "23rd",
      "label": "23 St",
      "ridership": 13000,
      "express": false
    },
    {
      "id": "28th",
      "label": "28 St",
      "ridership": 9000,
      "express": false
    },
    {
      "id": "33rd",
      "label": "33 St",
      "ridership": 16000,
      "express": false
    },
    {
      "id": "grand-central",
      "label": "Grand Central",
      "ridership": 42000,
      "express": true
    },
    {
      "id": "51st",
      "label": "51 St",
      "ridership": 18000,
      "express": false
    },
    {
      "id": "59th",
      "label": "59 St",
      "ridership": 27000,
      "express": true
    },
    {
      "id": "68th",
      "label": "68 St-Hunter",
      "ridership": 15000,
      "express": false
    },
    {
      "id": "77th",
      "label": "77 St",
      "ridership": 10000,
      "express": false
    },
    {
      "id": "86th",
      "label": "86 St",
      "ridership": 24000,
      "express": true
    }
  ],
  "edges": [
    {
      "source": "brooklyn-bridge",
      "target": "canal",
      "service": "Local"
    },
    {
      "source": "canal",
      "target": "spring",
      "service": "Local"
    },
    {
      "source": "spring",
      "target": "bleecker",
      "service": "Local"
    },
    {
      "source": "bleecker",
      "target": "astor",
      "service": "Local"
    },
    {
      "source": "astor",
      "target": "union-square",
      "service": "Local"
    },
    {
      "source": "union-square",
      "target": "23rd",
      "service": "Local"
    },
    {
      "source": "23rd",
      "target": "28th",
      "service": "Local"
    },
    {
      "source": "28th",
      "target": "33rd",
      "service": "Local"
    },
    {
      "source": "33rd",
      "target": "grand-central",
      "service": "Local"
    },
    {
      "source": "grand-central",
      "target": "51st",
      "service": "Local"
    },
    {
      "source": "51st",
      "target": "59th",
      "service": "Local"
    },
    {
      "source": "59th",
      "target": "68th",
      "service": "Local"
    },
    {
      "source": "68th",
      "target": "77th",
      "service": "Local"
    },
    {
      "source": "77th",
      "target": "86th",
      "service": "Local"
    },
    {
      "source": "brooklyn-bridge",
      "target": "union-square",
      "service": "Express"
    },
    {
      "source": "union-square",
      "target": "grand-central",
      "service": "Express"
    },
    {
      "source": "grand-central",
      "target": "59th",
      "service": "Express"
    },
    {
      "source": "59th",
      "target": "86th",
      "service": "Express"
    }
  ],
  "encoding": {
    "nodeSize": {
      "field": "ridership"
    },
    "nodeLabel": {
      "field": "label"
    },
    "edgeStyle": {
      "field": "service",
      "type": "nominal"
    }
  },
  "seedNode": "grand-central",
  "layout": {
    "type": "force",
    "chargeStrength": -100,
    "linkDistance": 50,
    "seed": 5
  },
  "chrome": {
    "title": "Express service skips ten of the fifteen stations",
    "subtitle": "Stations sized by weekday ridership; Grand Central is the seed node"
  }
}
```

## Fields that matter

| Field | What it does |
| --- | --- |
| `type` | `"graph"` selects this visualization. |
| `nodes` | The nodes, each with an `id` and any data fields. |
| `edges` | The edges, each with a `source` and a `target` node id and any data fields. |
| `layout.type` | The layout algorithm. `"force"` is the default. |
| `layout.chargeStrength`, `layout.linkDistance` | How hard nodes repel and how long edges pull. |
| `layout.clustering` | A node field whose groups the layout pulls together. |
| `layout.seed` | A number that makes the force layout settle the same way each time. |
| `encoding.nodeSize`, `nodeColor`, `nodeLabel` | Node radius, fill and label text from node fields. |
| `encoding.edgeWidth`, `edgeStyle` | Edge width and line style from edge fields. |
| `seedNode` | The id of a node to mark with a ring and an always-on label; it stays lit when the rest dims. |

## Related

- [Sankey](/docs/flow/sankey/) for amounts that flow between stages.
- [Data table](/docs/tables/data-table/) for exact values per node.
- [Scatter](/docs/correlation/scatter/) when each item has two numbers to place it by, not connections.
- [GraphSpec reference](/docs/spec/graph-spec/) lists every graph field.
