# How to create diagrams using Wolfram / Mathematica?

How can one use Wolfram to make diagrams like

with the arrows labeled as well (to label "mediating factors" between the causal elements)?

vertices = {"rush hour", "bad weather", "accident", "traffic jam", "sirens"};

edges = DirectedEdge @@@ {"rush hour" -> "traffic jam", "bad weather" -> "accident",
"accident" -> "traffic jam", "bad weather" -> "traffic jam",
"accident" -> "sirens"};

edgelabels = RandomWord["Noun", Length @ edges];

Graph[edges,
PlotTheme -> "IndexLabeled",
VertexSize -> Large,


Use additional options to embellish the picture:

elabeling = AssociationThread[edges, edgelabels];

{.05, .45, Graphics @ Text[Framed[Style[elabeling @ #2, 14],
FrameStyle -> None,  Background -> White]]}}],
Last @ GraphElementData["Arrow"][##]} &;

coords = Drop[Join @@ Array[{ #2, (3 - #)}&, {2, 3}], {4}]

Graph[vertices, edges,
VertexLabelStyle -> 14,
ImageSize -> Large,
GraphStyle -> "IndexLabeled",
VertexSize -> .4,
EdgeShapeFunction -> eSF,
VertexCoordinates -> coords]


We can also construct the graphics primitives from scratch:

radius = Offset @  Max[(1.2/2)
Rasterize[Style[#, 14, "Graphics"], "RasterSize"][[1]] & /@ vertices];

Graphics @Text[Framed[Style[elabeling @ #, 14], FrameStyle -> None,
Background -> White], {0, 0}, {0, .25}]}}],
Arrow[List @@ # /. Thread[vertices -> coords]]} & /@ edges,
FaceForm[White], EdgeForm[Gray], Disk[#, radius] & /@ coords,
MapThread[Text, {Style[#, 16] & /@ vertices, coords}]},
ImageSize -> 800, PlotRangePadding -> Scaled[.2]]


Update: From comments: "Ideally a user just supplies a list of relationships (with possible labels)..."

elist = {{"rush hour" -> "traffic jam",  "empty"},
{"accident" -> "traffic jam", "sweatshirt"},
{"bad weather" -> "traffic jam", "pump"},
{"accident" -> "sirens", "nominative"}};


You can use GraphComputationLayeredGraphPlotLegacy or GraphComputationGraphPlotLegacy (if you have access to versions before v12 you can use LayeredGraphPlot and GraphPlot, respectively):

GraphComputationLayeredGraphPlotLegacy[elist,
DirectedEdges -> True, EdgeLabeling -> True, VertexLabeling -> True,
ImageSize -> 500, BaseStyle -> 15, PlotStyle -> Black]


GraphComputationGraphPlotLegacy[elist,
DirectedEdges -> True, EdgeLabeling -> True, VertexLabeling -> True,
ImageSize -> 500, BaseStyle -> 15, PlotStyle -> Black,
Method -> "LayeredDigraphDrawing"]


same picture

To render vertices as disks add the option

VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .3], Black, Text[#2, #1]} &)


to get

• kglr: Can you explain more the syntax behind eSF with ArrowHeads? Commented Aug 15, 2019 at 17:56
• I'm trying to make this generalizable to arbitrary vertices/edges (including undirected edges). Ideally a user just supplies a list of relationships (with possible labels) into a function and everything is automatically drawn. I've removed the coords field since that lets Wolfram draw the layout. Commented Aug 15, 2019 at 18:02
• @George, thank you for the accept. The function eSF takes the built-in edge shape function GraphElementData["Arrow"] and injects the edge label into the Arrowheads directive so that the label has the same orientation as the arrow. See Arrowheads >> Scope >> Custom Arrowheads for more examples of how custom arrowheads work. I use coords to match the picture. In general, it is more convenient to use the built-in vertex layouts.
– kglr
Commented Aug 15, 2019 at 18:36
• kglr: I love how the eSF creates space in the edges so that the text is always legible (i.e., the edge and the edge label never overlap). The problem with the eSF is that sometimes labels can be upside down if the nodes are configured in the right way. Is there a way to adjust the eSF so that the labels are always rightside up (like the first graph in your answer), but the spacing in between the edges and the text is preserved so that the labels are always legible? Commented Sep 13, 2019 at 19:08
• @George, The graphics glyphs in custom arrowheads have the same orientation as the arrow by default. We should be able to control the orientation of the text using the fourth argument of Text (Text[expr, coords, offset, dir] ) in custom arrowheads.
– kglr
Commented Sep 13, 2019 at 20:29

r = 1; (*radius of each disk*)

(*center of each disk. Numbers left to right, top to bottom*)
c1 = {0, 0}; c2 = {r + 2, 0}; c3 = {r + 5, 0}; c4 = {r + 2, -(r + 2)};
c5 = {r + 5, -(r + 2)};

makeDisk[r_, c_] := {EdgeForm[Black],LightYellow, Disk[c, r]}(*change as needed*)

makeArrow[from_, to_, dir_] := Module[{z = Cos[Pi/4]},
Which[
dir == "right",
Arrow[{{from[[1]] + r, from[[2]]}, {to[[1]] - r, to[[2]]}}],

dir == "down",
Arrow[{{from[[1]], from[[2]] - r}, {to[[1]], to[[2]] + r}}],

dir == "right-down",
Arrow[{{from[[1]] + z, from[[2]] - z}, {to[[1]] - z, to[[2]] + z}}],

dir == "left-down",
Arrow[{{from[[1]] - z, from[[2]] - z}, {to[[1]] + z, to[[2]] + z}}]

]
];
putLabel[txt_, at_] := Style[Text[txt, at], Bold, 12]
Graphics[{
makeDisk[1, c1],
makeDisk[1, c2],
makeDisk[1, c3],
makeDisk[1, c4],
makeDisk[1, c5],
makeArrow[c2, c3, "right"],
makeArrow[c2, c4, "down"],
makeArrow[c3, c5, "down"],
makeArrow[c1, c4, "right-down"],
makeArrow[c3, c4, "left-down"],
putLabel["rush hour", c1],