2
$\begingroup$

I have a network with 3 different edge types, edgelist file is a three column file with id1, id2 and edge label. I uploaded the file here

What I need know is to find a way to plot this network, as undirected network, where edges of different labels appear with different colors. I was not even able to find how to read edge label from file.

I would appreciate any help/suggestion.

EDIT: few lines of the edgelist file:

1   230 1
1   113 3
1   493 1
1   327 2
2   94  2
3   64  2
3   192 2
3   10024   2
3   151 2
10  260 3
10  230 1
10  10216   1
10  41  1
13  295 1
13  2392    1
$\endgroup$
  • $\begingroup$ Please post here a minimal dataset that illustrates your problem. There should be no need for anyone to download a large file (and risk malware and such). $\endgroup$ – David G. Stork Oct 30 '15 at 0:10
  • $\begingroup$ I edited the q to include the edgelist. pasting lines here messed up the format though, all columns are separated by tab. $\endgroup$ – Yasmin Oct 30 '15 at 0:16
5
$\begingroup$

You can first define a function that translates your edge label into a color. You can then use this function with Style to plot the colored graph.

d = Import["..\\sample_el.txt", "Table"]; 

EdgeStyleColor[c_] := Switch[c, 1, Red, 2, Blue, 3, Green];
Graph[Style[#1 <-> #2, EdgeStyleColor[#3]] & @@@ d]

enter image description here

$\endgroup$
  • $\begingroup$ EdgeColor is a System context symbol. It is probably not a good idea to attach definitions to it. $\endgroup$ – Szabolcs Oct 30 '15 at 19:54
  • $\begingroup$ @Szabolcs My bad, but isn't EdgeColor depreciated anyway? $\endgroup$ – paw Oct 30 '15 at 19:58
  • $\begingroup$ I don't actually know what it is used for, or even if it is a graph-related thing. But if in the System` context, better not mess with it. I don't think this is the same as Combinatorica`EdgeColor. $\endgroup$ – Szabolcs Oct 30 '15 at 20:09
3
$\begingroup$
edgetypelist = {{1, 2, Red}, 
                {1, 3, Green}, 
                {2, 3, Red}, 
                {2, 4, Blue}} 

 myedgelist = 
       Style[UndirectedEdge[#[[1]], #[[2]]], #[[3]]] & /@ edgetypelist

Graph[Range[4], myedgelist]

enter image description here

If your edge type is an integer (1,2,3), then proceed as follows:

rawedgelist = 
 Table[{RandomInteger[15], RandomInteger[15], 
   RandomInteger[{1, 3}]}, {20}]

(This is the form of your raw table.)

Next create a list of edges from the vertex in the first entry to the vertex in the second entry, colored by the edge type integer (1,2,3):

myedgelist = 
 Style[UndirectedEdge[#[[1]], #[[2]]], 
    Part[{Red, Green, Blue}, #[[3]]]] & /@ rawedgelist

Then graph the list of vertexes (rawedgelist[[All,1]]) with the colored edges:

Graph[rawedgelist[[All, 1]], myedgelist]

enter image description here

$\endgroup$
  • $\begingroup$ Thanks for the answer. But I am not sure if I understand this solution. Are you suggesting that I rewrite the edge list to include the actual color name instead of numeric edge label? $\endgroup$ – Yasmin Oct 30 '15 at 0:26
1
$\begingroup$

You can also consider HighlightGraph

data = Import["~/Downloads/sample_el.txt", "Table"];

edges = UndirectedEdge @@@ data[[All, {1, 2}]];

markers = data[[All, 3]];

HighlightGraph[
 Graph[edges, VertexSize -> Scaled[0.003], VertexStyle -> Black],
 Style[Pick[edges, markers, #], ColorData[81][#]] & /@ {1, 2, 3}
]

enter image description here

Alternatively:

HighlightGraph[
 Graph[edges, VertexSize -> Scaled[0.003], VertexStyle -> Black],
 MapThread[Style[Pick[edges, markers, #1], #2] &, {{1, 2, 3}, {Red, Green, Blue}}]
]
$\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.