# Why does (Layered)GraphPlot lose labels and renumber vertices?

I define a graph of numbers as vertices, but also labeled, like g=Graph[{Labeled[1 \[DirectedEdge] 5, "text1"]…},VertexLabels -> {79 -> "text2",…}]. This is immediately plotted in the output, and that plot is labeled correctly (both vertices and edges, the original numbers used for vertices don't even show up). Then when I plot it with LayeredGraphPlot[g, VertexLabeling -> Tooltip,EdgeLabeling -> Automatic], the numbers showing up as vertex label tooltips are neither the defined labels, nor even the original vertex numbers, but integers (not even in an obvious order). This makes it the plot very misleading and hard to parse. What goes wrong here, and how to do it better?

If I ask for EdgeLabeling too, the edge labels don't show up, so the plot lost them as well. Why?

This behavior is the same with more basic GraphPlot or the more specific LayeredGraphPlot.

See this toy example:

g = Graph[{Labeled[1 \[DirectedEdge] 5, "text1"]},
VertexLabels -> {1 -> "text2", 5 \[DirectedEdge] "text3"}]
LayeredGraphPlot[g, VertexLabeling -> Tooltip,
EdgeLabeling -> Automatic]

• Can you give a complete example please? – Szabolcs Jul 16 '15 at 11:59
• @Szabolcs Point taken, now you have the working example. – László Jul 16 '15 at 14:21
• This is likely because *GraphPlot functions are the old way to visualize a graph, from before when Mathematica got a Graph datatype. I'd consider them deprecated. Originally they just took a pair-list or a rule-list, not a Graph. They've been updated to support Graph, but it seems the conversion loses both labels and names, and keeps only vertex indices. You can either convert on your own (Rule@@@EdgeList[g] will be accepted by GraphPlot), or you can use the new way to visualize the graph, SetProperty[g, GraphLayout -> "LayeredEmbedding"]. – Szabolcs Jul 16 '15 at 15:42
• Sorry, no time to dig into this ... that's why this is only a comment. In short, I think the only reason GraphPlot functions are still around is backwards compatibility... – Szabolcs Jul 16 '15 at 15:42
• Yes, you're right LayeredGraphPlot used to give nicer output ... I'd try converting the graph manually to a format supported by LayeredGraphPlot. – Szabolcs Jul 16 '15 at 18:34

g = Graph[{Labeled[1 -> 5, "text1"]},
VertexLabels -> {1 -> "text2", 5 -> "text3"}]


You can use the undocumented option "VertexNames" to add vertex labels in LayeredGraphPlot. Somehow, edge labels are lost when the first argument of LayeredGraphPlot is a graph object:

LayeredGraphPlot[g, VertexLabeling -> All, EdgeLabeling -> True,
"VertexNames" -> {"text2", "text3"}]


Using a list of {edge, edgelabel} pairs in the first argument we get the desired result:

LayeredGraphPlot[{Rule @@ #, PropertyValue[{g, #}, EdgeLabels] /. $Failed -> ""} & /@ EdgeList[g], VertexLabeling -> True, EdgeLabeling -> True, "VertexNames" -> {"text2", "text3"}]  Another example: SeedRandom[1] {vlbls, elbls} = RandomWord["Noun", #] & /@ {8, 12}; rg = RandomGraph[{8, 12}]; eLbl = Association[# -> elbls[[EdgeIndex[rg, #]]] & /@ EdgeList[rg]]; vLbl = Association[# -> vlbls[[VertexIndex[rg, #]]] & /@ VertexList[rg]]; g0 = SetProperty[rg, { EdgeLabels -> {e_ :> eLbl[e]}, VertexLabels -> {v_ :> Placed[Framed[vLbl[v], Background -> White], Center]}, ImageSize -> 700}]  LayeredGraphPlot[{Rule @@ #, PropertyValue[{g0, #}, EdgeLabels] /.$Failed -> ""} & /@ EdgeList[g0],
VertexLabeling -> All, EdgeLabeling -> True, "VertexNames" -> vlbls, ImageSize -> 700]


Of course, using Graph with the option GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Top} we get the desired result much more conveniently without the need for LayeredGraphPlot:

SetProperty[g0,
GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Top}]


Note: In version 12.0, LayeredGraphPlot is updated with many changes and the above method does not work. To access the legacy version of GraphPlot use the function GraphComputationLayeredGraphPlotLegacy`.