我正在使用 igraph 给顶点着色
我有两个 CSV 文件答案和图表拓扑。
答案:(这表明玩家 K 和 N 回答正确)
Player Q1_I1
1 k 1
2 l 0
3 n 1
4 m 0
拓扑:(表示谁与谁相连)
Node.1 Node.2
1 k l
2 l k
3 l m
4 m l
5 l n
6 n l
7 n k
8 k n
我想使用 IGraph 包构建一个图,并根据顶点的正确性将其着色为不同的颜色。
这就是我能够实现的目标:
# reads answers and creates a graph from topology
answers <- read.csv("answers2.csv",header=T)
data<-read.csv('edges2.csv')
data<-graph.data.frame(data1, directed=FALSE)
g<-simplify(data)
# goes through vertices and colors them in different color, depending on correctness.
# 2 means second column (First one is the players name)
V(g)$color <- ifelse(answers[V(g), 2] == 1, "blue", "red")
plot(g, layout=layout.fruchterman.reingold, vertex.color=V(g)$color)
The problem is that in my output the colors are wrong:
这里M和K被标记为正确,而应该是N和K。
我认为问题是因为我没有指定 Node 应该与 Player 相关,并且我尝试实现这一点,但没有成功。
有什么想法如何实现这一目标吗?
最简单的方法是创建包含所有元数据的图表,然后 igraph 处理其余的事情。例如。
library(igraph)
answers <- read.table(textConnection(
" Player Q1_I1
1 k 1
2 l 0
3 n 1
4 m 0
"))
topology <- read.table(textConnection(
" Node.1 Node.2
1 k l
2 l k
3 l m
4 m l
5 l n
6 n l
7 n k
8 k n
"))
g2 <- graph.data.frame(topology, vertices=answers, directed=FALSE)
g <- simplify(g2)
V(g)$color <- ifelse(V(g)$Q1_I1 == 1, "lightblue", "orange")
plot(g)
但是,实际上,如果您不包含数据表中两个方向的每条边,那么您甚至不需要调用简化。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)