# Color Specified Vertices in LayeredGraphPlot

Can vertices S and R9 (for example) be fill colored (say red or black) in a LayeredGraphPlot?

edges = {S -> R9, S -> R4, S -> R1, R1 -> R2, R2 -> R3, R3 -> R4,
R4 -> R5, R5 -> R6, R6 -> T, R9 -> T, T -> S};

LayeredGraphPlot[edges, Left, VertexLabeling -> True,
EdgeRenderingFunction -> ({Black, Arrowheads[0.03],
Arrow[#1, 0.4]} &),
VertexRenderingFunction -> ({White, EdgeForm[Darker[Blue]],
Disk[#, .4], Black, Text[#2, #1]} &)]

• have you seen this question: mathematica.stackexchange.com/questions/33409/… Commented Jun 27, 2019 at 19:51
• Why don't you just use Graph[..., GraphLayout -> "LayeredDigraphEmbedding", VertexStyle -> {1->Red, 2->Green, ...}]? Commented Jun 27, 2019 at 22:22
• I prefer the orientaton options LayeredGraphPlot allows such as Left, Top, etc ... the graph layout you show is nice - being parallel edges. Is there a way to shift that orientation as in the LayerGraphPlot? Commented Jun 27, 2019 at 22:41
• Look up GraphLayout, same options are available. Commented Jun 27, 2019 at 23:32
• yes; good recommendation. I was looking at this in IGraph-M Commented Jun 28, 2019 at 15:11

You can use the option "Orientation" with "LayeredDigraphEmbedding" layout:

Graph[edges,
GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left},
VertexSize -> .6,
VertexLabelStyle -> 16,
VertexStyle -> {_ :> Directive[White, EdgeForm[Darker[Blue]]], S -> Red, T -> Green},
VertexLabels -> Placed["Name", Center],
EdgeStyle -> Directive[Black, Arrowheads[{{0.03, .8}}]],
ImageSize -> Large]


Alternatively, wrap the desired vertices with Style

Graph[VertexList[edges] /.{S -> Style[S, Red], T -> Style[T, Green]}, edges,
GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Left},
VertexSize -> .6,
VertexLabelStyle -> 16,
VertexStyle ->  Directive[White, EdgeForm[Darker[Blue]]],
VertexLabels -> Placed["Name", Center],
EdgeStyle -> Directive[Black, Arrowheads[{{0.03, .8}}]],
ImageSize -> Large]


same picture

• Fantastic ... thank you! Commented Jun 28, 2019 at 15:07

In the specific case requested, by writing:

edges = {S -> R9, S -> R4, S -> R1, R1 -> R2, R2 -> R3, R3 -> R4,
R4 -> R5, R5 -> R6, R6 -> T, R9 -> T, T -> S};

color = ConstantArray[RGBColor[1, 1, 1], 150];
fct[t_] := color[[Total[ToCharacterCode[ToString[t]]]]]
fct[S] = LightRed;
fct[R9] = LightBlue;

LayeredGraphPlot[edges, Left, VertexLabeling -> True,
EdgeRenderingFunction -> ({Black, Arrowheads[0.03],
Arrow[#1, 0.4]} &),
VertexRenderingFunction -> ({fct[#2], EdgeForm[Darker[Blue]],
Disk[#, .4], Black, Text[#2,#1]}&)]


I get:

which is what is desired.

Wanting to satisfy the needs requested in the comments, by writing:

edges = {S -> R9, S -> R4, S -> R1, R1 -> R2, R2 -> R3, R3 -> R4,
R4 -> R5, R5 -> R6, R6 -> T, R9 -> T, T -> S};

colorules = {{"S", "T", LightRed},
{"R1", "R3", "R5", "R9", LightBlue},
{"R2", "R4", "R6", LightGreen}};
fct[t_] := Last[colorules[[Position[colorules, ToString[t]][[1, 1]]]]]

LayeredGraphPlot[edges, Left, VertexLabeling -> True,
EdgeRenderingFunction -> ({Black, Arrowheads[0.03],
Arrow[#1, 0.4]} &),
VertexRenderingFunction -> ({fct[#2], EdgeForm[Darker[Blue]],
Disk[#, .4], Black, Text[#2,#1]}&)]


I get:

which is what is desired.

• Very nice! Is there a way to modify fct[t_]? Instead of writing fct[S] and fct[R9} as separate lines, if you want S, R2, R5, R9 all the same color (say light red) then can you write code for fct[S,R2,R5,R9}? Commented Jun 27, 2019 at 22:15
• TeM: Wonderful; thank you for sharing your creative approaches. I've learned a lot ... prg Commented Jun 28, 2019 at 15:09