igraph 不适用于负相关系数的边缘宽度。

7
简单来说,我想根据相关性强度绘制边缘,删除非显著值。我可以使用edge.betweeness为正相关对绘制边缘,但不幸的是,无法为负相关对执行此操作:
data <- matrix(rnorm(100),10,10)    
colnames(data) <- LETTERS[1:10]

library(Hmisc)
cor1 <- rcorr(data)
diag(cor1$r) <- 0

library(igraph)

#####Example 1:
graph <- graph.adjacency(cor1$r, weighted=TRUE, mode="lower")
graph <- delete.edges(graph, E(graph)[ weight < 0.3 & weight > -0.3])
#####trying to pass edge weights to edge.width
plot.igraph(graph, vertex.size=20, edge.width=E(graph)$weight)
###edge.width=E(graph)$weight is ignored

#####Example 2:
graph <- graph.adjacency(cor1$r, weighted=TRUE, mode="lower")
graph <- delete.edges(graph, E(graph)[ weight < 0.3]) #omitting the 2nd condition
E(graph)$weight <- edge.betweenness(graph) #apparently required
plot.igraph(graph, vertex.size=20, edge.width=E(graph)$weight)
####this does work, but only for positive correlation coefficients 

#####Example 3:
graph <- graph.adjacency(cor1$r, weighted=TRUE, mode="lower")
graph <- delete.edges(graph, E(graph)[ weight < 0.3 & weight > -0.3])
E(graph)$weight <- edge.betweenness(graph)
#####gives error: Error in .Call("R_igraph_edge_betweenness", graph, directed, weights,  : 
#################At centrality.c:2046 : Weight vector must be non-negative, Invalid value

那么,如何将负相关值传递给 edge.width
1个回答

10
你可以使用有色边来表示正负相关,使用 edge.width 表示相关性的程度。在下面的例子中,我做了以下更改:
  1. 将图形对象的名称更改为g1,因为graph是igraph软件包中的函数。
  2. 为简洁起见,使用了绝对值作为边缘删除条件。
  3. edge.width参数更改为abs(E(g1)$weight)*8。绝对值确保权重始终为正值。 乘以8只是使边缘宽度更大。
  4. 添加了edge.color参数,用于为正相关着色为蓝色,负相关着色为红色。
#####Example 3:
g1 <- graph.adjacency(cor1$r, weighted=TRUE, mode="lower")
g1 <- delete.edges(g1, E(g1)[ abs(weight) < 0.3 ])
plot.igraph(g1, vertex.size=20, edge.width=abs(E(g1)$weight)*8, 
        edge.color=ifelse(cor1$r > 0, "blue","red"))

输入图片说明


非常感谢。太棒了的想法。 - nouse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接