Go with the data flow
The ggsankeyfier packages allows you to visualise your
data as Sankey or Alluvial diagrams. A Sankey diagram is essentially a
stacked bar plot, where the bands connect bars across stages (on the
x-axis), to show how quantities flow between them.
You can install the development version of ggsankeyfier from GitHub with:
# install.packages("pak")
pak::pak("pepijn-devries/ggsankeyfier")Or get the CRAN version with:
install.packages("ggsankeyfier")ggsankeyfier?ggsankeyfier allows you to add Sankey diagram layers to
a ggplot2::ggplot(). The package also provides
stat_* and position_* functions that allow you
to add all sorts of other layers, such as text and labels.
Furthermore, the data model used by the package allows you to visualise flows that skip stages or even feedback loops.
And last but not least, in contrast to other packages,
ggsankeyfier uses proportional bands. In alluvial and
Sankey diagrams data quantity flows are represented by bands that flow
across stages. In order to effectively visualise quantities, the width
of the band needs to be proportionate to the quantity. Other packages
use a vertically oriented brush to plot the bands, making them narrow
when the slope is steep. The ggsankeyfier package uses a
brush that is perpendicular to the direction of the flow. This keeps the
width along the band constant and proportionate with its representing
quantity.
This is illustrated in the animation below where both panels visualise the same quantity. As a reference a horizontal band representing the same quantity is shown. Note that the orientation of the brush affects its appearance.
As there is some variation in the definition and terminology used in Sankey diagrams, there are some introduced here for consistency across the package documentation. Here we try to adhere to common definitions used in the graph theory. This theory is used to model pairwise relationships between ‘nodes’ which are connected by ‘edges’. These aspects are circled in the illustration below.
The ggsankeyfier package can only visualise structured
graphs. Meaning that each node belongs to a specific stage (arranged
along the x-axis).
As there are no standards in Sankey diagrams, there may be different words representing the same or similar aspects. Therefore, the following thesaurus is presented to provide an overview and hopefully avoid confusion. The list starts with the term preferred in the present package, followed by alternatives.
Like any other ggplot, you start by calling
ggplot2::ggplot2(), provide the data for plotting and
specify aesthetics (aes). Layers with Sankey edges and
nodes are simply added to the plot using the
+-operator:
data("ecosystem_services")
ggplot(ecosystem_services_pivot1,
aes(x = stage, y = RCSES, group = node,
connector = connector, edge_id = edge_id)) +
geom_sankeyedge(v_space = "auto") +
geom_sankeynode(v_space = "auto")For consistency with aesthetics used in other
ggplot2::ggplot() layers, the stage variable should be
assigned to x, the quantity of the nodes and edges to
y and the node identifier to group. In
addition to these ‘standard’ aesthetics, you also need to specify a
connector specifying the direction of an edge (one of
'from' or 'to'); and an edge_id
which is used to determine which connector ends should be paired
together.
Note that the plotting routines require data organised in a
data.frame, with in each row a ‘connector’. A connector is
either the start or an end of an edge. This allows you to provide
different characteristics for each of these ends. However, in most cases
this is not the type of data you will be working with. Check
vignette("data_management"), on how to rearrange your data
for displaying it in a Sankey diagram.
The package gives you much control on the positioning of elements in the diagram. Think of:
vignette("positioning") and
vignette("stacking_order") will show you how.
When creating your own Sankey diagrams you may want to alter its appearance. You may want to:
Check vignette("decorating") to discover how this is
done.
Please note that the ggsankeyfier project is released
with a Contributor
Code of Conduct. By contributing to this project, you agree to abide
by its terms.
This package was development as part of the EU GES4SEAS project (EU call HORIZON-CL6-2021-BIODIV-01-04, grant agreement 101059877) and the WUR Knowledge Base Research program KB-36-003-022 “The use of ecosystem services to conserve biodiversity in the North Sea” that is supported by finance from the Dutch Ministry of Agriculture, Nature and Food Quality