Correctly color vertices in R igraph

后端 未结 2 1772
南旧
南旧 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

提交回复
热议问题