4
$\begingroup$

I am building some tools to help me visualize fitting of generic n-dimensional nonlinear model. I have the data and results stored in a nested association. I've written a few functions to generate a Tree[] that provides a high level view of the structure.

The edge labels are unfortunately overlapping, as you can see. My desire is to rotate the labels to lay (lie?) parallel to their edges. I have been trying to understand the behavior of ParentEdgeLabel but i cannot make sense of it, even the use of Placed[].

If possible, how do I rotate labels in a Tree[]

enter image description here

for reference, here is the code i used to generate this smaller example tree (Using 14.0, fresh kernel, I tried on 13.0 but some of the options for Tree[] are newer i spose)

enter image description here

burnLeaves[l_List|l_Association]:=If[AllTrue[l,Not[ListQ[#]]&],{},Map[burnLeaves,l]]; burnLeaves[x_]:=Nothing; emptyLeaves[list_List|list_Association]:=Map[emptyLeaves,list]; emptyLeaves[tree_Tree]:=tree; emptyLeaves[_]={}; linearIndicize[list_]:=Block[{n},n=1;Map[n++&,burnLeaves[list],{-2}]] applyToNestedAssociation[f_,expr_]:=Module[{sortedPosfa,inner,out},sortedPosa=SortBy[Position[expr,_Association],-Length[#]&]; inner=FoldList[ReplacePart[#1,#2->f[Extract[#1,#2]]]&,expr,sortedPosa];If[AssociationQ[expr],out=f[Last@inner],out=inner]; out] treeAssociation[assoc_]:=Module[{treemaker,viewer,out},treemaker[assc_]:=Tree[assc,TreeLayout->"RadialEmbedding",PlotRangePadding->0]; viewer[asc_]:=Module[{step1,step21,step31,innerout},If[LeafCount[asc]<100,Return[treemaker[asc]],step1=emptyLeaves[asc];]; If[LeafCount[step1]<100,Return[treemaker[step1]],step2=linearIndicize[step1];]; step3=Dimensions/@step2; innerout=treemaker[step3]; innerout]; out=Tree[applyToNestedAssociation[viewer,assoc],TreeElementLabel->"Root"]; out] Tree[<|"keyA"->{b1,b2,b3,b4},"keyB"->{1,2,3},"keyC"-><|"keyC1"->"C1","keyC2"->"C2"|>|>] 
$\endgroup$

1 Answer 1

3
$\begingroup$

Since Tree[ ] is still in EXPERIMENTAL in MMA Ver 14.0.0, there seems to be no canonical solution. If you are happy with rotating the edge labels of Graph[ ] instead, the following code might be useful. I tested this on only a few tree graphs, though. You can convert a Tree into Graph by Graph[ ] or TreeGraph[ ].

ClearAll[tilting]; tilting[g_Graph] := Module[{vertexcoordinates, oldedgelabels, newedgelabels, from, to}, vertexcoordinates = Thread[VertexList[g] -> GraphEmbedding[g]]; oldedgelabels = Table[edge -> (edge /. (EdgeLabels /. AbsoluteOptions[g])), {edge, EdgeList[g]}]; newedgelabels = Table[ {from, to} = (List @@ edge) /. vertexcoordinates; edge -> Rotate[Text[edge /. oldedgelabels, Background->White], ArcTan @@ (to - from)], {edge, EdgeList[g]}]; Graph[g, EdgeLabels -> newedgelabels] ]; 

Sample run:

nvert = 20; g0 = IndexGraph[RandomTree[nvert], GraphLayout -> "RadialEmbedding"]; edgelabels = Thread[EdgeList[g0] -> RandomWord["Adjective", EdgeCount[g0]]]; g = Graph[g0, EdgeLabels -> edgelabels, ImageSize -> 500] 

enter image description here

tilting[g] 

enter image description here

$\endgroup$
2
  • 4
    $\begingroup$ Try wrapping the labels in Text[..., Background -> White] for a bit of improvement. Or shift them a bit, so the edges will serve as a baseline for the text (no overlap) $\endgroup$ Commented Jul 5, 2024 at 12:40
  • 1
    $\begingroup$ @Szabolics Thank you. I changed the code and the sample image accordingly. $\endgroup$ Commented Jul 6, 2024 at 0:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.