2016-05-15 32 views
3

簡而言之,我想根據相關強度繪製邊緣,刪除不重要的值後。我能做到這一點的正相關性對與edge.betweeness,但不幸的是不是否定:igraph不適用edge.width負相關係數

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

回答

4

您可以使用有色邊緣來表示負相關和正相關,並使用edge.width來表示相關的大小。在下面的例子中,我進行了如下修改:

  1. 改變圖形對象的名稱g1,因爲是 在的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")) 

enter image description here

+0

非常感謝你。卓見。 – nouse