3

I want to specify the labels attached to vertices in TreePlot, allowing distinct vertices to carry the same label. The following attempt fails:

aTree =   TreeGraph[{1 -> 2, 1 -> 3}, VertexLabels -> {1 -> "a", 2 -> "b", 3 -> "b"}]; 
TreePlot[aTree, VertexLabeling -> True]

and so does

aTree = TreeGraph[{1 -> 2, 1 -> 3},  VertexLabels -> {1 -> "a", 2 -> "b", 3 -> "b"}]; 
TreePlot[ Rule @@@ EdgeList[aTree], DirectedEdges -> True,  VertexLabeling -> True]

Any idea?

kglr
  • 394,356
  • 18
  • 477
  • 896
bpioline
  • 31
  • 1
  • 3
    Why are you using TreePlot when you already have the correct layout from TreeGraph? You can just re-style the TreeGraph using EdgeStyle, VertexStyle, Placed in VertexLabels, VertexShapeFunction, etc. See the documentation for each of these. – Szabolcs May 11 '17 at 08:43
  • The reason is that I'm fully happy with the layout produced by TreePlot, except for the fact it does not allow identical labels for different vertices. – bpioline May 11 '17 at 13:13
  • Sounds like all you need is TreeGraph then, as it produces the same layout as TreePlot. TreePlot and GraphPlot are old functions from before Mathematica has a Graph data structure. They are fully replaced by newer constructs. – Szabolcs May 11 '17 at 13:22

3 Answers3

3

You can specify PlotTheme in TreeGraph

TreeGraph[{1 -> 2, 1 -> 3}, 
 VertexLabels -> 
  Thread[{1, 2, 3} -> (Placed[#, Center] & /@ {"a", "b", "b"})], 
 PlotTheme -> "VintageDiagram"]
halmir
  • 15,082
  • 37
  • 53
2

Update 2: Came across a hidden option "VertexNames" for TreePlot

TreePlot[{1 -> 2, 1 -> 3}, 
  VertexLabeling -> True, 
 "VertexNames" -> {"A", "B", "B"}]

enter image description here

This also works for GraphPlot and LayeredGraphPlot.

Note: In versions 12.0, TreePlot is changed substantially, and the legacy Treeplot is available under the name GraphComputation`TreePlotLegacy.

Update: If you have to use TreePlot you can post-process the output of TreePlot to relabel the vertices.

The function reLabelF below takes a list of vertices to be relabeled (vertices) and their new labels(labels) and post-processes a TreePlot output (tp) to get a relabeled TreePlot:

ClearAll[reLabelF]
reLabelF[verts_, labels_][tp_] := tp /. (Framed[#, p__] :> Framed[#2, p] & @@@ 
    Transpose[{verts, labels}])

Examples:

vertices = {1, 2, 3};
labels =  Style[#, 18, "Panel"] & /@ {"a", "b", "b"};

TreePlot[{1 -> 2, 1 -> 3}, VertexLabeling -> True] // reLabelF[vertices, labels]

enter image description here

Relabel only the vertices {1, 2, 3}:

 TreePlot[{1 -> 2, 1 -> 3, 1 -> 4, 2 -> 5}, VertexLabeling -> True] // 
   reLabelF[vertices, labels]

enter image description here

Relabel all vertices:

TreePlot[{1 -> 2, 1 -> 3, 1 -> 4, 2 -> 5}, VertexLabeling -> True] // 
 reLabelF[Range[5], Style[#, 18, "Panel"] & /@ {"a", "b", "b", "b", "c"}]

enter image description here

Original answer:

You can specify the labels for each vertex using Property and use the built-in "VintageDiagram" for GraphStyle option setting:

labeledvertices = Property[#, VertexLabels -> 
  Placed[Style[#2, 18, "Panel", Background -> Transparent], Center]] & @@@
    Transpose[{{1, 2, 3}, {"a", "b", "b"}}];

TreeGraph[labeledvertices, {1 -> 2, 1 -> 3}, GraphStyle -> "VintageDiagram"]

enter image description here

Alternatively, use a different built-in GraphStyle:

TreeGraph[labeledvertices, {1 -> 2, 1 -> 3},  GraphStyle -> "DiagramGold"]

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896
1

In:

rule = {1 -> "a", 2 -> "b", 3 -> "b"};
shapeRule = Rule[#1, Text[Style[#2, Black, "Text"]]] & @@@ rule;
aTree = TreeGraph[{1 -> 2, 1 -> 3}, VertexShape -> shapeRule, 
  VertexSize -> Large]

Out:

Mathematica graphics

webcpu
  • 3,182
  • 12
  • 17