5
$\begingroup$

I am trying to figure out a way to include edgeweights in the visualisation of a graph in Mathematica, to find an idea for the drawing such that even for relatively large node numbers the graphs remain visually clear. But the basic built-in feature leads to rather messy layouts as soon as there are large number of nodes/edges. Here's an example below:

SeedRandom[100]
n = 500;
m = 1000;
edgeweights = 1./RandomReal[{0.1, 1}, m];
G = RandomGraph[{n, m}, EdgeWeight -> edgeweights]

Produces: enter image description here

Including GraphLayout -> {"SpringElectricalEmbedding", "EdgeWeighted" -> True} into the definition of G produces:

enter image description here

It seems to simply draw the nodes whose connecting edge weight is larger closer to one another, which leads to a very dense embedded layout.

Would it be possible to:

  • Modulate the edge thickness and color [*] according to their weights? The weights do not necessarily have to be given in the graph definition (G), they could also simply be called for the purpose of the visualisation.

[*]: That is, the greater the weight, the thicker and the more brightly colored the edge. For normalization, we can use the maximal weight in the vector of edgeweights.

$\endgroup$

3 Answers 3

4
$\begingroup$

I do not think that any good way exists. Once a graph is large enough, it will always look like a hairball unless it has a clear structure that might be made visible. For example, this is a similarity graph of musicians. The musicians cluster into groups, and it is possible to make this structure visible. Your example graph, on the other hand, is completely random, with random edge weights. Since there are lots of nodes and edges, but no real information is contained within them, I do not think that it can be visualized in a meaningful way.

Assuming that there is something to show, things you can try are:

  • Take edge weights into consideration when computing the layout. Look up individual graph layouts on the GraphLayout doc page, and see if they support weights. You have already found GraphLayout -> {"SpringElectricalEmbedding", "EdgeWeighted" -> True}, but it's still useful to mention this for other readers.

    The example I linked above was created by one of the authors of the igraph library. IGraph/M is a Mathematica interface to igraph (and much more), and exposes multiple layout algorithms that support weights. The above example was created using the DrL layout (IGLayoutDrL function in IGraph/M)

  • Visualize weights as not edge lengths, but edge weights or edge colours. You can do this with EdgeStyle. IGraph/M provides a very convenient way to do it:

    SeedRandom[137]
    g = RandomGraph[{10, 20}, EdgeWeight -> RandomReal[{.1, 1}, 20]]
    
    Graph[g, EdgeStyle -> Directive[CapForm["Round"], Opacity[1/3]]] //
     IGEdgeMap[AbsoluteThickness[10 #] &, EdgeStyle -> IGEdgeProp[EdgeWeight]]
    

    enter image description here

  • Use colours in the same way.

    Graph[g, EdgeStyle -> Directive[CapForm["Round"], AbsoluteThickness[4]]] //
     IGEdgeMap[ColorData["RustTones"], EdgeStyle -> Rescale@*IGEdgeProp[EdgeWeight]]
    

    enter image description here

  • Use all of the above: edge length, edge thickness and edge colour.

    IGLayoutFruchtermanReingold[g, EdgeStyle -> Directive[CapForm["Round"], Opacity[1/2]]] // 
     IGEdgeMap[
      Directive[ColorData["RustTones"][#], AbsoluteThickness[10 #]] &, 
      EdgeStyle -> (#/Max[#] &)@*IGEdgeProp[EdgeWeight]]
    

    enter image description here

  • Cluster the graph vertices before visualizing them. The clustering can take weights into account.

    CommunityGraphPlot[g]
    

    enter image description here

    This related to what I said above. First, try to identify the structure, then explicitly make it visible.

$\endgroup$
4
$\begingroup$
edgeStyle[weights_, thickbounds_:{0.0001,0.01}, colorf_:ColorData["SolarColors"]]:=
    Block[{minmax, thickness, color},
        minmax = MinMax[weights];
        thickness = Thickness /@ Rescale[weights, minmax, thickbounds];
        color = colorf /@ Rescale[weights, minmax, {0, 1}];
        Thread[Directive[Opacity[.7], CapForm["Round"], thickness, color]]
    ]

Here's the example:

Graph[G, EdgeStyle -> Thread[EdgeList[G] -> edgeStyle[edgeweights]], 
 VertexSize -> 1, VertexStyle -> Blue]

enter image description here

With different thickness and color:

Graph[G, EdgeStyle -> 
  Thread[EdgeList[G] -> 
    edgeStyle[edgeweights, {0.0001, 0.02}, 
     ColorData["BrightBands"]]], VertexSize -> 1, VertexStyle -> Blue]

enter image description here

$\endgroup$
3
$\begingroup$

When you have a lot of things to display in a small space you get a mess no matter what. But you can always try to make it better. I suggest 2 steps:

  1. Untangle a bit the mess with GraphLayout
  2. Avoid noise in style logic

1. Untangle a bit the mess with GraphLayout

I would use a proper GraphLayout for a specific cases. For instance, a general messy graph can benefit from "GravityEmbedding" which will be available in V12 (compare left and right images):

RandomGraph[{100,100},ImageSize->400,GraphLayout->#]&/@
{Automatic,"GravityEmbedding"}

enter image description here

But on the other hand in case of trees you are better of with "RadialEmbedding"

Graph[RandomInteger[#]<->#+1&/@Range[0,500],ImageSize->400,GraphLayout->#]&/@
{Automatic,"RadialEmbedding"}

enter image description here

And so on depending on your specific graph structure.

2. Avoid noise in style logic

I recommend to read an article I wrote (even so your graphs are larger a lot of logic still holds):

On design of styles for small weighted graphs: https://community.wolfram.com/groups/-/m/t/838652

enter image description here

$\endgroup$
4
  • $\begingroup$ So what is this "GravityEmbedding" anyway? It's the first new piece of functionality added in a long time. Is it explained what it does? If not, why do you think it's more appropriate? And why does a layout change the appearance of edges? That is counterproductive and people will end up having to keep switching back to the default (I bet most won't even know how). $\endgroup$
    – Szabolcs
    Commented Apr 9, 2019 at 9:09
  • $\begingroup$ I remember watching the graphs Twitch stream. SW asked the very same questions and there was no answer to either one. Why did it make it so far in this state then? $\endgroup$
    – Szabolcs
    Commented Apr 9, 2019 at 9:10
  • $\begingroup$ @Szabolcs I did not have a chance too look at it in depth, but something like "energy with vertices as mass points and edges as springs". If you mean bent edges - not sure why but I like it. My guess is that close about parallel edges will have an opposite curvature to not overlap --- but I am not sure. There are too many things for one person to be aware of :-) $\endgroup$ Commented Apr 9, 2019 at 9:16
  • $\begingroup$ If you like curved edges, maybe you can convince the developer to document EdgeShapeFunction -> "CurvedArc", something we've been requesting for years. A graph layout should not affect edge rendering, as these are separate concepts. BTW I do not see any documented way to switch back to the default edge rendering. $\endgroup$
    – Szabolcs
    Commented Apr 9, 2019 at 9:22

Your Answer

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