簡體   English   中英

節點的兩種模式網絡顏色不同

[英]two mode network different colors for nodes


我在可視化2模式網絡中的不同節點時遇到問題,希望有人可以幫助我找到錯誤。
我檢查了以下相關的問答,“ 如何在i中使用igraph或tnet創建雙向網絡,或 在r中為2mode網絡轉換數據形式,但是即使我按照指示進行操作,也無法解決我的問題。

所以,這就是我所做的:我有一個csv.file,其中有兩列(位置和對齊)。 看起來像這樣:

title of the file: mydata.csv
id  position               justification
[1] contra solidarity  legal regulations
[2] pro solidarity     no justification    
[3] pro solidarity     political solidarity
[4] pro solidarity     political solidarity
[5] pro solidarity     legal regulations
... (in total, 620 observations/rows of 2 variables)

我使用包進行工作,並希望為節點/頂點創建一個具有不同顏色的 R讀取文件,腳本運行正常(無錯誤):

EC_data <- read.csv("Mydata.csv", sep=";")
EC_data

library(statnet)
library(devtools)

euro_network <- network(EC_data, matrix.type="edgelist", sep=";")
euro_network

# overview about the network
summary(euro_network, print.adj=FALSE)

# for 2-mode networks
detach(package:statnet)
library(igraph)

# bipartite/2-mode network is created from edge lists
bn <- graph.data.frame(EC_data, directed=FALSE)
bn

# telling igraph that this is bipartite graph
V(bn)$type <- V(bn)$type %in% EC_data[,1]
bn
plot(bn)

# plotting the network
plot.igraph(bn,layout=layout.fruchterman.reingold)

# creating an vertex attribute to have different colors
V(bn)$type <- V(bn)$name %in% bn[,1]
bn
colors <- c("green", "red")
# green for 'contra solidarity', red for 'pro solidarity'
plot(bn, vertex.color=colors[V(bn)$type+1])

但這行不通。 所有節點具有相同的顏色,我不知道為什么。 而且,我有一種感覺,就是我從根本上錯過了一些東西或沒有充分考慮,因為我剛剛開始使用R和igraph。

那么,數據或腳本出了什么問題?

任何幫助或建議都非常歡迎!

試試這個

library(igraph)

col <- c("#a0d468", "#ffce54")
shape <- c("circle", "square")


d_edgelist <- read.csv("G:\\DATA.csv")

summary(d_edgelist)

library(igraph)
g <- graph.data.frame(d_edgelist, directed = FALSE)
g <- simplify(g)
V(g)$type <- FALSE
V(g)$type[V(g)$name %in% d_edgelist[, 1]] <- TRUE
g.proj <- bipartite.projection(g)
plot(g, layout = layout.bipartite,
     vertex.size = 40,
     vertex.shape = shape[as.numeric(V(g)$type) + 1],
     vertex.color = col[as.numeric(V(g)$type) + 1],
     edge.color = "#333333",
     edge.width = E(g)$weight * 2
)

暫無
暫無

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

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