正确地绘制Riggraph中的顶点 [英] Correctly color vertices in R igraph

查看:90
本文介绍了正确地绘制Riggraph中的顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用igraph为顶点着色

I am using igraph to color vertices

我有两个CSV文件答案和图形拓扑.

I have two CSV files answers and topology of the graph.

答案:(这表明玩家K和N正确回答了

Answers: (this tells that players K and N answered correctly)

  Player Q1_I1
1      k     1
2      l     0
3      n     1
4      m     0

拓扑:(表示谁与谁连接的人)

Topology: (representation of who is connected to whom)

  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包构建图形,并根据其正确性为顶点设置不同的颜色.

I wanted to build a graph using package IGraph and to color vertices in different colors depending of their correctness.

这是我能够实现的:

# 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相关,而我试图实现这一点,但是没有成功.

Here M and K are marked as correct, whereas it should be N and K. I think that the problem is because I am not specifying that Node should be related to Player, and I tried to achieve this, but with no success.

有什么想法可以实现这一目标吗?

Are there any ideas how to achieve this?

推荐答案

最简单的方法是创建包含所有元数据的图形,然后igraph负责其余的工作.例如

The easiest is to create the graph with all meta data included and then igraph takes care of the rest. E.g.

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)

但是,实际上,如果您没有在数据表中同时包含两个方向的每个边,那么您甚至不需要调用simple.

But, actually if you don't include each edge in both directions in your data table, then you don't even need to call simplify.

这篇关于正确地绘制Riggraph中的顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆