title: Sankey Networks in R: with Javascript Network D3 or GGforce
body: <p>Recently, I have been learning about Sankey Diagrams and have been trying to recreate them in R (<a href="https://rpubs.com/dmormandy/DV_Sankey" rel="nofollow noreferrer">https://rpubs.com/dmormandy/DV_Sankey</a>).</p>
  <p>Here is my imaginary problem: I have a list of people with their first name, middle name and last name. They have common names and their names repeat. I am trying to make a 3 layer (each name is a layer) Sankey Diagram that shows how the names become more unique when you go to the middle name and last name. E.g. there are many &quot;John's&quot;, but there is only one &quot;John Claude Frank&quot;.</p>
  <p>Here is the fake data I created:</p>
  <pre><code>library(dplyr)
  
  name_data &lt;- data.frame(
      &quot;City&quot; = c(&quot;Paris&quot;, &quot;Paris&quot;, &quot;Paris&quot;, &quot;Paris&quot;, &quot;Paris&quot;, &quot;London&quot;, &quot;London&quot;, &quot;London&quot;, &quot;Paris&quot;, &quot;London&quot;, &quot;Paris&quot;), 
      &quot;First_Name&quot; = c(&quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;James&quot;, &quot;James&quot;, &quot;Adam&quot;, &quot;Adam&quot;, &quot;Henry&quot;),
      &quot;Middle_Name&quot; = c(&quot;Claude&quot;, &quot;Claude&quot;, &quot;Claude&quot;, &quot;Smith&quot;, &quot;Smith&quot;, &quot;Peters&quot;, &quot;Stevens&quot;, &quot;Stevens&quot;, &quot;Ford&quot;, &quot;Tom&quot;, &quot;Frank&quot;),
      &quot;Last Name &quot; = c(&quot;Tony&quot;, &quot;Tony&quot;, &quot;Frank&quot;, &quot;Carson&quot;, &quot;Phil&quot;, &quot;Lewis&quot;, &quot;Eric&quot;, &quot;David&quot;, &quot;Roberts&quot;, &quot;Scott&quot;, &quot;Xavier&quot;)
  )
  </code></pre>
  <p>From here, I understand that you need to create a &quot;links&quot; object and a &quot;nodes&quot; object which will be used to specify the relationships between the names. For the time being, I tried to create an additional column (in Microsoft Excel) that counts the total number of times each name appears in a given column, and then places that number beside the said name. But I don't think this is the right way to solve this problem.</p>
  <p>I tried to accomplish this in R, but I don't think I am doing it right:</p>
  <pre><code>dats_all &lt;- name_data %&gt;%                                                   # data
      group_by( First_Name, Middle_Name, Last.Name.) %&gt;%  # group them
      summarise(Freq = n())                                    # add frequencies
  </code></pre>
  <p>I found this website over here and I am trying to recreate it with my data: <a href="https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html" rel="nofollow noreferrer">https://ggforce.data-imaginist.com/reference/geom_parallel_sets.html</a></p>
  <pre><code>library(ggforce)
  library(reshape2)
  
   name_data &lt;- data.frame(
      &quot;City&quot; = c(&quot;Paris&quot;, &quot;Paris&quot;, &quot;Paris&quot;, &quot;Paris&quot;, &quot;Paris&quot;, &quot;London&quot;, &quot;London&quot;, &quot;London&quot;, &quot;Paris&quot;, &quot;London&quot;, &quot;Paris&quot;),
   
      &quot;First_Name&quot; = c(&quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;John&quot;, &quot;James&quot;, &quot;James&quot;, &quot;Adam&quot;, &quot;Adam&quot;, &quot;Henry&quot;),
      &quot;Middle_Name&quot; = c(&quot;Claude&quot;, &quot;Claude&quot;, &quot;Claude&quot;, &quot;Smith&quot;, &quot;Smith&quot;, &quot;Peters&quot;, &quot;Stevens&quot;, &quot;Stevens&quot;, &quot;Ford&quot;, &quot;Tom&quot;, &quot;Frank&quot;),
      &quot;Last Name &quot; = c(&quot;Tony&quot;, &quot;Tony&quot;, &quot;Frank&quot;, &quot;Carson&quot;, &quot;Phil&quot;, &quot;Lewis&quot;, &quot;Eric&quot;, &quot;David&quot;, &quot;Roberts&quot;, &quot;Scott&quot;, &quot;Xavier&quot;)
  )
  
  name_data$ID &lt;- seq.int(nrow(name_data))
   
  data &lt;- reshape2::melt(name_data)
  data &lt;- gather_set_data(name_data)
  
  ggplot(name_data, aes(x, id = ID, split = First_Name, value = value)) +
    geom_parallel_sets(aes( alpha = 0.3, axis.width = 0.1) +
    geom_parallel_sets_axes(axis.width = 0.1) +
    geom_parallel_sets_labels(colour = 'white')
  </code></pre>
  <p>I think I am getting closer, but there are still some problems. Could someone please show me how to me a basic Sankey Diagram in R (preferably where it shows the number of people at each level, something like this : <a href="https://i.redd.it/lrjdj45xrpo21.png" rel="nofollow noreferrer">https://i.redd.it/lrjdj45xrpo21.png</a>)?</p>