过渡的桑基图
Sankey Diagram for transitions
我正在尝试从下面的堆栈溢出问题中复制代码和问题
添加一些样本数据
head(links) #Data.frame
Source Target Weight
Fb Google 20
Fb Fb 2
BBC Google 21
Microsoft BBC 16
head(nodes)
Fb
BBC
Google
Microsoft
构建 sankey 转换流的代码
sankeyNetwork(Links = links,
Nodes = nodes,
Source = "Source",
Target = "Target",
Value = "value",
fontSize = 12,
nodeWidth = 30)
上面提到的堆栈溢出帖子提到源和目标应该在 0 处建立索引。但是,如果我尝试相同的语法,我的源和目标中会出现 NA。什么可能导致此错误?
此代码生成了底部的图。请参阅我的评论以了解您的代码更改的说明。而且,这里有一个很棒的资源:several methods with R
to create Sankey (river) plots.
library(networkD3)
# change to numeric index starting at 0. I assigned Fb to zero, and so on
links <- data.frame(Source = c(0, 0, 1, 2),
Target = c(3, 0, 3, 1),
Weight = c(20, 2, 21, 16))
# a nodes dataframe (or dataframe element of a list, as in the help) is needed
nodes <- data.frame(name = c("Fb", "Google", "BBC", "MS"))
sankeyNetwork(Links = links,
Nodes = nodes,
Source = "Source",
Target = "Target",
Value = "Weight", # changed from "value"
fontSize = 12,
nodeWidth = 30)
您可以像这样将链接数据框中的源和目标变量转换为节点数据框中的节点索引...
links <- read.table(header = T, text = "
Source Target Weight
Fb Google 20
Fb Fb 2
BBC Google 21
Microsoft BBC 16
")
nodes <- read.table(header = T, text = "
name
Fb
BBC
Google
Microsoft
")
# set the Source and Target values to the index of the node (zero-indexed) in
# the nodes data frame
links$Source <- match(links$Source, nodes$name) - 1
links$Target <- match(links$Target, nodes$name) - 1
print(links)
print(nodes)
# use the name of the column in the links data frame that contains the values
# for the value you pass to the Value parameter (e.g. "Weight" not "value")
library(networkD3)
sankeyNetwork(Links = links, Nodes = nodes, Source = "Source",
Target = "Target", Value = "Weight",
fontSize = 12, nodeWidth = 30)
我正在尝试从下面的堆栈溢出问题中复制代码和问题
添加一些样本数据
head(links) #Data.frame
Source Target Weight
Fb Google 20
Fb Fb 2
BBC Google 21
Microsoft BBC 16
head(nodes)
Fb
BBC
Google
Microsoft
构建 sankey 转换流的代码
sankeyNetwork(Links = links,
Nodes = nodes,
Source = "Source",
Target = "Target",
Value = "value",
fontSize = 12,
nodeWidth = 30)
上面提到的堆栈溢出帖子提到源和目标应该在 0 处建立索引。但是,如果我尝试相同的语法,我的源和目标中会出现 NA。什么可能导致此错误?
此代码生成了底部的图。请参阅我的评论以了解您的代码更改的说明。而且,这里有一个很棒的资源:several methods with R
to create Sankey (river) plots.
library(networkD3)
# change to numeric index starting at 0. I assigned Fb to zero, and so on
links <- data.frame(Source = c(0, 0, 1, 2),
Target = c(3, 0, 3, 1),
Weight = c(20, 2, 21, 16))
# a nodes dataframe (or dataframe element of a list, as in the help) is needed
nodes <- data.frame(name = c("Fb", "Google", "BBC", "MS"))
sankeyNetwork(Links = links,
Nodes = nodes,
Source = "Source",
Target = "Target",
Value = "Weight", # changed from "value"
fontSize = 12,
nodeWidth = 30)
您可以像这样将链接数据框中的源和目标变量转换为节点数据框中的节点索引...
links <- read.table(header = T, text = "
Source Target Weight
Fb Google 20
Fb Fb 2
BBC Google 21
Microsoft BBC 16
")
nodes <- read.table(header = T, text = "
name
Fb
BBC
Google
Microsoft
")
# set the Source and Target values to the index of the node (zero-indexed) in
# the nodes data frame
links$Source <- match(links$Source, nodes$name) - 1
links$Target <- match(links$Target, nodes$name) - 1
print(links)
print(nodes)
# use the name of the column in the links data frame that contains the values
# for the value you pass to the Value parameter (e.g. "Weight" not "value")
library(networkD3)
sankeyNetwork(Links = links, Nodes = nodes, Source = "Source",
Target = "Target", Value = "Weight",
fontSize = 12, nodeWidth = 30)