2
$\begingroup$

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?

$\endgroup$
3
  • 3
    $\begingroup$ 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. $\endgroup$ – Szabolcs May 11 '17 at 8:43
  • $\begingroup$ 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. $\endgroup$ – bpioline May 11 '17 at 13:13
  • $\begingroup$ 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. $\endgroup$ – Szabolcs May 11 '17 at 13:22
3
$\begingroup$

You can specify PlotTheme in TreeGraph

TreeGraph[{1 -> 2, 1 -> 3}, 
 VertexLabels -> 
  Thread[{1, 2, 3} -> (Placed[#, Center] & /@ {"a", "b", "b"})], 
 PlotTheme -> "VintageDiagram"]
$\endgroup$
1
  • $\begingroup$ Thanks for your help, I have switched to TreeGraph and got it to work. $\endgroup$ – bpioline May 11 '17 at 14:54
2
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.