title: Sankey diagram, alluvial, ggalluvial in R – Three data blocks: Baseline-Flow (many time points)-Outcome
body: <p>We would like to present the change in muscle mass due to the exercise of different age group and the final performance/outcome at the competition at the end of the study.<br>
  We have several time points at which the muscle mass was measured. In this example I only show three time points, however, the study compromises 12 time points.<br>
  To present the change in muscle mass and deviation from the average I was able to use <code>geom_flow()</code>. However, it becomes very tricky to add the age groups on the left of the chart as well as the performance on the right side. These data are located in different variables.
  Please help us to find a great way to present the data. Thanks.</p>
  
  <p>Data Structure:<br>
  ID  Age_at_start    Month   Deviation_muscle    Performance<br>
  1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;36&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   3   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;59    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Outstanding<br>
  1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     36  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   104&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   Outstanding<br>
  1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     36&nbsp;&nbsp;&nbsp;&nbsp;&nbs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-30&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   average<br>
  3   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;22&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  3   &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;310&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     above average<br></p>
  
  <p><img src="https://i.stack.imgur.com/rDs4U.png" alt="Example how the chart should look like"></p>
  
  <pre><code>library(ggplot2)
  library(ggalluvial)
  
  df.san$age&lt;-factor(df.san$age)
  df.san$age&lt;-factor(df.san$age, levels=c(1,2,3,4), labels=c("20 to 24 years","25 to 29 years","30 to 34 years","35 to 39 years"))
  df.san$dev_group &lt;-factor(df.san$dev_group,levels=c(1,2,3,4,5,6,7),labels=c("≥250g","≥150 to &lt;250g","≥50 to &lt;150g","&gt; -50 to &lt;50g","&gt; -150 to ≤ -50","&gt; -250 to ≤ -150", "≤ -250g"))
  df.san$month &lt;- factor(df.san$month,labels=c("1mo","2mo","3mo"))
  df.san$perform&lt;-factor(df.san$perform,levels=c(1,2,3,4),labels=c("outstanding "," above average "," average "," below average"))
  ggplot(df.san,aes(x = month,stratum = dev_group, alluvium = ID, fill = dev_group,label = dev_group)) +
  scale_fill_brewer(type = "qual", palette = "Set2") +
  geom_flow(stat = "alluvium", lode.guidance = "rightleft", color = "darkgray") +
  geom_stratum() +
  theme(legend.position = "bottom") +
  ggtitle("Effect of Exercice on Muscle Growth on Performance in 4 Different Age Groups ")
  </code></pre>
  
  <p>Data for df.san:</p>
  
  <pre><code>structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L), age = c(2L, 3L, 3L, 1L, 3L, 1L, 2L, 3L, 4L, 1L, 1L, 3L, 1L, 4L, 4L, 3L, 4L, 3L, 4L, 2L, 2L, 1L, 2L, 4L, 1L, 1L, 4L, 1L, 3L, 1L, 2L, 3L, 4L, 4L, 2L, 2L, 2L, 2L, 4L, 2L, 2L, 4L, 3L, 3L, 2L), month = c(2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L, 2L, 4L, 6L), dev_muscle = c(-109.3, -236.2, -275.4, -44.5, -202.6, -436, 3, -115.8, -136.2, -142.1, -429, -561.4, -49, -248.8, -232.6, -15.9, -171.5, -391.6, -5.8, -21.7, -104.1, 12.6, -33.4, -25.4, -57.3, -50.7, -103.6, -124, -221.4, -457.2, 22.1, -126.9, -79.5, -76.8, -113.2, -129.7, -86.1, -126, -82.9, -10.8, -2.8, 88.3, 41.6, 0.2, 184.7), perform = c(1L, 2L, 1L, 2L, 4L, 1L, 1L, 4L, 3L, 4L, 2L, 4L, 4L, 4L, 2L, 2L, 4L, 3L, 3L, 4L, 1L, 2L, 1L, 1L, 2L, 3L, 2L, 2L, 2L, 1L, 2L, 3L, 2L, 1L, 2L, 4L, 3L, 2L, 1L, 3L, 2L, 1L, 1L, 4L, 4L), dev_group = c(5L, 6L, 7L, 4L, 6L, 7L, 4L, 5L, 5L, 5L, 7L, 7L, 4L, 6L, 6L, 4L, 6L, 7L, 4L, 4L, 5L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 6L, 7L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 3L, 4L, 4L, 2L)), class = "data.frame", row.names = c(NA, -45L))
  </code></pre>