# Can I stop edges in graphs from drawing on top of the vertices?

Consider the following

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];


The nodes are almost completely hidden in the mess of edges. They can be made easier to see by making the edges fade into the background a little.

AdjacencyGraph[testadj, EdgeStyle -> Directive[Opacity[0.4], Gray],
VertexSize -> Large]


But we still have the problem that the lines draw over the top of the nodes. Is there a way to force the nodes to draw on top of the lines?

-
I have a feeling this has been asked before, or at least came up in a question. I can't find it unfortunately. –  Szabolcs Jul 26 '12 at 9:11
Do you need to keep the graph functionality or would it be enough to create a Graphics (not Graph) object that has the vertices on top? –  Szabolcs Jul 26 '12 at 9:12
@Szabolcs This one? stackoverflow.com/a/8205128/353410 –  belisarius Jul 26 '12 at 12:51
@belisarius - it's a different problem. –  Verbeia Jul 27 '12 at 0:07
@Verbeia Look at Mr's answer :) –  belisarius Jul 27 '12 at 0:11

This is a nasty hack. It might be the quickest workaround until you find a solution.

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];

Show[gr, SetProperty[gr, EdgeShapeFunction -> ({} &)]]


The end result is a Graphics object, not a Graph. I am using {} as a "neutral graphics object", something that is accepted inside Graphics, but does not render.

Unfortunately the analogous SetProperty[gr, VertexShapeFunction -> ({} &)] does not seem to work, and I don't understand why. It may have to do something with the fact that the system analyses the vertex shape to make the edges join up nicely to them. If you need to make them disappear, you can use SetProperty[gr, VertexShape -> None].

-

GraphPlot does alright. Perhaps Inset is the key.

testadj = RandomInteger[BernoulliDistribution[0.15], {50, 50}];

(* gr = graphic *)

GraphPlot[testadj, VertexRenderingFunction -> (Inset[gr, #1] &)]


-
I´d go for the Evolution smiley ;-) –  Yves Klett Jul 26 '12 at 9:49
Having our Q&A in two different sites makes us repeat ourselves :) stackoverflow.com/questions/885910/… –  belisarius Jul 26 '12 at 15:03
This is a similar solution but the problem is actually different. –  Verbeia Jul 27 '12 at 0:07

Here is my implementation by modifying the Box structures.

Clear[vertexFirstShow]
vertexFirstShow[graph_] :=
Module[{graphdata, vShow},
graphdata = ToBoxes[graph];
vShow =
Cases[graphdata, GraphicsGroupBox[{v_, e_}] :> v, \[Infinity]][[1]]
/. {
TagBox[DiskBox[pos_, r_], "DynamicName", BoxID -> id_]
:> DiskBox[DynamicLocation[id], r],
TagBox[StyleBox[DiskBox[pos_, r_], opts__], "DynamicName", BoxID -> id_]
:> StyleBox[DiskBox[DynamicLocation[id], r], opts]
};
With[{v2 = vShow},
ToExpression[
graphdata /. GraphicsGroupBox[{v_, e_}] :> GraphicsGroupBox[{v, e, v2}]]
]]

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];

graph = AdjacencyGraph[testadj, VertexSize -> Large, GraphHighlight -> {1, 2, 3}]

vertexFirstShow[graph]


It retains a Graph object, and I'm sure the code can be improved to fit more complicated cases.

-
Really nice solution, thanks! I accepted @Szabolcs' because it was sufficient for our purposes. –  Verbeia Jul 27 '12 at 0:07
@Verbeia His answer is great. It never comes to my mind to use EdgeShapeFunction like that. :) –  Silvia Jul 27 '12 at 0:12

You can always extract full info from Graph and then use graphics primitives. It is more elaborate but it gives full control.

testadj = RandomVariate[BernoulliDistribution[0.15], {50, 50}];