【问题标题】:How to code edge attributes as vertex attributes using igraph in R如何在 R 中使用 igraph 将边缘属性编码为顶点属性
【发布时间】:2015-04-01 20:54:03
【问题描述】:

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

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

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

    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]

我尝试了同样的方法,只使用了:
顶点颜色

然后进行绘图,我将边缘颜色更改为黑色,指定布局,并更改边缘和顶点的大小。

    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

【问题讨论】:

    标签: r


    【解决方案1】:

    我认为你弄乱了你的 vertex_color 向量,用 head() 看看它。

    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 向量是您的问题,如果不是其他问题。

    【讨论】:

      猜你喜欢
      • 2015-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      • 2014-04-13
      相关资源
      最近更新 更多