4
$\begingroup$

I use the following code plot a binary tree and wants to change an edge to double arrow.

g = TreeGraph[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30, 
   24 -> 50, 40 -> 55, 40 -> 45}, 
  VertexShapeFunction -> ({EdgeForm[White], 
      RGBColor[113/255, 190/255, 236/255], Disk[#1, 0.2], White, 
      Text[#2, #1]} &)]

This plots the tree well. My question is how do I change a single edge to a double arrow, as illustrated below? It looks EdgeShapeFunction can do this, and I have tried the following without success. Thank you.

SetProperty[{g, 11 -> 23}, EdgeShapeFunction -> ("DoubleArrowheads")]

enter image description here


I also have tried

SetProperty[{g, (11 -> 23)}, EdgeStyle -> {Arrowheads[{-.05, .05}]}]

However, the arrows overlap with the vertex.

enter image description here

BTW, anyone knows how to chnage the font size in tree graph?

$\endgroup$
  • 1
    $\begingroup$ Use the option EdgeStyle -> {(11 -> 23) -> Arrowheads[{-.05, .05}]},? $\endgroup$ – kglr Nov 16 '15 at 13:43
  • 1
    $\begingroup$ ... or use SetProperty[{g, (11 -> 23)}, EdgeStyle -> {Arrowheads[{-.05, .05}]}]. $\endgroup$ – kglr Nov 16 '15 at 13:44
  • $\begingroup$ or SetProperty[{g, (11 -> 23)}, {EdgeStyle -> {Arrowheads[{-.05, .05}]}, PerformanceGoal -> "Quality"}] $\endgroup$ – halmir Nov 16 '15 at 17:44
2
$\begingroup$

You could use

SetProperty[{g, (11 -> 23)}, EdgeStyle -> {Arrowheads[{{-.05, .2}, {.05, .8}}]}]

but since the offset is scaled relative to the line length, it's not possible in general to point the edge of the disk:

enter image description here

You can create custom EdgeShapeFunction to replace automatic:

arrow[pts : {p1_, p2_}, offset_, arrowheads_: {}] := 
  With[{m = Mean[pts], w = Normalize[p2 - p1], 
    n = Norm[p1 - p2]/2}, {Arrowheads@arrowheads, 
    Arrow@{m - (n - offset) w, m + (n - offset) w}}];

g = TreeGraph[{11 -> 23, 11 -> 24, 23 -> 40, 23 -> 39, 24 -> 30,    24 -> 50, 40 -> 55, 40 -> 45},

  EdgeShapeFunction -> (arrow[#, .2] &), 
  VertexShapeFunction -> ({EdgeForm[White], 
      RGBColor[113/255, 190/255, 236/255], Disk[#1, 0.2], White, 
      Text[#2, #1]} &)
]

and

SetProperty[{g, (11 -> 23)}, EdgeShapeFunction -> (arrow[#, .2, {-.05, .05}] &)]

enter image description here

$\endgroup$
  • $\begingroup$ I'm curious why I had to sue EdgeShapeFunction -> (arrow[#, .2] &), for g. If it is automatic, SetProperty results in single arrowhead added to every other edge... $\endgroup$ – Kuba Nov 16 '15 at 16:11

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.