My problem is that I want to assign values of correlations from a matrix to the thickness of areas in the Igraph package using R.
Here is my data
require(igraph)
links = (AI)# AI in a correlation matrix
links= as.matrix(AI)#coersÃo do data sete em uma matrix
matrixnetwork = graph.adjacency(links, mode="undirected", weighted = TRUE, add.colnames=NULL, diag=FALSE)
plot(matrixnetwork) #here I change to network for igraph
I try this form
edge.width=strength(matrixnetwork) *2.3 #Set node thickness using strength and match within the graph
edge.width=E(matrixnetwork)$weight*2.3#I set the thickness of the node using the graph's weight
I have this graph
In this case, the edges received either the strength or the weight, but I want them to have thickness corresponding to the weight of the correlations that I have in the "AI" variable, or the "Links" variable.
It doesn't look like the information is getting to the right places in the igraph representation.
It's easier to assume all edge weights are positive, so starting there, some fake data could look like this:
library(igraph)
set.seed(62)
N <- 7
fakecors <- runif(choose(N, 2))
cormat <- matrix(nrow = N, ncol = N)
cormat[lower.tri(cormat)] <- fakecors
diag(cormat) <- 0
cormat[upper.tri(cormat)] <- t(cormat)[upper.tri(t(cormat))]
g <- graph_from_adjacency_matrix(cormat, mode = "undirected", weighted = TRUE)
Then, you can assign the appropriate values to node and edge attributes like this:
V(g)$size <- 10*strength(g)
E(g)$width <- 3*E(g)$weight
plot(g, layout = layout_in_circle)
I don't think igraph likes negative edge weights or node sizes, so if you have negative correlations you may want to map the sign to a different feature, like color:
library(igraph)
set.seed(220)
N <- 7
fakecors <- runif(choose(N, 2), -1, 1)
cormat <- matrix(nrow = N, ncol = N)
cormat[lower.tri(cormat)] <- fakecors
diag(cormat) <- 0
cormat[upper.tri(cormat)] <- t(cormat)[upper.tri(t(cormat))]
g <- graph_from_adjacency_matrix(cormat, mode = "undirected", weighted = TRUE)
s <- strength(g)
w <- E(g)$weight
V(g)$size <- 10*abs(s)
V(g)$color <- ifelse(s > 0, "lightblue", "lightcoral")
E(g)$width <- 3*abs(w)
E(g)$color <- ifelse(w > 0, "blue", "red")
plot(g, layout = layout_in_circle)
EDIT
I think this will do, based on your comment below:
library(igraph)
set.seed(62)
N <- 7
fakecors <- runif(choose(N, 2))
cormat <- matrix(nrow = N, ncol = N)
cormat[lower.tri(cormat)] <- fakecors
diag(cormat) <- 0
cormat[upper.tri(cormat)] <- t(cormat)[upper.tri(t(cormat))]
matrixnetwork <- graph_from_adjacency_matrix(cormat, mode = "undirected", weighted = TRUE)
## Here is example, I need to change the width of the edges, to correspond to the weight of the corrections: Being width 1, correlation >7, width 2, correlation >8, width 3, correlation greater than 9
set_thickness <- function(weight) {
if(weight < 0.7) {
0
} else if(weight < 0.8) {
1
} else if(weight < 0.9) {
2
} else if(weight <= 1) {
3
}
}
E(matrixnetwork)$width <- sapply(E(matrixnetwork)$weight, set_thickness)
plot(matrixnetwork, layout = layout_in_circle)