Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

igraph does not apply edge.width for negative correlation coefficients

Tags:

r

igraph

Simply put, i want to draw edges according to correlation strength, after removing non-significant values. I can do that for positive correlation pairs with edge.betweeness, but unfortunately not for negatives:

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

So, how can i pass negative correlation values to edge.width?

like image 393
nouse Avatar asked May 15 '16 20:05

nouse


1 Answers

You could use colored edges to represent negative and positive correlations, and edge.width to represent the magnitude of the correlations. In the example below, I've made the following changes:

  1. Changed the name of the graph object to g1, because graph is a function in the igraph package.
  2. Used the absolute value for the edge deletion condition for brevity.
  3. Changed the edge.width argument to abs(E(g1)$weight)*8. The absolute value makes sure the weights are always positive. Multiplying by 8 just makes the edge widths larger.
  4. Added the edge.color argument to color vertices blue for positive correlations and red for negative correlations.

#####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

like image 129
eipi10 Avatar answered Nov 01 '22 23:11

eipi10