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

时间:2019-02-28 22:04:12

标签: r sankey-diagram htmlwidgets networkd3

我正在使用中的软件包来创建一个 基于以下示例的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图:

Sankey diagram

有什么方法可以迫使我的网络从左到右顺畅流动,以匹配我在顶部提供的示例链接?我尝试删除自循环(即from = to),但仍然得到类似的结果。

1 个答案:

答案 0 :(得分:1)

您需要删除周期性链接(源节点位于目标节点的下游)。

一种非常简单的方法是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)

enter image description here