R sankeynetwork 强制节点从左到右流动

R sankeynetwork force nodes to flow left to right

我正在使用 package in 创建一个 基于此示例的 sankeynetwork 图:https://christophergandrud.github.io/networkD3/

这是我的代码:

# Load Packages        
library(dplyr)
library(networkD3)

# Read data
a0 <- 
  read.table(header = TRUE, stringsAsFactors = FALSE, text = 
    "from   to  value
    page1/page3 page1/page5/page6/page7 1589
    page1/page3 page1/page3 1172
    page1/page4/page5/  page1/page3 929
    page1/page4/page5/  page1/page4/page5/  834
    page1/page5/page6/page7 page1/page4/page5/  557
    page1/page5/page6/page7 page2/page8/page9/page11    546
    page1/page3 page1/page10/page5/page6/   400
    page1/page10/page5/page6/   page2/page8/page9/page11    373
    page2/page8/page9/page11    page1/page4/page5/  296
    page1/page4/page12/page13/page14    page1/page4/page12/page13/page15    282")

# Get list of distinct variable names, which are needed for nodes    
nodes <- data.frame(name = unique(c(a0$from, a0$to)))

# Create Nodes data frame    
nodes$id <- 0:(nrow(nodes) - 1)

# Create Links data frame    
links <- a0 %>%
  left_join((nodes %>% select(name, id)), 
            by = c("from" = "name")) %>%
  left_join((nodes %>% select(name,id)),
            by = c("to"="name")) %>%
  rename(source = id.x, target = id.y) %>% 
  select(-from,-to)

# Drop ID
nodes <- nodes %>% select(-id)

# Plot
sankeyNetwork(Links = links, Nodes = nodes, Source = 'source',
              Target = 'target', Value = 'value', NodeID = 'name',
              units = 'TWh', fontSize = 12, nodeWidth = 30, iterations = 0)

这是生成的 Sankey 图:

有什么方法可以强制我的网络有一个很好的从左到右的流程来匹配我在顶部提供的示例 link?我已经尝试删除自循环(即从哪里开始=到哪里),但仍然得到类似的结果。

您需要删除循环链接(源节点在目标节点的下游)。

一个非常简单的方法是 links <- links[links$source < links$target, ]

例如...

# Load Packages

library(dplyr)
library(networkD3)

# Read data
a0 <- 
  read.table(header = TRUE, stringsAsFactors = FALSE, text = 
    "from   to  value
    page1/page3 page1/page5/page6/page7 1589
    page1/page3 page1/page3 1172
    page1/page4/page5/  page1/page3 929
    page1/page4/page5/  page1/page4/page5/  834
    page1/page5/page6/page7 page1/page4/page5/  557
    page1/page5/page6/page7 page2/page8/page9/page11    546
    page1/page3 page1/page10/page5/page6/   400
    page1/page10/page5/page6/   page2/page8/page9/page11    373
    page2/page8/page9/page11    page1/page4/page5/  296
    page1/page4/page12/page13/page14    page1/page4/page12/page13/page15    282")


# Get list of distinct variable names, which are needed for nodes

nodes <- data.frame(name = unique(c(a0$from,a0$to)))

# Create Nodes data frame

nodes$id <- 0:(nrow(nodes) - 1)

# Create Links data frame

links <- a0 %>%
  left_join((nodes %>% select(name,id)), 
            by = c("from" = "name")) %>%
  left_join((nodes %>% select(name,id)),
            by = c("to"="name")) %>%
  rename(source = id.x, target = id.y) %>% 
  select(-from,-to)

# Drop ID
nodes <- nodes %>% select(-id)

# remove cyclical links (source node is downstream from target node)
links <- links[links$source < links$target, ]

# Plot
sankeyNetwork(Links = links, Nodes = nodes, Source = 'source',
              Target = 'target', Value = 'value', NodeID = 'name',
              units = 'TWh', fontSize = 12, nodeWidth = 30, iterations = 0)