4
$\begingroup$

I want to create a signed graph where the edges of my graph are either green (for a positive correlation) or red (for a negative correlation). I only managed to create a graph from a thresholded adjacency matrix so far (greater than x), but struggling with this and didn't find a function in Mathematica. I'm also not sure if it's even possible :/

Is it also possible to make the edge lines thicker for a stronger correlation?

$\endgroup$
5
$\begingroup$
n = 3;
corrMat = RandomReal[{-1, 1}, {n, n}]; 
Graph[Flatten@Table[
      Style[DirectedEdge[i, j], If[corrMat[[i, j]] > 0, Green, Red], 
      Thickness[.001 Abs[corrMat[[i, j]]]]], {i, n}, {j, n}]]

Mathematica graphics

Edit

Perhaps better:

Graph[Flatten@ MapIndexed[
      Style[DirectedEdge @@ #2, If[#1 > 0, Green, Red], Thickness[.001 Abs[#1]]] &, 
      corrMat, {2}]]
$\endgroup$
1
$\begingroup$

You can also use WeightedAdjacencyGraph and set the edge styles using SetProperty:

corrMat = RandomReal[{-1, 1}, {4, 4}]; 

wag = WeightedAdjacencyGraph[corrMat];
SetProperty[wag, EdgeStyle -> {x_ :> (PropertyValue[{wag, x}, EdgeWeight] /. 
  {_?Positive -> Green, _?Negative -> Red})}]

enter image description here

Is it also possible to make the edge lines thicker for a stronger correlation?

SetProperty[wag, EdgeStyle -> {x_ :> (PropertyValue[{wag, x}, EdgeWeight] /. 
 {a_?Positive -> Directive[Thickness[Abs@a/40], Arrowheads[Max[.03, Abs@a/10]], 
    Opacity[.5], Green], 
  b_?Negative -> Directive[Thickness[Abs@b/40], Arrowheads[Max[.03, Abs@b/10]], 
    Opacity[.5], Red]})}]

enter image description here

$\endgroup$
1
$\begingroup$

With IGraph/M, you could build and style the graph like this:

vecs = RandomReal[1, {10, 20}];
wam = Outer[Correlation, vecs, vecs, 1]; (* weighted adjacency matrix *)

MatrixPlot[wam]

enter image description here

<<IGraphM`

IGWeightedAdjacencyGraph[wam, GraphLayout -> "CircularEmbedding", Background -> Black] //
  IGEdgeMap[
    Directive[
      AbsoluteThickness[5 Abs[#]],
      ColorData[{"LightTemperatureMap", {-1, 1}}][#]
    ] &,
    EdgeStyle -> IGEdgeProp[EdgeWeight]
  ]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.