gpt4 book ai didi

r - R 中的桑基图

转载 作者:行者123 更新时间:2023-12-05 02:15:40 25 4
gpt4 key购买 nike

我正致力于在 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)

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

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

最佳答案

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

enter image description here

关于r - R 中的桑基图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51538812/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com