R中的强制网络图

Forced network diagram in R

我正在使用 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.

我目前的输出是完全空白的,并且已经尝试过数据结构和变量赋值。如有任何帮助,我们将不胜感激!

提前致谢。

如果我使用你的数据框的较大样本,该图对我有用...

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)

如果我使用您共享的示例 edgenode 数据帧,则绘图不会显示...

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] TRUE