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
?
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:
g1
, because graph
is
a function in the igraph package.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.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"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With