我正在使用networkD3 R包构建强制网络图,但在显示网络时遇到问题。我可以使用"simpleNetwork“函数,但不能使用"forced_network_df”函数。
代码如下:
library(ggraph)
library(readr)
library(readxl)
library(networkD3)
library(dplyr)
library(tidyr)
library(tidyverse)
forced_network_df <- read_xlsx("vt_forced_network.xlsx")
#Create DF with ID1 ID2 & co_ocur as source, target and value
edge <- forced_network_df %>% select(from, to, value) %>% mutate(from=from-1, to=to-1)
node <- forced_network_df %>% select(ID, Industry1) %>% mutate(ID = ID-1)
#Create forced network diagram
f <- forceNetwork(Links = edge, Nodes = node, Source = "from", Target = "to",
NodeID = "Industry1", Group = "ID", Value = "value",
opacity = 1, width = 550, height = 100,
zoom = TRUE)原始数据帧示例(称为forced_network_df):
ID to from IndustryID1 IndustryID2 Industry1 Industry2 value
<dbl> <dbl> <dbl> <chr> <chr> <chr> <chr> <dbl>
1 1 1 1 3162 1133 Footwear manufacturing Logging 7
2 2 1 2 3162 55 Footwear manufacturing Management of companies & enterprises 8
3 3 1 3 3162 928110P7 Footwear manufacturing Military Reserves or National Guard 6
4 4 1 4 3162 3114 Footwear manufacturing Fruit & vegetable preserving & specialty food manufacturing 6
5 5 1 5 3162 54194 Footwear manufacturing Veterinary services 6
6 6 2 6 311M2 5419Z Seafood & other miscellaneous foods, n.e.c. Other professional, scientific & technical services 7
7 7 2 7 311M2 311S Seafood & other miscellaneous foods, n.e.c. Not specified food industries, manufacturing 6
8 8 2 8 311M2 3118Z Seafood & other miscellaneous foods, n.e.c. Bakeries & tortilla manufacturing, except retail bakeries 6边缘df示例(从forced_network_df创建):
from to value
<dbl> <dbl> <dbl>
1 0 0 7
2 1 0 8
3 2 0 6
4 3 0 6
5 4 0 6
6 5 1 7
7 6 1 6和节点示例(从forced_network_df创建):
ID Industry1
<dbl> <chr>
1 0 Footwear manufacturing
2 1 Footwear manufacturing
3 2 Footwear manufacturing
4 3 Footwear manufacturing
5 4 Footwear manufacturing
6 5 Seafood & other miscellaneous foods, n.e.c.我当前的输出完全是空白的,并且已经处理了数据结构和变量赋值。这里的任何帮助都将不胜感激!
提前谢谢。
发布于 2019-10-26 22:24:34
如果我使用您的数据帧的较大样本,该图对我有效...
library(tidyverse)
library(networkD3)
forced_network_df <- read_csv('
"ID", "to", "from", "IndustryID1", "IndustryID2", "Industry1", "Industry2", "value"
1, 1, 1, 3162, 1133, "Footwear manufacturing", "Logging", 7
2, 1, 2, 3162, 55, "Footwear manufacturing", "Management of companies & enterprises", 8
3, 1, 3, 3162, 928110P7, "Footwear manufacturing", "Military Reserves or National Guard", 6
4, 1, 4, 3162, 3114, "Footwear manufacturing", "Fruit & vegetable preserving & specialty food manufacturing", 6
5, 1, 5, 3162, 54194, "Footwear manufacturing", "Veterinary services", 6
6, 2, 6, 311M2, 5419Z, "Seafood & other miscellaneous foods, n.e.c.", "Other professional, scientific & technical services", 7
7, 2, 7, 311M2, 311S, "Seafood & other miscellaneous foods, n.e.c.", "Not specified food industries, manufacturing", 6
8, 2, 8, 311M2, 3118Z, "Seafood & other miscellaneous foods, n.e.c.", "Bakeries & tortilla manufacturing, except retail bakeries", 6
')
edge <- forced_network_df %>% select(from, to, value) %>% mutate(from=from-1, to=to-1)
node <- forced_network_df %>% select(ID, Industry1) %>% mutate(ID = ID-1)
forceNetwork(Links = edge, Nodes = node, Source = "from", Target = "to",
NodeID = "Industry1", Group = "ID", Value = "value", opacity = 1,
width = 550, height = 100, zoom = TRUE)

如果我使用您共享的示例edge和node数据帧,绘图将不会显示...
library(tidyverse)
library(networkD3)
edge <- tribble(
~from, ~to, ~value,
0, 0, 7,
1, 0, 8,
2, 0, 6,
3, 0, 6,
4, 0, 6,
5, 1, 7,
6, 1, 6
)
node <- tribble(
~ID, ~Industry1,
0, "Footwear manufacturing",
1, "Footwear manufacturing",
2, "Footwear manufacturing",
3, "Footwear manufacturing",
4, "Footwear manufacturing",
5, "Seafood & other miscellaneous foods, n.e.c."
)
forceNetwork(Links = edge, Nodes = node, Source = "from", Target = "to",
NodeID = "Industry1", Group = "ID", Value = "value",
opacity = 1, width = 550, height = 100,
zoom = TRUE)这是因为您在edge数据帧(0-6)中引用了7个不同的节点,但在node数据帧中只有6个节点。您可以使用以下命令轻松测试这一点:
any(c(edge$from, edge$to) + 1 > nrow(node))
#[1] TRUEhttps://stackoverflow.com/questions/58533064
复制相似问题