R中的Sankey图

时间:2018-07-26 12:24:10

标签: r nodes sankey-diagram htmlwidgets networkd3

我正在用R制作sankey图,但是节点名称后面的含义有问题。

以下是我在网上找到的示例:

library(networkD3)
nodes = data.frame("name" = 
                     c("Node A", # Node 0
                      "Node B", # Node 1
                      "Node C", # Node 2
                      "Node D"))# Node 3
links = as.data.frame(matrix(c(
  0, 1, 10, # Each row represents a link. The first number
  0, 2, 20, # represents the node being conntected from. 
  1, 3, 30, # the second number represents the node connected to.
  2, 3, 40),# The third number is the value of the node
  byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
sankeyNetwork(Links = links, Nodes = nodes,
              Source = "source", Target = "target",
              Value = "value", NodeID = "name",
             fontSize= 12, nodeWidth = 30) 

enter image description here

我正在使用的数据集如下:

source<-c('EASTERN PARKWAY', 'CONEY ISLAND AVENUE', 'ATLANTIC AVENUE', 'ATLANTIC AVENUE','ATLANTIC AVENUE','ATLANTIC AVENUE',
      'AVENUE P', 'BAY PARKWAY', 'BUFFALO AVENUE', 'FLATBUSH AVENUE', 'PROSPECT EXPRESSWAY', 'SAINT JOHNS PLACE',
      '6 AVENUE', '65 STREET', '65 STREET', '65 STREET', 'ATLANTIC AVENUE', 'ATLANTIC AVENUE', 'ATLANTIC AVENUE', 'CONEY ISLAND AVENUE')

target<-c('BUFFALO AVENUE', 'AVENUE J', 'CLASSON AVENUE', 'EASTERN PARKWAY', 'HICKS STREET', 'LOGAN STREET',
      'EAST 18 STREET', 'CROPSEY AVENUE', 'EASTERN PARKWAY', 'AVENUE V', 'CHURCH AVENUE', 'ROCHESTER AVENUE',
      'ATLANTIC AVENUE', '17 AVENUE', '18 AVENUE', 'BAY PARKWAY', 'NEVINS STREET', 'UTICA AVENUE', 'VANDERBILT AVENUE', 'AVENUE P')

value<-c(8,5,4,4,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3)
df<-data.frame(source, target, value)
df

   source              target             value
 1 EASTERN PARKWAY     BUFFALO AVENUE      8.00
 2 CONEY ISLAND AVENUE AVENUE J            5.00
 3 ATLANTIC AVENUE     CLASSON AVENUE      4.00
 4 ATLANTIC AVENUE     EASTERN PARKWAY     4.00
 5 ATLANTIC AVENUE     HICKS STREET        4.00
 6 ATLANTIC AVENUE     LOGAN STREET        4.00
 7 AVENUE P            EAST 18 STREET      4.00
 8 BAY PARKWAY         CROPSEY AVENUE      4.00
 9 BUFFALO AVENUE      EASTERN PARKWAY     4.00
10 FLATBUSH AVENUE     AVENUE V            4.00
11 PROSPECT EXPRESSWAY CHURCH AVENUE       4.00
12 SAINT JOHNS PLACE   ROCHESTER AVENUE    4.00
13 6 AVENUE            ATLANTIC AVENUE     3.00
14 65 STREET           17 AVENUE           3.00
15 65 STREET           18 AVENUE           3.00
16 65 STREET           BAY PARKWAY         3.00
17 ATLANTIC AVENUE     NEVINS STREET       3.00
18 ATLANTIC AVENUE     UTICA AVENUE        3.00
19 ATLANTIC AVENUE     VANDERBILT AVENUE   3.00
20 CONEY ISLAND AVENUE AVENUE P            3.00

有人知道如何用这些数据重现上面的sankey图吗?我似乎无法弄清楚节点是如何发挥作用的。任何帮助将非常感谢!

1 个答案:

答案 0 :(得分:4)

Nodes数据帧定义了将要绘制的所有节点,NodeID数据帧中的Nodes向量包含将为每个节点显示的标签。 / p>

links <- read.csv(text = "
source,target,value
EASTERN PARKWAY,BUFFALO AVENUE,8.00
CONEY ISLAND AVENUE,AVENUE J,5.00
ATLANTIC AVENUE,CLASSON AVENUE,4.00
ATLANTIC AVENUE,EASTERN PARKWAY,4.00
ATLANTIC AVENUE,HICKS STREET,4.00
ATLANTIC AVENUE,LOGAN STREET,4.00
AVENUE P,EAST 18 STREET,4.00
BAY PARKWAY,CROPSEY AVENUE,4.00
BUFFALO AVENUE,EASTERN PARKWAY,4.00
FLATBUSH AVENUE,AVENUE V,4.00
PROSPECT EXPRESSWAY,CHURCH AVENUE,4.00
SAINT JOHNS PLACE,ROCHESTER AVENUE,4.00
6 AVENUE,ATLANTIC AVENUE,3.00
65 STREET,17 AVENUE,3.00
65 STREET,18 AVENUE,3.00
65 STREET,BAY PARKWAY,3.00
ATLANTIC AVENUE,NEVINS STREET,3.00
ATLANTIC AVENUE,UTICA AVENUE,3.00
ATLANTIC AVENUE,VANDERBILT AVENUE,3.00
CONEY ISLAND AVENUE,AVENUE P,3.00
")

# build a nodes data frame using all unique names of nodes found in your links
# source *and* target vectors
nodes <- data.frame(name = unique(c(as.character(links$source), as.character(links$target))))

# set the source and target values in your links data frame to the index of the
# node that they refer to in the nodes data frame (0-indexed becauuse it's 
# used by JavaScript)
links$source <- match(links$source, nodes$name) - 1
links$target <- match(links$target, nodes$name) - 1

# plot it
library(networkD3)
sankeyNetwork(Links = links, Nodes = nodes, Source = "source", 
              Target = "target", Value = "value", NodeID = "name")

enter image description here