rcategorical-datasankey-diagramnetworkd3

Making a Sankey Diagram in R


I'm trying to create a Sankey Diagram. I am using R with either {plotly} or {networkD3} packages. Both ask for the same type of data: source, target, value. I'm not really sure what source, target, and value is supposed to be and how to aggregate my data to this format. I have the following:

data.frame(
  UniqID = rep(c(1:10), times=4), 
  Year = c(rep("2005", times=10), rep("2010", times=10), rep("2015", times=10), rep("2020", times=10)),
  Response_Variable = round(runif(n = 40, min = 0, max = 2), digits = 0)
)

The response variable is a categorical variable of 0, 1, or 2. I would like to show the flow of the classes of this variable from one year to the next. The final product should look something like this:

enter image description here

In my case, "Wave" would be Year and "Outcome" would be the classes (0, 1, 2) of the response variable.


Solution

  • The answer is to use ggsankey and not plotly nor networkD3!