0
$\begingroup$

I'm looking to make a network visualization of a list of data values. The list is formatted as so: $DataList = ((a_1, b_1, c_1, d_1,), (a_2, b_2, c_3, d_3), (a_3, b_3, c_3, d_3),...(a_n, b_n, c_n, d_n))$. Where $a, b, c,$ and $d$ are all integers. Furthermore, I have a list of pairs that denotes names to replace $a$ and $c$ values.

For clarification, $a$ and $c$ are indexes and I have an "index list" (called IndexList) with the corresponding labels: $((1, label_1), (2, label_2)...(n, label_n)$.

So, what I'm trying to accomplish is making a network where if $b_n > d_n$, then connect a node with label $c_n$ (aka, IndexList[[$c_n$, 2]]) to a node with label $a_n$ (aka IndexList[[$a_n$, 2]]). And if $d_n > b_n$, then connect a node with label $a_n$ to a node with label $c_n$.

In other words, $a$ and $b$ are related values and $c$ and $d$ are related values. The DataList is basically setup as $(Index_1, Value_1, Index_2, Value_2)$ and we're connecting the lower value to the greater value.

I don't have any experience using the network graphs and all the wolfram documentation seems to show is a bunch of pictures rather than a walkthrough on how to do it. Thank you for any and all help.

$\endgroup$
3
  • 3
    $\begingroup$ The chances for getting help here are much higher if you present example data and as much information as possible as copyable code. This will help other user to grasp what the essentials of for problem are and it will motivate them to play around with it. $\endgroup$ Apr 7, 2018 at 8:21
  • $\begingroup$ "all the wolfram documentation seems to show is a bunch of pictures rather than a walkthrough on how to do it" A walkthrough of how to do what specifically? I assume you didn't expect to have a walkthrough for this very specific problem. The documentation doesn't have "pictures". It has examples: input code, the resulting output, and a description of what the example is about. You'll find examples for most basic tasks. $\endgroup$
    – Szabolcs
    Apr 7, 2018 at 12:25
  • 1
    $\begingroup$ To generate a graph, you would usually generate a list of edges, then use Graph. Something like Map[indexList[[#]]&, If[#2>#4, #1 <-> #3, #1 <-> #2]& @@@ dataList, {2}]. $\endgroup$
    – Szabolcs
    Apr 7, 2018 at 12:28

1 Answer 1

3
$\begingroup$

Pretty straightforward to make a graph if the data is provided. (As Szabolcs mentioned in a comment.)

Data generation

In order to provide an answer we are going to generate data first.

SeedRandom[153]

Clear[MakeRMat]
MakeRMat[n_Integer] :=
  Block[{rmat, 
    rinds = RandomSample[Flatten[Outer[List, Range[n], Range[n]], 1], 3 n]},
    rmat = RandomReal[{0, 1}, {n, n}];
    Do[rmat[[Sequence @@ r]] = None, {r, rinds}];
    rmat
  ];

n = 6;
rmat1 = MakeRMat[n]; rmat2 = MakeRMat[n];
MatrixPlot /@ {rmat1, rmat2}

enter image description here

arRules = 
  Join[Most[ArrayRules[SparseArray[rmat1]]], Most[ArrayRules[SparseArray[rmat2]]]];

data = Riffle[#[[1]], #[[2]]] & /@ 
    Normal[GroupBy[arRules, First, #[[All, 2]] &]] /. None -> 0;
data = DeleteCases[data, {_, 0, _, 0}];

Here is a mapping of indices to labels:

aDict = 
 Thread[Range[Length[rmat1]] -> 
   RandomSample[DictionaryLookup["*"], Length[rmat1]]]

(* {1 -> "Issac", 2 -> "stratagem", 3 -> "joy", 4 -> "thew", 
    5 -> "yellow", 6 -> "stretches"} *)

This is how the generated data looks like:

ds = Dataset[Dataset[data][All, AssociationThread[{"a", "b", "c", "d"}, #] &]];
Row[{ds, ds /. aDict}]

enter image description here

Graph generation

grRules = 
  Map[If[#[[2]] > #[[4]], #[[1]] \[DirectedEdge] #[[3]], #[[3]] \[DirectedEdge] #[[1]]] /. aDict &, data];
Graph[grRules, VertexLabels -> "Name"]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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