I’m trying to construct a LayeredGraphPlot where some nodes connect to other nodes, like a tree. I would like to have the freedom to choose the thickness, color, presence or absence of labeling of each edge.
I would also like to have the freedom to display some vertex names and display some vertices as just “dots”. I can’t seem to understand how EdgeRenderingFunction and VertexRenderingFunction enable me to do this.
I am new to mathematica. I’ve clicked on more information on Wolfram help page but it appears to have a vague description and not precise syntax, followed by some cute but unhelpful (to me) examples (As compared to matlab at mathworks where the help provides precise syntax.. at least in my mind).
I have looked up about 10 mathematics books (Smith and Blachman, mathematica demystified.. etc etc) but they all seem to cover the function superficially with one or two examples and not provide the most general syntax.
Can someone help with this and I would also appreciate tips on how to learn mathematica? I’m a smart guy and I should not have so much trouble learning how to use commands.
LayeredGraphPlot[{1->2,1->3,2->4,3->5,3->6}]
So for example, I’d like to:
- Suppress all the vertex names except vertices 4,5 and 6.
- Color as blue and thick the edges from 3->6, 2->4 and 1->3
- All the other edges to be red and thin
VertexRenderingFunctionandEdgeRenderingFunctionallow you to take explicit control over the way that vertices and edges are drawn in the graph. Both functions are expected to return a symbolic graphics directive (or list of such directives) that is valid for the Graphics command.Let’s start with
VertexRenderingFunction. We shall define a new function calleddrawVertex. AVertexRenderingFunctionis called with two arguments: the position of the vertex (as an X/Y co-ordinate pair) and the name of the vertex.Two definitions are provided for
drawVertex. The first is only applicable to the vertices4or5or6. It draws those vertices as framed labels. The second definition is applicable to all other vertices and draws simple blue points.Now for an
EdgeRenderingFunctionnameddrawEdge. The function will be passed three arguments: the endpoints of the edge, a list of the source and target vertices of the edge, and the label for the edge (if any). In our case, all edges will be drawn as arrows but the colour and thickness will vary depending upon the edge. The helper functionedgeStyleis introduced to capture those differences:With these definitions in place, we can now use them in conjunction with
LayeredGraphPlotto produce a customized diagram: