Search code examples
graphviz

Labels not following edge lines


I used graphviz before for much smaller graphs, this time a structural equation modelling type of graph is giving me some headache. The labels in the top part of the graph seems not to follow the lines if spline=line is used. This is more prominent in the top part of the graph.

  • How can I get the labels to match the paths correctly?
digraph plot {

    
    graph [ fontsize = 10, splines=line, nodesep = 0.4];
    
    
    subgraph controls {
        rank = same
        node [shape = box];
            a; b; c; d; e; f;
    }
    
    node [shape = circle]
       G

    node [shape = box ];
        H; I; J; K; L; M; O; P
        
        
    edge [ color = black ]
        X -> G [label = "0.19***"]
        d->G [label = "0.06***"] 
        e->G [label = "-0.15***" color=red]
        a->X [label = "0.3***"] 
        b->X [X = "0.21***"] 
        c->X [label = "0.08***"]
        f->X [label = "-0.08***" color=red] 
        G->H [label = "0.85***"] 
        G->I [label = "0.89***"] 
        G->J [label = "0.76***"] 
        G->K [label = "0.85***"] 
        G->L [label = "0.74***"]
        G->M [label = "0.81***"] 
        G->O [label = "0.8***"]
        G->P [label = "0.76***"]
        a -> G [label = "-0.32***" color=red]
        b->G [label = "0.15***"]
        c->G [label = "0.06***"]
        f->G [label = "-0.12***" color=red]
        d->X [label = "0.06***"] 
        e->X [label = "-0.09***" color=red] 
        G->G [label= 1 ]
    
    subgraph central {
        rank = same
        node [shape = circle]
            G
        node [shape = box];
            X;
    }
    


}

Results in:

enter image description here

p.s. Other improvement suggestions are welcome too.


Solution

    • Graphviz does not offer edge labels that follow the curve of the edge. There is a request for this feature (https://gitlab.com/graphviz/graphviz/-/issues/2007). You might share your interest.
    • your source had a couple of suboptimals
      • G was declared twice - legal but unnecessary
      • edge b->X has an X attribute instead of label. No biggie

    Possibilities:

    • consider other layout engines. circo and twopi are interesting
    • increase nodesep
    • try splines=true (this really seems to help)
    • match font color to edge color
    • consider removing rank=same for X and G (again, this really seems to help)
    • for a-f -> X, try taillabel instead of label

    Most of these changes are cosmetic, just to make things easier for me to understand.
    The one iffy change was changing the G and X ranking. Your call.

    digraph plot {
        graph [ fontsize = 10, splines=line, nodesep = 0.8];
        splines=true
        
        subgraph controls {
            rank = same
            node [shape = box];
                a; b; c; d; e; f;
        }
        subgraph central {
            //rank = same
            G [shape = circle]
            X [shape = box];
        }
        
        node [shape = box ];
            H; I; J; K; L; M; O; P
            
        edge [ color = black ]
            a->X [taillabel = "0.3***"] 
            b->X [taillabel = "0.21***"] 
            c->X [taillabel = "0.08***"  labelangle=-70]  // tweak
            f->X [taillabel = "-0.08***" color=red] 
            d->X [taillabel = "0.06***"] 
            e->X [taillabel = "-0.09***" color=red] 
    
            X->G [label = "0.19***"]
            d->G [label = "0.06***"] 
            e->G [label = "-0.15***" color=red]
            a->G [label = "-0.32***" color=red]
            b->G [label = "0.15***"]
            c->G [label = "0.06***"]
            f->G [label = "-0.12***" color=red]
            G->G [label= 1 ]
        
            G->H [label = "0.85***"] 
            G->I [label = "0.89***"] 
            G->J [label = "0.76***"] 
            G->K [label = "0.85***"] 
            G->L [label = "0.74***"]
            G->M [label = "0.81***"] 
            G->O [label = "0.8***"]
            G->P [label = "0.76***"]
    }
    

    Giving:
    enter image description here