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

时间:2017-05-12 17:04:40

标签: r social-networking igraph bipartite sna


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

所以,这就是我所做的:我有一个带有两列的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。

那么,数据或脚本有什么问题?

非常欢迎任何帮助或建议!

1 个答案:

答案 0 :(得分:1)

试试这个

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
)