Диаграммы Санки в R

Я работаю над созданием диаграммы Санки в R, но у меня возникают проблемы со значением, стоящим за именами узлов.

Ниже приведен пример, который я нашел в Интернете:

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) 

Набор данных, который я использую ниже:

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

Кто-нибудь знает, как воспроизвести диаграмму Санки выше с этими данными? Я не могу понять, как узлы вступают в игру. Любая помощь будет большой благодарностью!

1 ответ

Решение

Nodes фрейм данных определяет все узлы, которые будут построены, и NodeID вектор в Nodes фрейм данных содержит метку, которая будет отображаться для каждого узла.

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")

Другие вопросы по тегам