两种模式网络节点的不同颜色

two mode network different colors for nodes


我在可视化 2-mode 网络中的不同节点时遇到问题,希望有人能帮我找到错误。
我检查了以下相关的问答, How to create a bipartite network in R with igraph or tnet 要么 但是即使我按照说明操作,也无法解决我的问题。

所以,这就是我所做的:我有一个包含两列(位置和对齐)的 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)

我和 and the packages & and want to create an 一起工作,nodes/vertices 有不同的颜色。 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
)