如何使用R中的igraph将边缘属性编码为顶点属性

问题描述:

我在绘制网络图并尝试使用非重叠属性对顶点着色。我希望我的网络图根据不同的属性进行着色。在这个例子中,如果ID 2的前三个字母等于U 50或U 51,我希望它显示为红色。我有5个属性我希望这个图表编码,任何不属于其中一个类别的观察值都应该用默认颜色编码。通过这种方式,我将能够看到这些属性的强度,并更好地将其传达给其他人。到目前为止,我一直无法使用各种不同的编码方法使代码工作。首先,我试图创建一个新的变量,在将每个观察值转换为一个i图形对象之前为其分配正确的属性。如何使用R中的igraph将边缘属性编码为顶点属性

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"` 

然后我指定了颜色我想分配给每个属性。我使用get vertex属性代码并填充适当的颜色。

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] 

我尝试这个相同的方法仅使用:
vertex_colors < -vertex_colors + 1

然后绘制,我改变边缘的颜色为黑色,指定我的布局,并改变我边的尺寸和顶点。

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) 

使用此方法,顶点上不会显示颜色,甚至不会显示默认颜色。使用不同的方法设置顶点属性,我会做得更好一些。默认颜色显示,但我想要的颜色不。

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") 

我认为这个问题可能是我使用多个(非重叠)边缘属性试图代码顶点颜色。但我不知道如何将边缘属性转换为顶点属性。我也不知道我的代码是否还有其他一些未知的问题。

下面是我的数据链接复制下面以及一个链接到我的完整的代码文件,其中有一个或两个其他方法,我试图用来解决这个问题。任何帮助将非常感激! Data

这里是我的代码,这也是上述的R档:R-file

我认为你是搞乱你vertex_color载体,看看它与头()。

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

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

您首先分配一个字符串,然后与数字进行比较,所以它们之间不应该为真。

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

这包含一个拼写错误,因为“vertex_colors”不是颜色名称,所以返回一个错误。

最后但并非最不重要的,确实在一个丰富多彩的情节

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

结果呢?如果是的话,vertex_colors矢量是你的问题,如果不是别的东西。