igraph不对edge.width应用负相关系数

时间:2016-05-15 20:19:06

标签: r igraph

简单地说,我想在删除非重要值后根据相关强度绘制边。对于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 个答案:

答案 0 :(得分:5)

您可以使用彩色边缘来表示负相关和正相关,使用edge.width来表示相关性的大小。在下面的示例中,我进行了以下更改:

  1. 将图表对象的名称更改为g1,因为graph是 igraph包中的一个功能。
  2. 为简洁起见,使用边缘删除条件的绝对值。
  3. edge.width参数更改为abs(E(g1)$weight)*8。该 绝对值确保权重始终为正。 乘以8只会使边缘宽度变大。
  4. 为颜色顶点蓝色添加了edge.color参数 正相关和红色表示负相关。
  5. #####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"))
    

    enter image description here