[英]R: How to Efficiently Visualize a Large Graph Network
我在 R 中模擬了一些圖網絡數據(~10,000 個觀察值),並嘗試使用 R 中的 visNetwork 庫將其可視化。 然而,數據非常混亂,很難直觀地分析(我理解在現實生活中,網絡數據是旨在使用圖查詢語言進行分析)。
就目前而言,我可以做些什么來改進我創建的圖網絡的可視化(這樣我就可以探索一些相互疊加的鏈接和節點)?
可以使用諸如“networkD3”和“diagrammeR”之類的庫來更好地可視化此網絡嗎?
我在下面附上了我的可重現代碼:
library(igraph)
library(dplyr)
library(visNetwork)
#create file from which to sample from
x5 <- sample(1:10000, 10000, replace=T)
#convert to data frame
x5 = as.data.frame(x5)
#create first file (take a random sample from the created file)
a = sample_n(x5, 9000)
#create second file (take a random sample from the created file)
b = sample_n(x5, 9000)
#combine
c = cbind(a,b)
#create dataframe
c = data.frame(c)
#rename column names
colnames(c) <- c("a","b")
graph <- graph.data.frame(c, directed=F)
graph <- simplify(graph)
graph
plot(graph)
library(visNetwork)
nodes <- data.frame(id = V(graph)$name, title = V(graph)$name)
nodes <- nodes[order(nodes$id, decreasing = F),]
edges <- get.data.frame(graph, what="edges")[1:2]
visNetwork(nodes, edges) %>% visIgraphLayout(layout = "layout_with_fr") %>%
visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE) %>%
visInteraction(navigationButtons = TRUE)
謝謝
應 OP 的要求,我正在應用上一個答案中使用的方法將網絡划分為社區的結果可視化解決此問題。
問題中的網絡不是使用指定的隨機種子創建的。 在這里,我指定了可重復性的種子。
## reproducible version of OP's network
library(igraph)
library(dplyr)
set.seed(1234)
#create file from which to sample from
x5 <- sample(1:10000, 10000, replace=T)
#convert to data frame
x5 = as.data.frame(x5)
#create first file (take a random sample from the created file)
a = sample_n(x5, 9000)
#create second file (take a random sample from the created file)
b = sample_n(x5, 9000)
#combine
c = cbind(a,b)
#create dataframe
c = data.frame(c)
#rename column names
colnames(c) <- c("a","b")
graph <- graph.data.frame(c, directed=F)
graph <- simplify(graph)
正如 OP 所指出的,一個簡單的情節是一團糟。 引用的先前答案將其分為兩部分:
1.小組件不同的組件有不同的顏色來幫助區分它們。
## Visualize the small components separately
SmallV = which(components(graph)$membership != 1)
SmallComp = induced_subgraph(graph, SmallV)
LO_SC = layout_components(SmallComp, layout=layout_with_graphopt)
plot(SmallComp, layout=LO_SC, vertex.size=9, vertex.label.cex=0.8,
vertex.color=rainbow(18, alpha=0.6)[components(graph)$membership[SmallV]])
可以用這個做更多的事情,但這相當容易,而不是問題的實質,所以我將把它作為小組件的表示。
2. 巨型組件
簡單地繪制巨型組件仍然難以閱讀。 這里有兩種改進顯示的方法。 兩者都依賴於對頂點進行分組。 對於這個答案,我將使用 cluster_louvain 對節點進行分組,但您可以嘗試其他社區檢測方法。 cluster_louvain 產生 47 個社區。
## Now try for the giant component
GiantV = which(components(graph)$membership == 1)
GiantComp = induced_subgraph(graph, GiantV)
GC_CL = cluster_louvain(GiantComp)
max(GC_CL$membership)
[1] 47
巨方法1——分組頂點
創建一個強調社區的布局
GC_Grouped = GiantComp
E(GC_Grouped)$weight = 1
for(i in unique(membership(GC_CL))) {
GroupV = which(membership(GC_CL) == i)
GC_Grouped = add_edges(GC_Grouped, combn(GroupV, 2), attr=list(weight=6))
}
set.seed(1234)
LO = layout_with_fr(GC_Grouped)
colors <- rainbow(max(membership(GC_CL)))
par(mar=c(0,0,0,0))
plot(GC_CL, GiantComp, layout=LO,
vertex.size = 5,
vertex.color=colors[membership(GC_CL)],
vertex.label = NA, edge.width = 1)
這提供了一些見解,但許多邊緣使它有點難以閱讀。
巨人法2——簽約社區
將每個社區繪制為單個頂點。 頂點的大小反映了該社區中節點的數量。 顏色代表社區節點的程度。
## Contract the communities in the giant component
CL.Comm = simplify(contract(GiantComp, membership(GC_CL)))
D = unname(degree(CL.Comm))
set.seed(1234)
par(mar=c(0,0,0,0))
plot(CL.Comm, vertex.size=sqrt(sizes(GC_CL)),
vertex.label=1:max(membership(GC_CL)), vertex.cex = 0.8,
vertex.color=round((D-29)/4)+1)
這更干凈,但失去了社區的任何內部結構。
只是“現實生活”的一個提示。 處理大圖的最佳方法是 1) 通過某種度量過濾您正在使用的邊,或 2) 使用一些相關變量作為權重。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.