0
$\begingroup$

Version: "14.3.0 for Linux x86 (64-bit) (July 8, 2025)"

The code listed below using the data following it produces the result shown in the image. I want the node labels positioned directly to the right of the labeled nodes. I really only want that for the leaf nodes. But I need to start somewhere. How can this be accomplished.

graph with misplaced labels

In a sandbox directory create and save a Mathematica notebook with the following content.

Module[{g, emb, scaledEmb, embF}
 , SetDirectory[NotebookDirectory[]];
 g = Import["ss01-05-sentence-leafnodes.gv", "Graph"];
 embF[g_] := GraphEmbedding[g
   , {"LayeredDigraphEmbedding", "Orientation" -> Left, 
    "RootVertex" -> "CH"}];
 emb = Map[{#[[1]]/2, -#[[2]]} &, embF[g]];
 scaledEmb = Map[{#[[1]]/2, #[[2]]} &, emb];
 Graph[g
  , VertexCoordinates -> scaledEmb
  , VertexLabels -> 
   Placed["Name", {Right, Center}, Offset[{5, 0}, #] &]
  , VertexSize -> 0.1
  , EdgeStyle -> Directive[Arrowheads[0.025]]
  , VertexLabelStyle -> Directive[FontSize -> 8, Background -> White]
  , ImageSize -> Large
  , ImagePadding -> {{50, 200}, {50, 50}} ]]

In the same directory as the Mathematica notebook create a file named ss01-05-sentence-leafnodes.gv with the following content:

digraph DocumentStructure {
  rankdir=TB;
  node [shape=record];
  CH [label="CH"];
  S01 [label="S01"];
  S01P01 [label="S01P01"];
  S01P01T01 [label="S01P01T01"];
  S01P01T02 [label="S01P01T02"];
  S01P01T03 [label="S01P01T03"];
  S01P01T04 [label="S01P01T04"];
  S01P01T05 [label="S01P01T05"];
  S01P01T06 [label="S01P01T06 | LN01"];
  S01P01T07 [label="S01P01T07"];
  S01P01T08 [label="S01P01T08"];
  S01P01T09 [label="S01P01T09"];
  S01P01T10 [label="S01P01T10"];
  S01P01T11 [label="S01P01T11"];
  S01P01T12 [label="S01P01T12 | LN02"];
  S01P02 [label="S01P02"];
  S01P02T01 [label="S01P02T01"];
  S01P02T02 [label="S01P02T02"];
  S01P03 [label="S01P03"];
  S01P03T01 [label="S01P03T01"];
  S02 [label="S02"];
  S02P01 [label="S02P01"];
  S02P01T01 [label="S02P01T01"];
  S02P01T02 [label="S02P01T02"];
  S02P01T03 [label="S02P01T03"];
  S02P01T04 [label="S02P01T04"];
  S02P01T05 [label="S02P01T05 | LN03"];
  S02P02 [label="S02P02"];
  S02P02T01 [label="S02P02T01"];
  S02P03 [label="S02P03"];
  S02P03T01 [label="S02P03T01"];
  S02P03T02 [label="S02P03T02"];
  S02P03T03 [label="S02P03T03"];
  S02P04 [label="S02P04"];
  S02P04T01 [label="S02P04T01"];
  S02P05 [label="S02P05"];
  S02P05T01 [label="S02P05T01"];
  S02P05T02 [label="S02P05T02"];
  S02P06 [label="S02P06"];
  S02P06T01 [label="S02P06T01"];
  S02P07 [label="S02P07"];
  S02P07T01 [label="S02P07T01"];
  S02P08 [label="S02P08"];
  S02P08T01 [label="S02P08T01"];
  S02P08T02 [label="S02P08T02"];
  S02P08T03 [label="S02P08T03 | LN04"];
  S03 [label="S03"];
  S03P01 [label="S03P01"];
  S03P01T01 [label="S03P01T01"];
  S03P01T02 [label="S03P01T02"];
  S03P01T03 [label="S03P01T03"];
  S03P01T04 [label="S03P01T04 | LN05"];
  CH -> S01;
  S01 -> S01P01;
  S01P01 -> S01P01T01;
  S01P01 -> S01P01T02;
  S01P01 -> S01P01T03;
  S01P01 -> S01P01T04;
  S01P01 -> S01P01T05;
  S01P01 -> S01P01T06;
  S01P01 -> S01P01T07;
  S01P01 -> S01P01T08;
  S01P01 -> S01P01T09;
  S01P01 -> S01P01T10;
  S01P01 -> S01P01T11;
  S01P01 -> S01P01T12;
  S01 -> S01P02;
  S01P02 -> S01P02T01;
  S01P02 -> S01P02T02;
  S01 -> S01P03;
  S01P03 -> S01P03T01;
  CH -> S02;
  S02 -> S02P01;
  S02P01 -> S02P01T01;
  S02P01 -> S02P01T02;
  S02P01 -> S02P01T03;
  S02P01 -> S02P01T04;
  S02P01 -> S02P01T05;
  S02 -> S02P02;
  S02P02 -> S02P02T01;
  S02 -> S02P03;
  S02P03 -> S02P03T01;
  S02P03 -> S02P03T02;
  S02P03 -> S02P03T03;
  S02 -> S02P04;
  S02P04 -> S02P04T01;
  S02 -> S02P05;
  S02P05 -> S02P05T01;
  S02P05 -> S02P05T02;
  S02 -> S02P06;
  S02P06 -> S02P06T01;
  S02 -> S02P07;
  S02P07 -> S02P07T01;
  S02 -> S02P08;
  S02P08 -> S02P08T01;
  S02P08 -> S02P08T02;
  S02P08 -> S02P08T03;
  CH -> S03;
  S03 -> S03P01;
  S03P01 -> S03P01T01;
  S03P01 -> S03P01T02;
  S03P01 -> S03P01T03;
  S03P01 -> S03P01T04;
}

$\endgroup$
1
  • $\begingroup$ Using Graph[g, GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left, "RootVertex" -> "CH"}, ...] should align the leaf labels vertically as expected. $\endgroup$ Commented 22 hours ago

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.