0

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

enter image description here

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.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Apr 20 '23 at 20:47
  • 1
    Data should not be shared on external sites. The link for "reproducible" example I included gives examples of how to include data in the question itself. To run this code we need not only `AI` but `links` as well. – MrFlick Apr 20 '23 at 21:18
  • Links are only AI atribuition – Ana Paula Castro Apr 20 '23 at 21:58

1 Answers1

0

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)
nmaclaren
  • 73
  • 6