Correctly color vertices in R igraph

后端 未结 2 1756
南旧
南旧 2020-12-03 21:16

I am using igraph to color vertices

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

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

相关标签:
2条回答
  • 2020-12-03 22:01

    The problem is that the graph is sorted after simplify and the answers vector is not. 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").

    You can see that your graph is sorted with 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.

    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)
    

    enter image description here

    0 讨论(0)
  • 2020-12-03 22:05

    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)
    

    plot

    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.

    0 讨论(0)
提交回复
热议问题