简体   繁体   English

在R igraph中正确地着色顶点

[英]Correctly color vertices in R igraph

I am using igraph to color vertices 我正在使用igraph来着色顶点

I have two CSV files answers and topology of the graph. 我有两个CSV文件答案和图表拓扑。

Answers: (this tells that players K and N answered correctly) 答案:(这告诉玩家K和N正确回答)

  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

I wanted to build a graph using package IGraph and to color vertices in different colors depending of their correctness. 我想使用包IGraph构建一个图形,并根据它们的正确性为不同颜色的顶点着色。

This is what I was able to achieve: 这就是我能够实现的目标:

# 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: 问题是在我的输出中颜色是错误的: 在此输入图像描述

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. 这里M和K被标记为正确,而它应该是N和K.我认为问题是因为我没有指定Node应该与Player相关,我试图实现这一点,但没有成功。

Are there any ideas how to achieve this? 有什么想法如何实现这一目标?

The easiest is to create the graph with all meta data included and then igraph takes care of the rest. 最简单的方法是创建包含所有元数据的图形,然后igraph处理其余部分。 Eg 例如

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)

情节

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. 但是,实际上如果您不在数据表中的两个方向都包含每个边,那么您甚至不需要调用简化。

The problem is that the graph is sorted after simplify and the answers vector is not. 问题是图表在simplify后排序,而答案矢量则不是。 There might be an easier way, but I would simply sort your answers table: answers <-answers[order(answers[,1]),] before setting V(g)$color <- ifelse(answers[V(g), 2] == 1, "blue", "red") . 可能有一种更简单的方法,但我会简单地对你的答案表进行排序: answers <-answers[order(answers[,1]),]在设置V(g)$color <- ifelse(answers[V(g), 2] == 1, "blue", "red")

You can see that your graph is sorted with get.data.frame(g, what="vertices") 你可以看到你的图表是用get.data.frame(g, what="vertices")排序的

Alternatively, you could match the get.data.frame names (note that I create g twice. For some reason, get.data.frame doesn't play nicely with simplify . 或者,您可以match get.data.frame名称(请注意,我创建g两次。出于某种原因, get.data.frame不能很好地使用simplify

answers <- read.csv("c:/answers2.csv",header=T)
data1<-read.csv('c:/edges2.csv')
data2<-graph.data.frame(data1, directed=FALSE)
g<-simplify(data2)
ordered.vertices <-get.data.frame(g, what="vertices")
g<-simplify(data2)
V(g)$color <- ifelse(answers[match(answers[,1],ordered.vertices$name), 2] == 1, "blue", "red")
plot(g, layout=layout.fruchterman.reingold, vertex.color=V(g)$color)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM