<h2>Step 1: Create a Tidy data frame</h2>
<p>The very first step in creating visualizations is to get the data in a useful format. In the case of <em>Sankey diagrams</em>, the trick is to get the data into the tidy data format. This post uses a simple example to make it clear how everything fits together. Below, you can see the R code to create a small data frame. I’ve shown this as a table, followed by the resulting Sankey diagram.</p>
<pre> 
my.data = data.frame(Married = c(&quot;Yes&quot;,&quot;Yes&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;No&quot;),
    Pet = c(&quot;Yes&quot;, &quot;Yes&quot;, &quot;No&quot;, &quot;Yes&quot;, &quot;No&quot;),
    Happy = c(&quot;Yes&quot;, &quot;Yes&quot;, &quot;Yes&quot;, &quot;Yes&quot;, &quot;No&quot;),
    freq = 5:1)
</pre>
<p><iframe src="https://embedded.azureedge.net/75668/289598/d1e4cf36-8ae6-4fe7-9939-9d32118abfc8.html?v=31964-3e15184768-624655995" width="432" height="177"></iframe></p>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/289598/c70d130c-dbfb-4e90-af5b-2cc2a30ddc89.html?v=31964-3e151847681129926858"></iframe></p>
<p>A few things to note:</p>
<ul>
<li>The Sankey diagram is in a different order to the data in the table, with “no” appearing before “yes”. Sankey automatically orders the categories to minimize the amount of overlap.</li>
<li>Where two rows in the table contain the same information, Sankey automatically combines them. In our table, we can see that the first two rows are the same. Our Sankey diagram has combined them so the flow from Married: Yes to Pet: Yes to Happy: Yes has a weight (width) of 5 + 4 = 9. You can see this value if you hover your mouse over the Sankey diagram.</li>
<li>The Sankey diagram automatically merges together the <em>nodes </em>(blocks) that have the same text. For example, while we have five rows of data in the example above, we only have two unique values of <strong>Pet</strong>, which is why only two blocks for pet ownership appears.</li>
<li> We can pull apart the blocks by changing the labels, as shown in the data frame and resulting Sankey diagram below. My colleague, Carmen is working on modifying the code to be able to split these apart without changing the labels.</li>
</ul>
<p><iframe width="450" src="https://embedded.azureedge.net/75668/289598/84f1171a-c3b4-4b3f-a418-da4f64c0e222.html?v=31964-3e15184768-681746200"></iframe>
</p>
<h2>Step 2: Install the flipPlot package</h2>
<p>The Sankey diagrams I am using in this post, come from our flipPlots package (<span>Displayr/flipPlots</span>). If you don’t know how to install from GitHub, please see <a rel="nofollow" href="https://www.displayr.com/installing-r-packages-from-github/" target="_blank">how to install packages from GitHub</a>.</p>
<h2>Step 3: Create the Sankey diagram</h2>
<p>We created the first of the Sankey diagrams shown in this post using the code below. Note that the data frame is passed in as the first argument, but the fourth column (the one containing the weight) has been removed. I’ve set <span>link.color</span> to <span>“Source”</span>, which sets the colors that emanate from the same node to be consistent.</p>
<pre>
library(flipPlots)
SankeyDiagram(my.data[, -4],
              link.color = &quot;Source&quot;, 
              weights = my.data$freq) 
</pre>
<p>I’ve provided the code for the second sankey diagram shown in the post below. The only difference from the previous code is that I’ve used <span>label.show.varname = FALSE</span>, to prevent the variable names to from being shown in the sankey diagram.</p>
<pre>
library(flipPlots)
SankeyDiagram(my.data.2[, -4],
              link.color = &quot;Source&quot;,
              label.show.varname = FALSE,
              weights = my.data.2$freq)
</pre>
<p> </p>
<h2>More complicated sankey diagrams</h2>
<p>If you want to create more complicated Sankey diagrams, which do not easily fit into the structure of a table (data frame), please see <a rel="nofollow" href="https://www.displayr.com/sankey-diagrams-r/" target="_blank">Creating Custom Sankey Diagrams Using R</a>.</p>
<h3><span>Acknowledgements</span></h3>
<p>The Sankey diagrams are created using a modified version of <span><a rel="nofollow" href="https://christophergandrud.github.io/networkD3/" target="_blank">networkD3</a></span>, created by Kenton Russell (<span>timelyportfolio/networkD3@feature/responsive</span>). <span>networkD3</span> is an HTMLwidget version of <a rel="nofollow" href="https://bost.ocks.org/mike/sankey/" target="_blank">Mike Bostock’s D3 Sankey diagram</a> code, which is inspired by Tom Counsell’s <a rel="nofollow" href="http://tamc.github.com/Sankey/" target="_blank">Sankey library</a>.</p>
	<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/tgfxb5z5hc0" height="1">