Skip to main content
v2026.11,986 entries · CC-BY 4.0

ggsankey in R: Sankey, Alluvial and Bump Plots with ggplot2

ggsankey draws sankey, alluvial and bump plots in ggplot2: GitHub install, make_long() data format, a worked example and troubleshooting.

Written and maintained by CASRAI Editorial Board

Published

On this page

ggsankey is an R package, written and maintained by David Sjoberg, that adds sankey, alluvial and sankey bump plots to ggplot2. It is installed from GitHub with devtools::install_github("davidsjoberg/ggsankey"), and every plot starts by reshaping your data with make_long() into the stage, node and next-node columns that geom_sankey() requires. This page covers what the package does, how to install it, a worked example, how to read the output, what to check when a plot fails, and which packages to use instead.

What ggsankey is used for in research

The package describes its own goal as making “beautiful sankey, alluvial and sankey bump plots in ggplot2”. Its README borrows Google’s definition of the chart type: a sankey diagram depicts a flow from one set of values to another, the things being connected are called nodes and the connections are called links, and sankeys are best used to show a many-to-many mapping between two domains or multiple paths through a set of stages.

In research data that maps onto any situation where the same units are classified more than once. Typical cases are participants moving between categories across study waves, records moving through screening and inclusion stages, or samples assigned to different groups at successive steps of a pipeline. Each classification becomes a stage, each category within a stage becomes a node, and the links show how many units pass from one node to the next. A sankey makes sense only when those stages are discrete and ordered; for a continuous variable plotted over time, use an ordinary line chart instead.

The package offers three plot families, each with its own geom: geom_sankey() for flows between separated nodes, geom_alluvial() for the same data drawn without gaps between nodes, and geom_sankey_bump() for a bump-style plot that the README describes as mostly useful for time series. The longer comparison of tools for drawing flow charts in R is in the guide to sankey diagrams in R; this page is the reference for the ggsankey package itself.

How to install or access ggsankey

The README gives a single installation route, the development version from GitHub:

install.packages("devtools")
devtools::install_github("davidsjoberg/ggsankey")
library(ggsankey)
library(ggplot2)
library(dplyr)

The package’s DESCRIPTION file lists the title “Sankey, Alluvial and Sankey Bump Plots”, the version 0.0.99999, the MIT licence plus a LICENSE file, and these imports: ggplot2, dplyr, stringr, purrr, tidyr, forcats and magrittr. Bugs are reported through the GitHub issue tracker at github.com/davidsjoberg/ggsankey/issues. The README gives no CRAN install command, so do not expect install.packages("ggsankey") to work.

Reproducibility note for research code

A package installed from a GitHub branch can change between two installs, and the repository’s most recent push, when checked on 2026-11-17, was dated 2024-04-04. For an analysis you intend to rerun or archive, record the commit you installed, run sessionInfo() and save its output with the scripts, and consider installing a fixed commit by adding it after an @ in the repository string. A package that is not on CRAN is also not covered by CRAN’s checks, which is one reason to keep an exact record. The same reasoning is behind the computational reproducibility expectations that journals and funders apply to code.

How to draw a sankey diagram with ggsankey step by step

The steps below follow the README’s basic example, which uses R’s built-in mtcars data to link five categorical columns. Swap in your own data frame and column names for real work.

  1. Load the packages: library(ggsankey), library(ggplot2) and library(dplyr).
  2. Choose the columns that will become stages, in the order you want them drawn from left to right. The README example uses cyl, vs, am, gear and carb.
  3. Reshape the data with make_long(): df = mtcars %>% make_long(cyl, vs, am, gear, carb). The result holds the stage (x), the node, and the instruction for which node each observation belongs to in the next stage (next_x, next_node).
  4. Map the four required aesthetics and a fill, then add the geom: ggplot(df, aes(x = x, next_x = next_x, node = node, next_node = next_node, fill = factor(node))) + geom_sankey() + scale_fill_discrete(drop = FALSE).
  5. Add labels. Put label = node in aes(), then add geom_sankey_label(size = 3, color = "white", fill = "gray40"); the README states that this places labels in the centre of the nodes when it is given the same aesthetics.
  6. Soften and tidy the plot. The README’s styled version uses geom_sankey(flow.alpha = .6, node.color = "gray30"), scale_fill_viridis_d(drop = FALSE), theme_sankey(base_size = 18), labs(x = NULL) and theme(legend.position = "none").
  7. For an alluvial version of the same data, replace the geom, label geom and theme with geom_alluvial(flow.alpha = .6), geom_alluvial_text(size = 3, color = "white") and theme_alluvial(base_size = 18).
  8. For a sankey bump plot, skip make_long() and map x to a time variable, node and fill to the group, and value to a size. The README example uses geom_sankey_bump(space = 0, type = "alluvial", color = "transparent", smooth = 6) with theme_sankey_bump(base_size = 16), applied to the gapminder data grouped by continent and year.

The data contract

Everything in ggsankey rests on how each observation is described. The table lists the columns and what the README says about each one.

Aesthetic What it holds Rule stated in the README
x The stage, a discrete x-value in ggplot Required by geom_sankey()
next_x The stage the observation moves to Required; the last stage points to NA
node The node the observation belongs to at this stage Required
next_node The node it belongs to in the next stage Required; the last stage points to NA
fill, color Appearance Affect both nodes and flows
node.color and similar Appearance of one element only For example, node.color = 'black' outlines the nodes but not the flows

How to read the output

Each vertical column of boxes is one stage, and each box is a node in that stage. The height of a box reflects how many observations sit in that node. The ribbons between boxes are the flows, also called links in Google’s definition quoted in the README, and a ribbon shows how many observations pass from a node in one stage to a node in the next.

The three geoms differ only in layout. In a sankey plot there are spaces between nodes and the stack is centred around the x-axis. In an alluvial plot, the README says there are no spaces between nodes and the plot starts at y = 0 rather than being centred. A sankey bump plot shows group sizes across a time axis, and the README notes that when one group becomes larger than another it bumps above it. Read the sankey and alluvial plots for how units are redistributed between categories, and the bump plot for how the ranking of groups changes.

A flow diagram shows counts moving between categories, not whether a change is statistically meaningful. Report the underlying counts in a table, and do not infer a trend from ribbon thickness alone.

Troubleshooting ggsankey

  • An error about a missing aesthetic. geom_sankey() requires x, next_x, node and next_node in aes(). Check that all four are mapped and that the data frame came from make_long() or has equivalent columns.
  • Flows disappear or the last stage looks wrong. The README states that the last stage should point to NA. If you build the long table yourself instead of using make_long(), set next_x and next_node to NA for the final stage.
  • Labels do not appear in the nodes. The README says geom_sankey_label() centres labels in nodes when it is given the same aesthetics, so put label = node alongside the other aesthetics in the main aes() call.
  • Colours or outlines affect more than you want. fill and color change both nodes and flows. Use the element-specific form, such as node.color, to change only one.
  • A legend or colour scale is missing categories. The README examples pass drop = FALSE to the fill scale, which keeps all factor levels in the scale. Remove the legend with theme(legend.position = "none") when the labels already name the nodes.
  • Installation fails. Install devtools first, since the README route runs through devtools::install_github(), and confirm you spelled the repository as davidsjoberg/ggsankey.

Free alternatives to ggsankey

Other open packages draw similar flow diagrams. The table gives what each package’s own listing says it does; the data table below compares how each is distributed. The ggplot2 reference documents the base plotting system on which the first three depend. For the Python side, see the guide to sankey diagrams in Python.

Package What its listing says Choose it when
ggalluvial “Alluvial Plots in ‘ggplot2′”; variable-width ribbons and stacked bars for categorical and ordinal repeated-measures data The data are repeated categorical measurements and you want a CRAN package
ggsankeyfier “Create Sankey and Alluvial Diagrams Using ‘ggplot2′”; visualises flows of quantities across stages in stacked bars You want a ggplot2 sankey from CRAN
networkD3 “D3 JavaScript Network Graphs from R”; creates network, tree, dendrogram and Sankey graphs You need an interactive JavaScript output instead of a static ggplot2 figure
ggsankey Sankey, alluvial and sankey bump plots in ggplot2, installed from GitHub You want all three plot families, including the bump plot, in one ggplot2 workflow

Do not confuse ggsankey with ggsankeyfier. They are separate packages by different authors, and ggsankeyfier is the one with a CRAN listing.

Sources

Data: Where four R packages for sankey and alluvial diagrams are distributed and what they render

Compares ggsankey, ggsankeyfier, ggalluvial and networkD3 by distribution channel, rendering engine and the diagram types named in each package's own listing, compiled from the GitHub repository and CRAN package pages.

Where four R packages for sankey and alluvial diagrams are distributed and what they render
PackageDistributionRendering engineDiagram types named in the listing
ggsankeyGitHub (development version)ggplot2Sankey, alluvial, sankey bump
ggsankeyfierCRANggplot2Sankey, alluvial
ggalluvialCRANggplot2Alluvial
networkD3CRAND3 JavaScriptNetwork, tree, dendrogram, Sankey

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about ggsankey in R: Sankey, Alluvial and Bump Plots with ggplot2

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →