7
$\begingroup$

I need to display a directed acyclic graph (say Hasse diagram of partial order) oriented with minimal elements at the bottom, maximal elements at the top as is conventional for Hasse diagrams.

I tried it for:

graph1 = Graph[{b \[DirectedEdge] a, c \[DirectedEdge] a, d -> b, 
   e -> b}, EdgeStyle -> Arrowheads[.04], 
  VertexLabels -> Placed["Name", Center], VertexSize -> 0.2, 
  VertexStyle -> White, VertexLabelStyle -> Directive[Red, 15], 
  EdgeStyle -> Blue, VertexShapeFunction -> "Square"]

Using:

graph2 = ReverseGraph[graph1,VertexCoordinates -> GraphEmbedding[graph1]]

However, this merely reverses the arrows. I need to take the mirror image of the graph (with respect to a horizontal line drawn above the graph) so that minimal elements end up at the bottom, maximal at the top.

So a ^ shaped Hasse diagram, should end up as a v shaped diagram. However nodes need to stay in the correct order: the vertices that were the minimal elements in ^ (the two bottom elements with arrows drawn down from the top element, i.e. minimum) should end up as minimal elements in the result (with arrows drawn up from the bottom element, i.e. minimum).

Is there an operation that achieves it?

I tried HasseDiagram.

HasseDiagram[graph1, 
  EdgeStyle -> Arrowheads[.04], 
  VertexLabels -> Placed["Name", Center], VertexSize -> 0.2, 
  VertexStyle -> White, VertexLabelStyle -> Directive[Red, 15], 
  EdgeStyle -> Blue, VertexShapeFunction -> "Square"]

It did not produce the right output, but also the output indicated an "upside-down" hasse diagram. Again, it would need to be displayed with maximal elements at the top, minimum at the bottom.

$\endgroup$
7
$\begingroup$
graph2 = SetProperty[graph1, 
 VertexCoordinates ->  ReflectionTransform[{0, -1}] @ GraphEmbedding[graph1]]

enter image description here

Show[graph1, graph2, Axes -> {True, False}, Ticks -> False]

enter image description here

Alternatively, you can use the built-in graph layout "LayeredDigraphEmbedding" with the option "Orientation" -> Top or "Orientation" -> Bottom:

SetProperty[graph1, 
   {ImageSize -> Medium, 
    GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> #}}] & /@ 
  {Top, Bottom} // Row

enter image description here

$\endgroup$
2
  • $\begingroup$ Many thanks kglr $\endgroup$ – Mike Feb 24 at 19:01
  • 1
    $\begingroup$ @Mike, my pleasure. Thank you for the accept. $\endgroup$ – kglr Feb 24 at 19:02

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.