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]
*GraphPlot
functions are the old way to visualize a graph, from before when Mathematica got aGraph
datatype. I'd consider them deprecated. Originally they just took a pair-list or a rule-list, not aGraph
. 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 byGraphPlot
), or you can use the new way to visualize the graph,SetProperty[g, GraphLayout -> "LayeredEmbedding"]
. $\endgroup$GraphPlot
functions are still around is backwards compatibility... $\endgroup$