0

For an analysis, I want to visualize movements between customer groups with a Sankey plot with the network3D:sankeyNetwork() function. However, I encountered the following issue: in most cases, flows are between a source group (say group A) and a target group (say group B). However, How should we deal with the flow from B to A. Within the current set-up in the Sankey plot, another flow will be visualized. But if we have multiple groups with multiple flows (back and forth), the plot becomes messy and hard to interpret.

How to deal with these specific flows? Is in this specific case a Sankey plot the right visual? Or do we need something else?

Any help would be much appreciated. See sample code below.

library(networkD3)
library(dplyr)
#library(plotly)


links_2 <- tibble(
  from = c(0, 1),
  to = c(1, 0),
  value = c(10, 20)
)

nodes_2 <- tibble(
  nodes = c("a", "b")
)


links_2
nodes_2

# Make the Network
p <- sankeyNetwork(Links = links_2, Nodes = nodes_2,
                   Source = "from", Target = "to",
                   Value = "value", NodeID = "nodes",
                   fontSize=20)
p
Phil
  • 7,287
  • 3
  • 36
  • 66
Liri
  • 350
  • 3
  • 19

1 Answers1

0

It sounds as though you will get a lot more freedom with how you plot this if you treat it as a graph and plot using ggraph

links_2$from <- nodes_2$nodes[links_2$from + 1]
links_2$to <- nodes_2$nodes[links_2$to + 1]

library(ggraph)

ggraph(links_2) + 
  geom_edge_arc(aes(width = value), color = 'steelblue', 
                arrow = arrow(type = 'closed')) + 
  geom_node_point(size = 5, color = '#303050') + 
  geom_node_text(aes(label = name), size = 10, color = 'gray50', 
                 vjust = -1)  + 
  scale_edge_width(range = c(1, 3)) + 
  theme_void() +
  theme(legend.position = 'none')

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87