6
$\begingroup$

In Graph or TreeGraph, I can make the vertices images, but the tree always starts at the top.

TreeGraph[{1 -> 2, 1 -> 3}, 
 VertexShape -> {1 -> Graphics[RegularPolygon[4]], 
   2 -> Graphics[RegularPolygon[5]], 
   3 -> Graphics[RegularPolygon[6]]}, VertexSize -> 0.6]

TreeGraph

If I use TreePlot instead, then I can make the tree start on the left:

TreePlot[{1 -> 2, 1 -> 3, 3 -> 4, 3 -> 5}, Left]

TreePlot

But TreeGraph doesn't have the "Left" option and TreePlot doesn't have the VertexShape option. How can I have images in a tree that goes from left to right?

Graph has the GraphLayout Option, but I couldn't find the one that does what I want.

$\endgroup$
9
$\begingroup$

It's explained on the doc page of GraphLayout. This documentation page is very long, and most of the information is under the Scope section, under specific layout methods.

Graph[{1 -> 2, 1 -> 3, 3 -> 4, 3 -> 5}, 
 GraphLayout -> {"LayeredEmbedding", "RootVertex" -> 1, "Orientation" -> Left}]

Mathematica graphics

You do not need to use TreeGraph unless you also want to verify that the graph is indeed a tree. Graph will do. The key is GraphLayout.

With this layout, the root of directed trees is not detected automatically. You can specify it automatically as I did above. With "LayeredDigraphEmbedding", it is detected, but the layout will not be identical.


You can also achieve the same with IGLayoutReingoldTilford from the IGraph/M package.

IGLayoutReingoldTilford[
 Graph[{1 -> 2, 1 -> 3, 3 -> 4, 3 -> 5}],
 "RootVertices" -> {1}, "Rotation" -> Pi/2
 ]

Mathematica graphics

$\endgroup$
6
$\begingroup$
tg = TreeGraph[{1 -> 2, 1 -> 3}, 
   VertexShape -> {1 -> Graphics[RegularPolygon[4]], 
       2 -> Graphics[RegularPolygon[5]], 
       3 -> Graphics[RegularPolygon[6]]}, VertexSize -> 0.6] 
   
SetProperty[tg, VertexCoordinates -> RotationTransform[Pi/2][GraphEmbedding[tg]]]

enter image description here

Alternatively, you can use TreePlot with the hidden options "VertexNames", "VertexFrameBackground" and "VertexFrameStyle":

shapes = Graphics[RegularPolygon[#], ImageSize -> 90, Background -> None] & /@ 
  {4, 6, 5}; 

TreePlot[{1 -> 2, 1 -> 3}, Left, 
  DirectedEdges -> True, VertexLabeling -> True, 
 "VertexNames" -> shapes, 
 "VertexFrameBackground" -> None, 
 "VertexFrameStyle" -> None]

enter image description here

$\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.