R Network Package - 节点属性

时间:2014-06-10 17:50:16

标签: r networking package visualization

我最近开始学习R,对网络建模应用程序特别感兴趣。我已经制作了一个样本数据集,并希望对其进行可视化,最终进行严格的统计网络分析。

这个例子是一个高中友谊网络。节点属性在HS1_Node_Attributes.csv中找到,邻接矩阵在HS1_adjacency_matrix中找到。我能够可视化网络,虽然我遇到了节点属性(人的特征)的问题。我正在使用网络包。

我得到的错误如下:

set.vertex.attribute(g,vertex.attrnames [[i]],vertex.attr [[i]])中的错误:   set.vertex.attribute中给出的不适当的值。

我已经在线参考了一些教程,以及R Network包文档。潜在的问题可能是我的属性数据框的类型,虽然我确认它是类型列表,它检查出来。所以我不确定问题是什么。如果我不取出节点属性(vertex.attrvertex.attrnames参数),一切正常(意味着我可以成功创建网络对象),告诉我其余的代码是声音。我的代码如下。

high_school1_attributes <- read.table("HS1_Node_Attributes.csv", header = TRUE, 
                     sep = ",")
high_school1_adj <- read.table("HS1_adjacency_matrix.csv", header = TRUE, 
                         row.names = 1, sep = ",")
adj1 <- as.matrix(high_school1_adj)

library("network")

high_school1_network <- network(adj1, vertex.attr = high_school1_attributes,
                            vertex.attrnames = colnames(high_school1_attributes),
                            directed = FALSE, hyper = FALSE, loops = FALSE, 
                            multiple = FALSE, bipartite = FALSE)

1 个答案:

答案 0 :(得分:0)

您可以自动将字符串转换为因子。我可以通过执行以下操作重新创建错误:

high_school1_attributes <- read.csv(text=
        "Name,Color
        Kermit,green
        Piggy,pink
        Gonzo,blue")
high_school1_adj <- read.csv(text=
        ",From,To
        1,1,3
        2,3,2
        3,2,1",
    row.names = 1)
adj1 <- as.matrix(high_school1_adj)


library("network")

high_school1_network <- network(
        adj1,
        vertex.attr = high_school1_attributes,
        vertex.attrnames = colnames(high_school1_attributes),
        directed = FALSE, hyper = FALSE, loops = FALSE, 
        multiple = FALSE, bipartite = FALSE)

并且可以通过将第一个语句替换为:

来修复它
high_school1_attributes <- read.csv(text=
        "Name,Color
        Kermit,green
        Piggy,pink
        Gonzo,blue",
    stringsAsFactors=FALSE)

您可以通过绘图来看到作品:

library(igraph)
library(intergraph)

hs_graph <- asIgraph(high_school1_network)
plot(hs_graph, vertex.size=8,
    vertex.color=V(hs_graph)$Color,
    vertex.label=V(hs_graph)$Name,
    edge.arrow.size=0.25,layout=layout.fruchterman.reingold)