【问题标题】:How to spread out community graph made by using igraph package in R如何传播在R中使用igraph包制作的社区图
【发布时间】:2015-04-27 06:04:21
【问题描述】:

试图在推文数据中找到社区。不同单词之间的余弦相似度形成邻接矩阵。然后,我根据该邻接矩阵创建了图形。图表的可视化是这里的任务:

# Document Term Matrix
dtm = DocumentTermMatrix(tweets)

### adjust threshold here
dtms = removeSparseTerms(dtm, 0.998)
dim(dtms)

# cosine similarity matrix
t = as.matrix(dtms)

# comparing two word feature vectors
#cosine(t[,"yesterday"], t[,"yet"]) 

numWords = dim(t)[2]

# cosine measure between all column vectors of a matrix.
adjMat = cosine(t)

r = 3
for(i in 1:numWords)
{
  highElement  = sort(adjMat[i,], partial=numWords-r)[numWords-r]
  adjMat[i,][adjMat[i,] <  highElement] = 0
}

# build graph from the adjacency matrix
g = graph.adjacency(adjMat, weighted=TRUE, mode="undirected", diag=FALSE)
V(g)$name

# remove loop and multiple edges
g = simplify(g)
wt = walktrap.community(g, steps=5) # default steps=2
    table(membership(wt))

# set vertex color & size
nodecolor = rainbow(length(table(membership(wt))))[as.vector(membership(wt))]
nodesize = as.matrix(round((log2(10*membership(wt)))))
nodelayout = layout.fruchterman.reingold(g,niter=1000,area=vcount(g)^1.1,repulserad=vcount(g)^10.0, weights=NULL)

par(mai=c(0,0,1,0)) 
plot(g, 
     layout=nodelayout,
     vertex.size = nodesize,
     vertex.label=NA,
     vertex.color = nodecolor,
     edge.arrow.size=0.2,
     edge.color="grey",
     edge.width=1)

我只是想在不同的集群/社区之间留出更多的差距。

【问题讨论】:

  • 请介绍g或者它的例子
  • 您是否尝试过更改绘图区域?默认为area = vcount(graph)^2 (inside-r.org/packages/cran/igraph/docs/layout)
  • 刚刚更新了最新的代码和图表。
  • 我不喜欢 fruchtermal.reingold 算法,因为这种事情一直在发生,而且我通常不知道如何解决。我通常做的是:我将我的图表导出到 Gephi,使用 Force Atlas 2 算法(检查选项“防止重叠”和“劝阻中心”),这样我通常可以很好地可视化社区结构。我希望有人能在这里告诉你解决这个问题的最佳方法,我也会学习它。
  • @jonathancardoso - 如何将图形从 R 导出到 Gephi。 Gephi 看起来很有趣!

标签: r cluster-analysis igraph graph-visualization


【解决方案1】:

据我所知,您不能仅使用 igraph 将同一社区的顶点布置得彼此靠近。我已经在我的包NetPathMiner 中实现了这个功能。只是为了可视化功能安装包似乎有点困难。我将在这里写一个简单的版本并解释它的作用。

layout.by.attr <- function(graph, wc, cluster.strength=1,layout=layout.auto) {  
        g <- graph.edgelist(get.edgelist(graph)) # create a lightweight copy of graph w/o the attributes.
        E(g)$weight <- 1

        attr <- cbind(id=1:vcount(g), val=wc)
        g <- g + vertices(unique(attr[,2])) + igraph::edges(unlist(t(attr)), weight=cluster.strength)

        l <- layout(g, weights=E(g)$weight)[1:vcount(graph),]
        return(l)
}

基本上,该函数添加了一个额外的顶点,该顶点连接到属于同一社区的所有顶点。布局是根据新图形计算的。由于每个社区现在都由一个公共顶点连接,因此它们倾向于聚集在一起。

正如 Gabor 在评论中所说,增加边权重也会产生类似的效果。该函数利用此信息,通过增加cluster.strength,为创建的顶点及其社区之间的边赋予更高的权重。

如果这还不够,您可以通过在相同社区的所有顶点之间添加边(形成一个派系)来扩展此原理(在更连通的图上计算布局)。根据我的经验,这有点矫枉过正。

【讨论】:

  • 嘿,我收到一个错误“布局错误(g, weights = E(g)$weight)[1:(vcount(graph)), ] : subscript out of bounds”有没有人遇到过这种情况吗?
  • 另外,这行代码是“g
  • 我怀疑您的图有断开的节点(与任何其他节点没有边),在这种情况下,它们不会出现在新图中(仅从边创建)。我承认,我没有考虑到这种极端情况。在您的情况下,您可以尝试只复制您的图表g &lt;- graph 吗?
猜你喜欢
  • 2018-12-18
  • 1970-01-01
  • 1970-01-01
  • 2015-01-10
  • 1970-01-01
  • 2014-03-25
  • 1970-01-01
  • 2021-09-03
  • 2014-08-22
相关资源
最近更新 更多