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.
- Load the packages:
library(ggsankey),library(ggplot2)andlibrary(dplyr). - 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,gearandcarb. - 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). - 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). - Add labels. Put
label = nodeinaes(), then addgeom_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. - 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)andtheme(legend.position = "none"). - 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")andtheme_alluvial(base_size = 18). - For a sankey bump plot, skip
make_long()and mapxto a time variable,nodeandfillto the group, andvalueto a size. The README example usesgeom_sankey_bump(space = 0, type = "alluvial", color = "transparent", smooth = 6)withtheme_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()requiresx,next_x,nodeandnext_nodeinaes(). Check that all four are mapped and that the data frame came frommake_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 usingmake_long(), setnext_xandnext_nodetoNAfor 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 putlabel = nodealongside the other aesthetics in the mainaes()call. - Colours or outlines affect more than you want.
fillandcolorchange both nodes and flows. Use the element-specific form, such asnode.color, to change only one. - A legend or colour scale is missing categories. The README examples pass
drop = FALSEto the fill scale, which keeps all factor levels in the scale. Remove the legend withtheme(legend.position = "none")when the labels already name the nodes. - Installation fails. Install
devtoolsfirst, since the README route runs throughdevtools::install_github(), and confirm you spelled the repository asdavidsjoberg/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
- ggsankey README and repository (David Sjoberg), GitHub, read 2026-11-17
- ggsankey DESCRIPTION file, GitHub, read 2026-11-17
- ggalluvial package page, CRAN, read 2026-11-17
- ggsankeyfier package page, CRAN, read 2026-11-17
- networkD3 package page, CRAN, read 2026-11-17
- ggplot2 package page, CRAN, read 2026-11-17
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.
| Package | Distribution | Rendering engine | Diagram types named in the listing |
|---|---|---|---|
| ggsankey | GitHub (development version) | ggplot2 | Sankey, alluvial, sankey bump |
| ggsankeyfier | CRAN | ggplot2 | Sankey, alluvial |
| ggalluvial | CRAN | ggplot2 | Alluvial |
| networkD3 | CRAN | D3 JavaScript | Network, tree, dendrogram, Sankey |
Sources
- ggsankey repository (accessed 2026-11-17)
- ggsankeyfier on CRAN (accessed 2026-11-17)
- ggalluvial on CRAN (accessed 2026-11-17)
- networkD3 on CRAN (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







