# network visualization with different colors for different edge labeled

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

• 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). – David G. Stork Oct 30 '15 at 0:10
• I edited the q to include the edgelist. pasting lines here messed up the format though, all columns are separated by tab. – Yasmin Oct 30 '15 at 0:16

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]


• EdgeColor is a System context symbol. It is probably not a good idea to attach definitions to it. – Szabolcs Oct 30 '15 at 19:54
• @Szabolcs My bad, but isn't EdgeColor depreciated anyway? – paw Oct 30 '15 at 19:58
• 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 CombinatoricaEdgeColor. – Szabolcs Oct 30 '15 at 20:09
edgetypelist = {{1, 2, Red},
{1, 3, Green},
{2, 3, Red},
{2, 4, Blue}}

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

Graph[Range[4], myedgelist]


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]


• 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? – Yasmin Oct 30 '15 at 0:26

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}
]


Alternatively:

HighlightGraph[
Graph[edges, VertexSize -> Scaled[0.003], VertexStyle -> Black],
MapThread[Style[Pick[edges, markers, #1], #2] &, {{1, 2, 3}, {Red, Green, Blue}}]
]