2

I'm new to R, and attempting to use sample data to create a Sankey Diagram using rCharts.

I've run this code and it works as intended. However, when I edit that code with my source, target, and value vectors, R crashes and no diagram is produced.

Here is what I'm attempting to enter in place of the above example's source, target and values:

target <- c('1', '1', '1', '1', '2', '2', '2', '3', '3', '3', '4', '4', '4', '4', '4', '5', '5', '5', '5')

source <- c('2', '3', '4', '5', '1', '3', '4', '2', '3', '4', '1', '2', '3', '4', '5', '1', '2', '4', '5')

value <- c(1, 2, 2, 1, 1, 1, 4, 1, 2, 1, 1, 1, 1, 2, 2, 1, 2, 2, 2)

For clarity, here's a smaller example I've produced using my data:

source <- c('2', '5', '5')
target <- c('4', '2', '5')
value <- c(1, 1, 1)

This also crashes. My understanding of Sankey Diagrams is that this should produce a chart where '2' flows to '4', and '5' flows to '2' and '5' with lines of equal thickness.

I'm assuming there's something wrong with the data itself, because I've tried to have the same formatting as the other examples I've seen. If anyone could explain what's wrong with my understanding here, that would be much appreciated!

Community
  • 1
  • 1
Sri Rao
  • 21
  • 3

1 Answers1

1

This also crashes.

It crashes because of the self-loop 5 to 5, which does not make any sense. The node with the name 5 should either be on the source or on the target side.

My understanding of Sankey Diagrams is that this should produce a chart where '2' flows to '4', and '5' flows to '2' and '5' with lines of equal thickness.

You could do e.g.

source <- c('2', '2', '5')
target <- c('4', '5 ', '5 ')
value <- c(1, 1, 1)
df <- cbind.data.frame(source, target, value)
library(rCharts)
sankeyPlot <- rCharts$new()
sankeyPlot$setLib('http://timelyportfolio.github.io/rCharts_d3_sankey')
sankeyPlot$set(
  data = df,
  nodeWidth = 10,
  nodePadding = 15,
  width = 300,
  height = 300
)
sankeyPlot

Notice the tiny difference between 5 in the source and 5 in the target vector...

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • Thank you! I drew out the example I'd linked to and realized my mistake yesterday. I was able to solve it by renaming the variables in the target vector, but I like your version better. – Sri Rao Jun 15 '16 at 13:48