简体   繁体   中英

How to code edge attributes as vertex attributes using igraph in R

I am graphing a network and trying to color the vertices using non-overlapping attributes. I want my network diagram to be colored according to different attributes. In this example, if the first three letters of ID 2 are equal to U 50 or U 51, I want this to show up as red. I have 5 attributes I want this graph coded by and any observations that don't fall into one of the categories should be coded in a default color. In this way I will be able to see the intensity of these attributes and better communicate this to other people. So far, I have been unable to get the code to work using a variety of different coding methods. First I tried to create a new variable that assigned the correct attribute to each observation before converting it into an i graph object.

    anon.nd$vertexcolor[substr(anon.nd$ID2,1,3)=="U50" | substr(anon.nd$ID2,1,3)=="U51"]<-"O"
    anon.nd$vertexcolor[substr(anon.nd$ID2,1,3)=="U54" | substr(anon.nd$ID2,1,3)=="U55"]<-"P"
    anon.nd$vertexcolor[anon.nd$INT.type=="K1"]<-"INT.NB"
    anon.nd$vertexcolor[anon.nd$Country=="L12"]<-"UK"
    anon.nd$vertexcolor[anon.nd$ID2=="U769"]<-"OBL"`

I then specified the colors I wanted to assign to each each attribute. I used the get vertex attribute code and filled in the appropriate colors.

    anon.nd1<-graph.data.frame(anon.nd)
    vertex_colors=get.vertex.attribute(anon.nd1,"vertexcolor")
    colors=c('azure3', 'firebrick1', 'orange1', 'darkblue', 'darkolivegreen', 'gold')
    vertex_colors[vertex_colors==0]=colors[1]
    vertex_colors[vertex_colors==1]=colors[2]
    vertex_colors[vertex_colors==2]=colors[3]
    vertex_colors[vertex_colors==3]=colors[4]
    vertex_colors[vertex_colors==4]=colors[5]
    vertex_colors[vertex_colors==5]=colors[6]

I tried this same method using just:
vertex_colors<-vertex_colors+1

Then to plot, I changed my edge color to black, specified my layout, and change the size of my edges and vertices.

    E(anon.nd1)$color="black"
    nd.layout<-layout.fruchterman.reingold(anon.nd1)
    plot(anon.nd1, layout=nd.layout, vertex.color=vertex_colors, vertex.size=2, edge.arrow.size=.01, vertex.label=NA)

Using this method, no color shows up on the vertices, not even the default color. Using a different method where I set the vertex attribute, I do a little better. The default color shows up, but the colors I want do not.

    anon.nd2<-graph.data.frame(anon.nd)
    V(anon.nd2)$colors<-"azure3"
    V(anon.nd2)$colors[substr(anon.nd2$ID2,1,3)=="U50" | substr(anon.nd2$ID2,1,3)=="U51"]<-"firebrick1"
    V(anon.nd2)$colors[substr(anon.nd2$ID2,1,3)=="U54" | substr(anon.nd2$ID2,1,3)=="U55"]<-"orange1"
    V(anon.nd2)$colors[anon.nd2$Country=="L12"]<-"darkblue"
    V(anon.nd2)$colors[anon.nd2$INT.type=="K1"]<-"darkolivegreen"
    V(anon.nd2)$colors[anon.nd2$ID2=="U769"]<-"gold"
    E(anon.nd2)$color<-"black"
    nd.layout<-layout.fruchterman.reingold(anon.nd2)
    windows(width=20, height=16)
    plot(anon.nd2, layout=nd.layout, vertex.size=2, edge.arrow.size=.01, vertex.label=NA, vertex.color="vertex_colors")

I think the problem might be that I am trying to code vertex color using multiple (non-overlapping) edge attributes. But I don't know how to convert and edge attribute into a vertex attribute. I also don't know if there is some other, unidentified problem with my code.

Here is the link to my data is copied below as well as a link to my full code file which has one or two other methods I tried using to solve this problem. Any help would be much appreciated! Data

And here is an R file with my code, which is also above: R-file

I think you are messing up your vertex_color vector, have a look at it with head().

anon.nd$vertexcolor[anon.nd$INT.type=="K1"]<-"INT.NB"

vertex_colors[vertex_colors==0]=colors[1]

You first assign a string and then compare with numbers, so non of them should be true.

plot(anon.nd2, layout=nd.layout, vertex.size=2, edge.arrow.size=.01, vertex.label=NA, vertex.color="vertex_colors")

This contains a typo and returns an error for me since "vertex_colors" isn't a colour name.

Last but not least, does

 plot(anon.nd2, vertex.color=colors)
 or
 plot(anon.nd2, vertex.color=1:8)

result in a colourful plots? If yes, the vertex_colors vector is your problem, if not something else is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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