簡體   English   中英

R中的強制網絡圖

[英]Forced network diagram in R

我正在使用 networkD3 R package 構建強制網絡圖,並且在讓網絡顯示時遇到問題。 我可以使用“simpleNetwork”function 就好了,但不能使用“forced_network_df”function。

代碼如下:

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)

原始 Dataframe 樣本(稱為強制網絡 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.

我目前的 output 完全是空白的,並且玩弄了數據結構和變量分配。 這里的任何幫助將不勝感激!

提前致謝。

如果我使用您的 dataframe 的較大樣本,則 plot 適合我...

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數據框,則 plot 不會顯示...

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 dataframe (0-6) 中引用了 7 個不同的節點,但您的node dataframe 中只有 6 個節點。 您可以使用...輕松測試它

any(c(edge$from, edge$to) + 1 > nrow(node))
#[1] TRUE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM