I am trying to create a multi-coloured tree plot (Mathematica 9) where each branch is coloured according to its parent node value. For example, for a tree plot with three nodes, n1, n2 and n3, coming from a single parent node (n0), the children nodes of n1 should be red & n2 and n3 should be green.
TreePlot[nodes, EdgeRenderingFunction -> ({Red, Line[#1]} &),
VertexRenderingFunction -> (Inset[
Row[{If[Last[#2] > 3, Rotate[Last[#2], 90 Degree],
Last[#2]]}], #1, Background -> White] &)]
I am using the EdgeRenderingFunction for this within the following expression which will render the edges of the tree plot. The problem, I think, is that the If statement is not evaluated straight away and so the compiler does not recognise the first option (contained within the If statement) as a relevant one. As you can see I'm trying to create a tree plot with red and green branches.
TreePlot[nodes,
EdgeRenderingFunction -> ({If[Last[#2] == 1, Red, Green],
Line[#1]} &),
VertexRenderingFunction -> (Inset[
Row[{If[Last[#2] > 3, Rotate[Last[#2], 90 Degree],
Last[#2]]}], #1, Background -> White] &)]
The error message I get is:
If is not a Graphic Primitive or Directive
Thanks for your time in helping me with this.
VertexRenderingFunction
part? What is your goal with the vertex rendering? The second parameter passed (#2
) is the vertex name, so I'm not sure what your function is trying to do with that. $\endgroup$==
does not evaluate. This indicates that you may have nodes with non-numerical names. The fix may be as simple as using===
, but I can't tell for sure without seeing a complete example (with data). $\endgroup${{0} -> {0, 1}, {0} -> {0, 2}, {0} -> {0, 3}, {0, 1} -> {0, 1, 4}, {0, 1} -> {0, 1, 5}, {0, 1} -> {0, 1, 6}, {0, 1} -> {0, 1, 7}, {0, 1} -> {0, 1, 8}...
The code works with===
orTrueQ[If...]
instead of==
(Thanks!) but I got an unexpected result - all edges were rendered the same colour. So either theEdgeRenderingFunction
function renders the whole tree object rather than it's parts or I'll need to include the edge colour into the tree data structure (node table), or use a different way (function) to represent the tree? $\endgroup$